Perl Eval function usage instance

Time:2021-12-24

As a scripting language, Perl can generate and execute code in real time. This feature can delay the compilation of code until runtime, so it is also called “dynamic code”. In addition, Perl also provides exception handling mechanism like Java and C + +. This article will discuss the function of dynamic code and exception handling mechanism in Perl: eval. If there are mistakes and deficiencies, welcome discussion, criticism and correction.

The eval function can be regarded as a Perl virtual machine, and its parameter is a piece of Perl code. Use ‘perldoc – f Eval’ to get help using Eval function, which introduces two ways to use it:

 

Copy codeThe code is as follows:

eval EXPR

 

Expr is an expression, for example:

 

Copy codeThe code is as follows:

eval “print $a” ;
eval ‘print $a’ . ‘, $b’ ;
eval 1 + 3 ;
eval ‘print ‘ . ‘$a + $b, “\n”‘ ;
eval $command;#$command = ‘print “hello Perl”’
eval $ARGV[0];

 

During execution, the Perl interpreter will first parse the value of the expression, and then insert the expression value as a Perl statement into the current execution context. Therefore, the newly generated statement has the same context as the eval statement itself. In this way, the expression is parsed every time the eval statement is executed. Therefore, if Eval expr appears in the loop, the expression may be parsed multiple times. This way of Eval enables Perl script programs to generate and execute code in real time, so as to realize “dynamic code”.

 

Copy codeThe code is as follows:

eval BLOCK

 

Block is a code block, for example:

 

Copy codeThe code is as follows:

eval {print $a};
eval {$a = 1, $b = 2, $c = $a + $b};

 

Different from the first method, block will be parsed only once, and then inserted into the execution context of the current Eval function. Due to the advantages of parsing performance and code syntax checking at compile time, this method is usually used as Perl to provide an exception capture mechanism for a piece of code, although the former method can also be used.

According to the name of the help, the parameter program of Eval is called “mini program”. In both methods, the return value of the eval function is the value of the last statement of the applet. If a return statement is encountered, it is the same as that of the subroutine.

Script1:

 

Copy codeThe code is as follows:

#!/usr/bin/perl -w
 
push ( @program,’$i = 1;’);
push ( @program,’$i = 3; $j = 2; $k = $i + $j’);
push ( @program, ‘$i = 3; return 24; $k = $i + $j’);
 
foreach $exp (@program)
{
    $rtn =eval($exp);
    print $rtn,”\n”;
}

 

Output:

 

Copy codeThe code is as follows:

1
5
24

 

If there are syntax errors and runtime errors in the applet and die statements are encountered, Eval will return undef. The error code is saved in [email protected]

Script2:

 

Copy codeThe code is as follows:

#!/usr/bin/perl -w
 
push ( @program, ‘$i = 3; die “error message”; $k = $i + $j’);
 
foreach $exp (@program)
{
    $rtn =eval($exp);
    if ( ! defined ( $rtn))
    {
       print “Exception: ” , [email protected],”\n”;
    }
    else
    {
       print $rtn,”\n”;
    }
} ;

 

Output:

 

Copy codeThe code is as follows:

Exception: error message at (eval 1) line 1.

 

Script3:

 

Copy codeThe code is as follows:

#!/usr/bin/perl -w
 
# a run-time error
eval ‘$answer =’ ;   # sets [email protected]
warn [email protected] [email protected];


Output:

Copy codeThe code is as follows:

syntax error at (eval 1) line 2, at EOF