Jennifer Garner wrote:
And, I have another syntax question here. I can't know clearly the
difference between eval "" and eval {}. I have run "perldoc -f eval" and
read it,but still can't know clearly.Can you help me on this?

eval {} or eval BLOCK is parsed only once at compile time. This means the Perl statements in it cannot change, only the values in the variables. eval "" or eval EXPR is parsed every time it is encountered. This means the Perl program can be completely changed. For example, suppose we want a program that compares the content of a file to a series of patterns but the patterns are determined by the user at run time. We can do this with two loops:

  while( <> ){
    chomp;
    for my $pattern ( @Patterns ){
      if( /$pattern/ ){
        process_when_found( $_ );
      }
    }
  }

We could use grep:

  while( my $line = <> ){
    chomp $line;
    if( grep { $line =~ /$_/ } @Patterns ){
      process_when_found( $line );
    }
  }

Or we could use eval

  my $expr = '/' . join( '/ || /', @Patterns ) . '/';
  while( <> ){
    chomp;
    if( eval $expr ){
      process_when_found( $_ );
    }
  }

eval EXPR is more flexible; eval BLOCK parses at compile time (meaning you, not the user, is notified of syntax errors).

Hopes this helps.

--

Just my 0.00000002 million dollars worth,
   --- Shawn

"Probability is now one. Any problems that are left are your own."
   SS Heart of Gold, _The Hitchhiker's Guide to the Galaxy_

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to