Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is $@ in Perl?

Tags:

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.
like image 751
Cool_Coder Avatar asked Apr 30 '13 06:04

Cool_Coder


2 Answers

$@ 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

like image 90
Suic Avatar answered Nov 16 '22 22:11

Suic


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).

like image 40
zoul Avatar answered Nov 16 '22 22:11

zoul