in reply to Iterative anonymous subroutines

The proper word is recursive, not iterative. Iterative means something else.

The error occurs because the my hasn't occured at the time where $anon is used. (Assignments are processed right to left.) Therefore, the sub uses the package variable $main::anon. Cause the my to occur first by seperating the my from the assignment:

my $anon; $anon = sub { my $count = shift; if ($count--) { print "Iterating\n"; $anon->($count); } }; $anon->(4);

Update: Elaborated on the problem.

Replies are listed 'Best First'.
Re^2: Iterative anonymous subroutines
by diotalevi (Canon) on Dec 13, 2005 at 18:55 UTC
    That's a guaranteed memory leak. You'll need to Scalar::Util::weaken $anon at some point to break the closure's circular reference.
      aye. Refer to the posts by demerphq and stvn for solutions that don't leak.
Re^2: Iterative anonymous subroutines
by ysth (Canon) on Dec 13, 2005 at 19:56 UTC
    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.

      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;