Perldoc Search: "$@" perl-5.20.1

search official POD

17 PODs, 74 LINEs found.
230 :             if ($@) {
231 :                 die unless $@ eq "alarm\n";   # propagate unexpected errors
652 :         "die" raises an exception. Inside an "eval" the error message is stuffed into $@ and the "eval" is terminated with the undefined value. If the exception is outside of all enclosing "eval"s, then the uncaught exception prints LIST to "STDERR" and exits with a non-zero value. If you need to exit the process with a specific exit code, see "exit".
671 :         If the output is empty and $@ already contains a value (typically from a previous eval) that value is reused after appending "\t...propagated". This is useful for propagating exceptions:
674 :             die unless $@ =~ /Expected exception/;
676 :         If the output is empty and $@ contains an object reference that has a "PROPAGATE" method, that method will be called with additional file and line number parameters. The return value replaces the value in $@; i.e., as if "$@ = eval { $@->PROPAGATE(__FILE__, __LINE__) };" were called.
678 :         If $@ is empty then the string "Died" is used.
688 :         You can also call "die" with a reference argument, and if this is trapped within an "eval", $@ contains that reference. This permits more elaborate exception handling using objects that maintain arbitrary state about the exception. Such a scheme is sometimes preferable to matching particular string values of $@ with regular expressions. Because $@ is a global variable and "eval" may be used within object implementations, be careful that analyzing the error object doesn't replace the reference in the global variable. It's easiest to make a local copy of the reference before any manipulations. Here's an example:
693 :             if (my $ev_err = $@) {
729 :         If "do" can read the file but cannot compile it, it returns "undef" and sets an error message in $@. If "do" cannot read the file, it returns undef and sets $! to the error. Always check $@ first, as compilation could fail in a way that also sets $!. If the file is successfully compiled, "do" returns the value of the last expression evaluated.
740 :                     warn "couldn't parse $file: $@" if $@;
843 :         If there is a syntax error or runtime error, or a "die" statement is executed, "eval" returns "undef" in scalar context or an empty list in list context, and $@ is set to the error message. (Prior to 5.16, a bug caused "undef" to be returned in list context for syntax errors, but not for runtime errors.) If there was no error, $@ is set to the empty string. A control flow operator like "last" or "goto" can bypass the setting of $@. Beware that using "eval" neither silences Perl from printing warnings to STDERR, nor does it stuff the text of warning messages into $@. To do either of those, you have to use the $SIG{__WARN__} facility, or turn off warnings inside the BLOCK or EXPR using "no warnings 'all'". See "warn", perlvar, and warnings.
849 :         If the code to be executed doesn't vary, you may use the eval-BLOCK form to trap run-time errors without incurring the penalty of recompiling each time. The error, if any, is still returned in $@. Examples:
852 :             eval { $answer = $a / $b; }; warn $@ if $@;
855 :             eval '$answer = $a / $b'; warn $@ if $@;
861 :             eval '$answer =';   # sets $@
867 :             warn $@ if $@;
876 :                print $@ if $@;                # prints "bar lives here"
894 :         Before Perl 5.14, the assignment to $@ occurred before restoration of localized variables, which means that for your code to run on older versions, a temporary is required if you want to mask some but not all errors:
896 :             # alter $@ on nefarious repugnancy only
900 :                  local $@; # protect existing $@
902 :                  # $@ =~ /nefarious/ and die $@; # Perl 5.14 and higher only
903 :                  $@ =~ /nefarious/ and $e = $@;
2946 :                         croak $@ ? "$@Compilation failed in require"
3326 :             die $@ unless $@ eq "Alarm!\n";
4858 :         If the output is empty and $@ already contains a value (typically from a previous eval) that value is used after appending "\t...caught" to $@. This is useful for staying almost, but not entirely similar to "die".
4860 :         If $@ is empty then the string "Warning: Something's wrong" is used.
130 :     Whenever control returns from the *call_** function you need to check the $@ variable as you would in a normal Perl script.
140 :     *    If G_SCALAR is specified *and* an error has occurred, the return value will be 1 and the value on the top of the stack will be *undef*. This means that if you have already detected the error by checking $@ and you want the program to continue, you must remember to pop the *undef* from the stack.
145 :     Using the G_EVAL flag described above will always set $@: clearing it if there was no error, and setting it to describe the error if there was an error in the called code. This is what you want if your intention is to handle possible errors, but sometimes you just want to trap errors and stop them interfering with the rest of the program.
151 :     When G_KEEPERR is used, any error in the called code will terminate the call as usual, and the error will not propagate beyond the call (as usual for G_EVAL), but it will not go into $@. Instead the error will be converted into a warning, prefixed with the string "\t(in cleanup)". This can be disabled using "no warnings 'misc'". If there is no error, $@ will not be cleared.
153 :     Note that the G_KEEPERR flag does not propagate into inner evals; these may still set $@.
576 :              print "Uh oh - $@\n" if $@;
578 :          "PL_errgv" is a perl global of type "GV *" that points to the symbol table entry containing the error. "ERRSV" therefore refers to the C equivalent of $@.
600 :         print "Saw: $@" if $@;             # should be, but isn't
602 :     This example will fail to recognize that an error occurred inside the "eval {}". Here's why: the call_Subtract code got executed while perl was cleaning up temporaries when exiting the outer braced block, and because call_Subtract is implemented with *call_pv* using the G_EVAL flag, it promptly reset $@. This results in the failure of the outermost test for $@, and thereby the failure of the error trap.
291 :             Due to an implementation glitch, the $SIG{__DIE__} hook is called even inside an "eval()". Do not use this to rewrite a pending exception in $@, or as a bizarre substitute for overriding "CORE::GLOBAL::die()". This strange action at a distance may be fixed in a future release so that $SIG{__DIE__} is only called if your program is about to exit, as was the original intent. Any other use is deprecated.
820 :     The variables $@, $!, $^E, and $? contain information about different types of error conditions that may appear during execution of a Perl program. The variables are shown ordered by the "distance" between the subsystem which reported the error and the Perl process. They correspond to errors detected by the Perl interpreter, C library, operating system, or an external program, respectively.
832 :     $@ is set if the string to be "eval"-ed did not compile (this may happen if "open" or "close" were imported with bad prototypes), or if Perl code executed during evaluation "die()"d. In these cases the value of $@ is the compile error, or the argument to "die" (which will interpolate $! and $?). (See also Fatal, though.)
836 :     Finally, $? may be set to non-0 value if the external program /cdrom/install fails. The upper eight bits reflect specific error conditions encountered by the program (the program's "exit()" value). The lower eight bits reflect mode of failure, like signal death and core dump information. See wait(2) for details. In contrast to $! and $^E, which are set only if error condition is detected, the variable $? is set on each "wait" or pipe "close", overwriting the old value. This is more like $@, which on every "eval()" is always set on failure and cleared on success.
838 :     For more details, see the individual descriptions at $@, $!, $^E, and $?.
937 :     $@      The Perl syntax error message from the last "eval()" operator. If $@ is the null string, the last "eval()" parsed and executed correctly (although the operations you invoked may have failed in the normal fashion).
28 :         eval 'exec perl -x -wS $0 ${1+"$@"}'
252 :          Since it is an actual "do" (not a "require"), doesn't need to return a true value. The code is run in package "main", in its own lexical scope. However, if the script dies, $@ will not be set.
427 :              eval 'exec /usr/bin/perl -wS $0 ${1+"$@"}'
430 :          The system ignores the first line and feeds the program to /bin/sh, which proceeds to try to execute the Perl program as a shell script. The shell executes the second line as a normal shell command, and thus starts up the Perl interpreter. On some systems $0 doesn't always contain the full pathname, so the -S tells Perl to search for the program if necessary. After Perl locates the program, it parses the lines and ignores them because the variable $running_under_some_shell is never true. If the program will be interpreted by csh, you will need to replace "${1+"$@"}" with $*, even though that doesn't understand embedded spaces (and such) in the argument list. To start up *sh* rather than *csh*, some systems may have to replace the "#!" line with a line containing just a colon, which will be politely ignored by Perl. Other systems can't control that, and need a totally devious construct that will work under any of *csh*, *sh*, or Perl, such as the following:
432 :                  eval '(exit $?0)' && eval 'exec perl -wS $0 ${1+"$@"}'
909 :        sub myjoin ($@)         myjoin ":", $a, $b, $c
911 :        sub mysplice (+$$@)     mysplice @array, 0, 2, @pushme
923 :         sub myref (\[$@%&*])
974 :             if ($@) {
975 :                 local $_ = $@;
443 :         if ($@ =~ /^Unimplemented at /) {
545 :         eval qq[\n#line 2001 ""\ndie 'foo']; print $@;
550 :         eval qq[\n#line 200 "foo bar"\ndie 'foo']; print $@;
557 :         print $@;
581 :             die $@ if $@;
588 :          die $@ if $@;
647 :                 /* check $@ */
877 :         (F) You passed die() an empty string (the equivalent of "die """) or you called it with no args and $@ was empty.
3010 :         (P) Perl was about to print an error message in $@, but the $@ variable did not exist, even after an attempt to create it.
3484 :         (W) You passed warn() an empty string (the equivalent of "warn """) or you called it with no args and $@ was empty.
17 :       $@%&*  dereference     $$foo{bar}     aka $foo->{bar}
66 :        6 rdev   6 weekday     6 evaltext    $@    eval error
556 :     If your "DESTROY" method throws an error, this error will be ignored. It will not be sent to "STDERR" and it will not cause the program to die. However, if your destructor is running inside an "eval {}" block, then the error will change the value of $@.
558 :     Because "DESTROY" methods can be called at any time, you should localize any global variables you might update in your "DESTROY". In particular, if you use "eval {}" you should localize $@, and if you use "system" or backticks you should localize $?.
1436 :             die $@ if $@;
1438 :             eval "tr/$oldlist/$newlist/, 1" or die $@;
107 :             local $@;   # Don't pollute caller's value.
632 :             eval 'exec /usr/local/bin/perl -S $0 ${1+"$@"}'
110 :         if ($@ && $@ !~ quotemeta($ALARM_EXCEPTION)) { die }
208 :         die $@ if $@;
338 :         sub MySub($$@) {
150 :     Using 'X' and cousins requires you not to use the type identifiers ($@%), just the 'name':
<< Back to Perldoc Search