Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: "eval" and "my" variable weirdness

by japhy (Canon)
on Jun 29, 2006 at 16:05 UTC ( #558355=note: print w/replies, xml ) Need Help??


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

I don't think it's a bug, but I might be wrong. The problem is that there's no reason for Perl to keep $thingy (a lexical) around once it's done compiling Bob.pm because nothing that "lasts" refers to it. However, if you have my $x = $thingy in a function in Bob.pm, that basically produces a closure around $thingy and keeps $thingy in existence.

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

Replies are listed 'Best First'.
Re^2: "eval" and "my" variable weirdness
by ikegami (Patriarch) on Jun 29, 2006 at 16:54 UTC

    Absolutely right japhy, but some might find your explanation a bit hard to swallow. It's a complicated topic, so I'll try to illustrate it.


    The minimal code illustrating the problem is:

    # Module.pm use warnings; package Module; my $thingy = 'thangy'; sub doit { eval 'print "thingy is $thingy\n"'; } 1;
    use Module; Module::doit(); # "thingy is "

    Each Perl files (both scripts and modules) create a scope. Just like adding {...} around statements creates a scope for the contained statements, use, require, etc create a scope in which the file is executed.

    The means the above code is equivalent to:

    { use warnings; package Module; my $thingy = 'thangy'; sub doit { eval 'print "thingy is $thingy\n"'; } 1; } Module::doit(); # "thingy is "

    Let's simplify the above to:

    { use warnings; my $thingy = 'thangy'; sub doit { eval 'print "thingy is $thingy\n"'; } } doit(); # "thingy is "

    When Perl reached the "}", it doesn't realize you'll still need $thingy (since the eval hasn't run yet), so $thingy is freed. If you added use strict; to the string being evaled, you'd get a strict error. eval's $thingy refers not to the lexical, but to the package variable $main::thingy.

    Enter closures. Closures force variables to stick around longer than they normally would:

    { use warnings; my $thingy = 'thangy'; sub doit { print "thingy is $thingy\n"; } } doit(); # "thingy is thangy"

    So you need to add a reference to $thingy in your sub so Perl knows (at compile-time) that $thingy is still needed:

    { use warnings; my $thingy = 'thangy'; sub doit { my $ref = \$thingy; # Close over $thingy eval 'print "thingy is $$ref\n"'; } } doit(); # "thingy is thangy"

    By "reference", I meant "occurance". In the previous snippet, my reference to $thingy is in the expression creating a reference to $thingy, but it need not be so:

    { use warnings; my $thingy = 'thangy'; sub doit { my $thingy = $thingy; # Close over $thingy eval 'print "thingy is $thingy\n"'; } } doit(); # "thingy is thangy"

    Alternatively, use package variables instead of lexical variables. They don't care about scope:

    { use warnings; our $thingy = 'thangy'; sub doit { eval 'print "thingy is $thingy\n"'; } } doit(); # "thingy is thangy"

      You're working too hard and breaking stuff. Don't redeclare $thingy, just use it once. When you redeclare you've just gotten yourself a whole different thingy and not the thingy you meant to get. Same data tho. Just a different thingy.

      { use warnings; my $thingy = 'thangy'; sub doit { $thingy; # Close over $thingy eval 'print "thingy is $thingy\n"'; } }

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        Of course! You're right, except your version gives a warning. The following removes it:
        { use warnings; my $thingy = 'thangy'; sub doit { 0 && $thingy; # Close over $thingy eval 'print "thingy is $thingy\n"'; } } doit(); # "thingy is thangy"

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (6)
As of 2022-05-28 21:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (101 votes). Check out past polls.

    Notices?