in reply to Re^2: "eval" and "my" variable weirdness
in thread "eval" and "my" variable weirdness

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"'; } }

⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Replies are listed 'Best First'.
Re^4: "eval" and "my" variable weirdness
by ikegami (Patriarch) on Jun 29, 2006 at 17:23 UTC
    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"

      Your version is dangerously close to getting optimized away into nothingness. Just disable the warning. That's much safer.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

        It *is* optimized away (according to Deparse), but not before satisfying our needs. The safe version would be:

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