PERL - The String Form: Expression Evaluation

Discussion in 'Perl' started by shree, Aug 16, 2007.

  1. shree

    shree New Member

    Joined:
    Aug 16, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    IT
    Location:
    Bangalore
    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:
    # 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:
    # 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:
    $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.
     
  2. tomjugirl

    tomjugirl New Member

    Joined:
    Aug 18, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    who is who

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

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Re: who is who

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

    rajkumar_singhalmca New Member

    Joined:
    Aug 22, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    student
    Location:
    bikaner(raj)
    hello, everybody i am new go4expert is very good
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I am not sure why everybody is introducing here instead of the Introduce yourself forum but anyway welcome to the forum.
     
  6. pradeep

    pradeep Team Leader

    Joined:
    Apr 4, 2005
    Messages:
    1,645
    Likes Received:
    87
    Trophy Points:
    0
    Occupation:
    Programmer
    Location:
    Kolkata, India
    Home Page:
    http://blog.pradeep.net.in
    Easy way of 'slurping' contents of a file

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

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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!
     
  8. oleber

    oleber New Member

    Joined:
    Apr 23, 2007
    Messages:
    37
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Software Developer (Perl, C/C++ and Java)
    Location:
    Hamburg, Germany
    Home Page:
    http://oleber.freehostia.com/

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice