It evaluates the code that you give it.  It can be used when you need to
create code on the fly, like this...

my $cmd = 'print';
my $arg = 'Hello World';
eval("$cmd '$arg'");

This is useful for allowing a user to pass code to the program (for whatever
reason).

The other use it to trap errors.

my $this = 1;
my $that = 2;
eval {
  if ($this != $that) {
    die "They don't match!";
  }
};
print $@;

In this case the "die" doesn't terminate the program, it only terminates the
eval.  And $@ stores the error message that was "thrown" in the eval.  So
this prints the error message without abnormally terminating the program.
This is akin to the try/catch blocks in other languages (the Errors gives
you try/catch functionality by using eval).

Hope that helps.

Rob

-----Original Message-----
From: Mallik [mailto:[EMAIL PROTECTED]
Sent: Thursday, January 29, 2004 7:26 AM
To: Perl Beginners
Subject: What is eval?


Can anybody explain the functionality of eval in brief?

Thanks in advance,
Mallik.

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


-- 
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