Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^2: Know Your Garbage -- A Quiz

by Ido (Hermit)
on Jun 19, 2004 at 13:16 UTC ( [id://368163]=note: print w/replies, xml ) Need Help??


in reply to Re: Know Your Garbage -- A Quiz
in thread Know Your Garbage -- A Quiz

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://368163]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2024-04-26 09:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found