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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.