Re: Eval Errors

From: James Edward Gray II (james_at_grayproductions.net)
Date: 02/23/04


Date: Mon, 23 Feb 2004 14:21:51 -0600
To: Perl Beginners <beginners@perl.org>

As several have pointed out now, my description of the problem was
involved. Sorry about that. I was showing you a tiny slice of the
3,000 line server where I know the problem is occurring.

However, thanks to the help provided, I managed to simplify the problem
to a small script. We can talk about this sucker and all be on the
same page. Behold:

#!/usr/bin/perl

use strict;
use warnings;

my @strings = ( q(my $bad_syntax = ;),
                                q('a poorly 'nested' string'),
                                q('a poorly 'nested::test' string') );

foreach (@strings) {
        print "\nCode: $_\n";
        {
                print "\nDisabling warnings...\n";
                no warnings;
                print "Calling eval()...\n";
                eval;
        }
        print "eval() complete.\n\n";
        if ($@) {
                print "Handling error...\n";
                print "Caught error: $@";
        }
}

__END__

Interesting things to note about the above: All three @strings are bad
perl code and don't compile. Warnings are disabled when I run the
eval() and thus, not a part of the equation. (The script runs
identically if they are left on.) We're not catching eval()'s return
value here, so it's not part of the equation either. Note how similar
the second and third string are as well.

When I run this code, I get:

Code: my $bad_syntax = ;

Disabling warnings...
Calling eval()...
eval() complete.

Handling error...
Caught error: syntax error at (eval 1) line 1, at EOF

Code: 'a poorly 'nested' string'

Disabling warnings...
Calling eval()...
eval() complete.

Handling error...
Caught error: Bad name after nested' at (eval 2) line 1.

Code: 'a poorly 'nested::test' string'

Disabling warnings...
Calling eval()...
Bareword found where operator expected at (eval 3) line 1, near "'a
poorly 'nested::test"
         (Missing operator before nested::test?)
String found where operator expected at (eval 3) line 1, near
"nested::test' string'"
eval() complete.

Handling error...
Caught error: syntax error at (eval 3) line 1, near "'a poorly
'nested::test"

__END__

The first two results are what I expect/want. The third string, a
super simplified example of the typo I fed my server this morning,
throws errors inside the eval() call and yet the script keeps on
trucking along!

Can anyone explain to me what's going on here?

James