Here's another example:
# 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__
If one runs foo.pl, the output is
OK
NOT OK
...meaning that frobozz sees $x but quux doesn't. If one uncomments the commented line in quux or runs bar.pl directly, the output is
OK
OK
(This is true for both 5.8.6 and 5.8.8 on Linux.)
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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|