Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Perl (http://www.go4expert.com/articles/perl-tutorials/)
-   -   PERL - The String Form: Expression Evaluation (http://www.go4expert.com/articles/perl-string-form-expression-evaluation-t5895/)

shree 16Aug2007 16:07

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

# Slurp in the entire file
while ($line = <>) {
  $str .= $line; # Accumulate the entire file.
}

# $str now contains the entire file. Execute it !
eval $str;

As you can see, eval handles any Perl script handed to it. The beauty of this thing is that this facility is available not just to Larry, but to mortals like you and me. Try this:
Code: PERL

# put some code inside $str
$str = '$c = $a + $b'; # Perl doesn't care what's inside $str
$a = 10; $b = 20;
eval $str; # Treat $str as code, and execute it.
print $c; # prints 30

In this snippet, $str is treated as an ordinary string at first, because that is what it is. But eval thinks of it as a program and executes it. The important point is that it doesn't think of it as a separate program, but as if it belonged right there in the original code instead of the eval statement. For this reason, the string that is given to eval can use variables and subroutines available to it at that point, including my and local variables, and optionally produce new ones in the same environment. In the preceding example, the string given to eval adds two initialized variables ($a and $b) and produces a new variable, $c. If you have more than one statement inside the string (remember that the string can be as big a program as you want), eval evaluates all of them and returns the result of the last evaluation:
Code: PERL

$str = '$a++; $a + $b'; # Contains two expressions
$a = 10; $b = 20;
$c = eval $str; # $c gets 31 (result of the 2nd expression, $a+$b)

Of course, it's quite pointless to eval a piece of code that you know at compile time, as in the example above. Things get interesting if $str comes from elsewhere - standard input, a file, or over the network. We will shortly look at some examples that make use of this. NOTE: The string form of eval is a security risk. If the string argument comes from an untrusted source and contains, say, system('rm *') the code would be merrily executed - and result in a distinct lack of merriment on your part.

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.

tomjugirl 18Aug2007 08:09

who is who
 
Hi! I'm new here. Just want to say hi to everyone, see who is who here. :) Tomara

shabbir 18Aug2007 09:40

Re: who is who
 
Quote:

Originally Posted by tomjugirl
Hi! I'm new here. Just want to say hi to everyone, see who is who here. :) Tomara

Welcome to the forum but you should be doing that in the Introduce yourself forum.

rajkumar_singhalmca 25Aug2007 13:35

Re: PERL - The String Form: Expression Evaluation
 
hello, everybody i am new go4expert is very good

shabbir 25Aug2007 18:18

Re: PERL - The String Form: Expression Evaluation
 
Quote:

Originally Posted by rajkumar_singhalmca
hello, everybody i am new go4expert is very good

I am not sure why everybody is introducing here instead of the Introduce yourself forum but anyway welcome to the forum.

pradeep 13Jul2010 18:49

Re: PERL - The String Form: Expression Evaluation
 
Easy way of 'slurping' contents of a file

Code: Perl

open H,"<file.txt";
my $data = do{ local $/; <H>;};
close H;


shabbir 13Jul2010 19:57

Re: PERL - The String Form: Expression Evaluation
 
Quote:

Originally Posted by pradeep (Post 70210)
Easy way of 'slurping' contents of a file

Code: Perl

open H,"<file.txt";
my $data = do{ local $/; <H>;};
close H;


Nice to see you around after a long time and your sig is still couple of years back. To err is human,to detect is divine!

oleber 27Jul2010 11:01

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 08:06.