Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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"

In reply to Re^2: "eval" and "my" variable weirdness by ikegami
in thread "eval" and "my" variable weirdness by splinky

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (5)
As of 2024-03-28 10:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found