1

I'm trying to understand the following piece of code:

sub foo {
    ...
    if ( $@ ) {
        ...
        die $@;
    }
}
Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Jin Kim
  • 16,562
  • 18
  • 60
  • 86

3 Answers3

13

perldoc -f eval:

If there is a syntax error or runtime error, or a "die" statement is executed, an undefined value is returned by "eval", and $@ is set to the error message. If there was no error, $@ is guaranteed to be a null string.

See also perldoc perlvar.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
3

$@ is a magic variable containing the error message of the last eval command, if any.

brian d foy
  • 129,424
  • 31
  • 207
  • 592
Nick Lewis
  • 4,150
  • 1
  • 21
  • 22
0

The if loop should be preceded by eval for it to be able to trap $@.

During an eval(), $@ is always set on failure and cleared on success.

In case whr code inside eval() did not compile, $@ is set to the compilation error.

angel_007
  • 103
  • 6