in reply to Re: I can't see why this shorthand doesn't behave like the verbose form
in thread I can't see why this shorthand doesn't behave like the verbose form

O'Reilly's Advanced Perl Programming sais that eval'ed code is executed in the environment of the surrounding code, just as if the eval wasn't there - so declaring it inside or outside of the eval block is irrelevant
Anyway, the eval just added noise to the core of my post, that's why I removed it from the follow up example.
  • Comment on Re^2: I can't see why this shorthand doesn't behave like the verbose form

Replies are listed 'Best First'.
Re^3: I can't see why this shorthand doesn't behave like the verbose form
by Anonymous Monk on Mar 25, 2012 at 14:17 UTC

    That does not change the scoping rules , {   } introduces a new scope

    $ perl -le " eval { my $foo = 1; } ; print $foo; " $ perl -le " use strict; eval { my $foo = 1; } ; print $foo; " Global symbol "$foo" requires explicit package name at -e line 1. Execution of -e aborted due to compilation errors.
      Out of my scope ;)
      I thought, in eval's case the { } were just nice decoration...

        :) I wonder what happens when you get rid of them

        $ perl -le " eval my $foo = 1; " Can't modify eval "string" in scalar assignment at -e line 1, near "1; +" Execution of -e aborted due to compilation errors. $ perl -MO=Deparse,-p -le " eval my $foo = 1; " Can't modify eval "string" in scalar assignment at -e line 1, near "1; +" -e had compilation errors. BEGIN { $/ = "\n"; $\ = "\n"; } (eval(my $foo) = 1); $ perl -le " eval( my $foo = 1;) " syntax error at -e line 1, near "1;" Execution of -e aborted due to compilation errors. $ perl -le " eval( my $foo = 1) " $ perl -le " eval( my $foo = q/print 666/ ) " 666

        Uh oh :)