Try printing $two->{msg} at the beginning of the anonymous sub (with warnnings on.). Here, on 5.8.3 I get an uninitialized value. Also by printing the stringified \$two in closure(), and in the anon sub, I get different strings.
I find a suitable explanation in perldiag:
Variable "%s" may be unavailable
(W closure) An inner (nested) anonymous subroutine is inside a named subroutine, and outside that is another subroutine; and the anonymous (innermost) subroutine is referencing a lexical variable defined in the outermost subroutine. For example:
sub outermost { my $a; sub middle { sub { $a } } }
If the anonymous subroutine is called or referenced (directly or indirectly) from the outermost subroutine, it will share the variable as you would expect. But if the anonymous subroutine is called or referenced when the outermost subroutine is not active, it will see the value of the shared variable as it was before and during the *first* call to the outermost subroutine, which is probably not what you want.
In these circumstances, it is usually best to make the middle subroutine anonymous, using the sub {} syntax. Perl has specific support for shared variables in nested anonymous subroutines; a named subroutine in between interferes with this feature.
Only here, we have just a scope rather than a named outer sub, and no warning is given.
But, as it appears, $two isn't replaced in the anon sub, and therefore lasts after the end of execution, then five is printed, and four and two are collected in some order. (I get : one, three, five, two, four).
If you replace sub closure with our $closure=sub, and closure() with $::closure->(), $two remains shared, and I get : one,three,two,five,four.
| |
| [reply] [Watch: Dir/Any] [d/l] [select] |