Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
more useful options
 
PerlMonks  

Re: "eval" and "my" variable weirdness

by tlm (Prior)
on Jun 29, 2006 at 16:41 UTC ( #558373=note: print w/ replies, xml ) Need Help??


in reply to "eval" and "my" variable weirdness

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.

the lowliest monk


Comment on Re: "eval" and "my" variable weirdness
Select or Download Code
Re^2: "eval" and "my" variable weirdness
by japhy (Canon) on Jun 29, 2006 at 16:50 UTC
    Again, the "problem" is that, when bar.pl is require()d, Perl has to make some scope decisions. The quux() function doesn't make any claims to $x, so it's not a closure around $x. frobozz() is a closure around $x.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://558373]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (12)
As of 2014-04-24 11:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (565 votes), past polls