in reply to Re: Iterative anonymous subroutines
in thread Iterative anonymous subroutines

The error occurs because the my hasn't occured at the time where $anon is used. (Assignments are processed right to left.)
Not exactly; the problem isn't the run-time effect of my not having occurred, it's with the compile-time effect.

my() "returns" (not exactly, since it's a declarator, not a function) the new lexical and that "returned" SV can be used in the current statement, but for purposes of compiling other mentions of the lexical of that name, the lexical's scope only begins with the following statement.

This can be seen with

perl -we'$foo = 2; print((my $foo=4),$foo)' </c> printing 42, not 44.

Replies are listed 'Best First'.
Re^3: Iterative anonymous subroutines
by duff (Parson) on Dec 14, 2005 at 00:45 UTC

    And everyone is going to have to learn the other way for perl 6 because lexicals spring into existence immediately upon being parsed (not the whole statement, just the variable). So, in this code:

    my $x = $x + 5;
    Both $x refer to the same storage location (the same variable). To get the perl5 behavior you have to say something like:
    my $x = $OUTER::x + 5;