Cool_Coder
Cool_Coder

Reputation: 5073

What is $@ in Perl?

I have a Perl script which I cannot understand.

for $i(@myarr)
{
    #some stuff
    eval {
        #some stuff
    };
    if($@)
    {
        print "*** $@ ****";
    }
}
  1. What does that eval do? Is it a standard Perl command or just some sub?
  2. What is the variable $@? It is currently printing a string but I don know where that string is coming from.

Upvotes: 15

Views: 22871

Answers (2)

zoul
zoul

Reputation: 104065

To add to Suic’s answer, see the English module that lets you use more descriptive $EVAL_ERROR instead of $@ and the Try::Tiny or TryCatch modules that avoid common traps associated with using eval for exception handling. Also, the next time you wonder about a Perl function, perldoc -f is your friend (like perldoc -f eval).

Upvotes: 10

Suic
Suic

Reputation: 2501

$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval. please read this doc http://perldoc.perl.org/perlvar.html

Upvotes: 21

Related Questions