![]() |
PERL - The String Form: Expression Evaluation
When Perl is given a file to execute or a string as a command line option (using -e), it needs to parse the contents, check it for syntax errors, and, if all is fine, execute it. Perl makes this feature available to the programmer through the eval string form. This contrasts powerfully with languages such as C, C++, or Java, where the compiler itself is a separate beast from your program, not available to it at run-time. In other words, the Perl interpreter itself works somewhat like this:
Code: PERL
Code: PERL
Code: PERL
In situations in which you cannot trust input, you can use the taint-checking option provided by Perl, which prevents you from using data derived from outside the program to affect files or things outside the program [5]. You can also use the Safe module bundled with the Perl distribution, which provides safe compartments in which to eval strings, similar to the environment that a web browser provides for Java or Tcl/Tk applets. What if $str doesn't contain a valid Perl expression? Perl then puts an error message in a special variable called $@ (or $EVAL_ERROR, if you use the English module). Since eval compiles the string before actually executing it, this can be either a compilation or a run-time error. $@ is guaranteed to be undef if $str contains error-free code (well, I should say free of syntax errors, because it can't really protect you against flawed logic). Since eval is used by the Perl interpreter itself to parse and execute a given script, the error strings (in $@) are exactly those you see on the standard error output when processing a flawed script. There is one subtle, yet important, point that needs to be mentioned. eval treats the string as a block, which is why it is able to process a number of statements (not just expressions) and return the value of the last statement. This also means that you don't see the changes to localized or lexical variables present in the eval'ed string. |
who is who
Hi! I'm new here. Just want to say hi to everyone, see who is who here. :) Tomara
|
Re: who is who
Quote:
|
Re: PERL - The String Form: Expression Evaluation
hello, everybody i am new go4expert is very good
|
Re: PERL - The String Form: Expression Evaluation
Quote:
|
Re: PERL - The String Form: Expression Evaluation
Easy way of 'slurping' contents of a file
Code: Perl
|
Re: PERL - The String Form: Expression Evaluation
Quote:
|
Re: PERL - The String Form: Expression Evaluation
There are multiple ways to do a file slurp:
http://sites.google.com/site/oleberp...2-slurp-a-file personaly I prefer: use File::Slurp qw(slurp); my $body = slurp($filepath); |
| All times are GMT +5.5. The time now is 10:24. |