note
tlm
<p>Here's another example:</p>
<c>
# foo.pl
use strict;
use warnings;
require 'bar.pl';
frobozz( 2 );
__END__
# bar.pl
use strict;
use warnings;
{
my $x; # same thing with "my $x = 'whatever';"
frobozz( 1 ) unless caller;
sub frobozz {
( $x ) = @_;
print 'NOT ' unless defined eval( '$x' );
print "OK\n";
quux();
}
sub quux {
# $x = $x;
print 'NOT ' unless defined eval( '$x' );
print "OK\n";
}
}
1;
__END__
</c>
If one runs foo.pl, the output is
<pre>
OK
NOT OK
</pre>
...meaning that <c>frobozz</c> sees <c>$x</c> but <c>quux</c> doesn't. If one uncomments the commented line in <c>quux</c> or runs <c>bar.pl</c> directly, the output is
<pre>
OK
OK
</pre>
<p>
(This is true for both 5.8.6 and 5.8.8 on Linux.)
</p>
<p>After re-reading the docs on eval, I can't see how a programmer can be expected to predict this behavior. Therefore, it is, at the very least, a design bug, IMO.</p>
<div class="pmsig"><div class="pmsig-439528">
<p><small>the lowliest monk</small></p>
</div></div>
558346
558346