It seems to me that "my $x" has gone out of scope by the time the anon sub runs...

It has.

is the value still around because there remains a ref to it?

Yes.

If so, what is doing the ref... the anon sub itself?

No, not in my book. The reference to the anonymous sub is a special type of "code ref" called a closure (yes, we all knew that by now). This closure (the code ref) is what holds a reference to the lexical (and not the anonymous sub which is separate from the reference in my book -- especially since the anonymous sub is not recompiled every time you take another reference to it).

So $h holds (something that contains) a reference to the $x that was set to "Howdy" while $g holds a reference to the $x that was set to "Greetings".

Make $x an object and you can see that destroying $h triggers a destructor, etc:

#!/usr/bin/perl -w use strict; sub Obit::new { my( $this, $ref )= @_; my $desc= "$ref"; $desc .= " (${$ref})" if UNIVERSAL::isa( $ref, "SCALAR" ); warn "$desc is born.\n"; return bless $ref, $this; } sub Obit::DESTROY { my $self= shift; my $desc= "$self"; $desc .= " (${$self})" if $self->isa("SCALAR"); warn "$desc has died.\n"; } sub newprint { my $x= Obit->new( \shift ); return sub { my $y= shift; print "${$x}, $y!\n"; }; } $|= 1; { my $h= Obit->new( newprint("Howdy") ); warn "About to destroy \$h.\n"; } warn "\$h is no more.\n"; { my $g= Obit->new( newprint("Greetings") ); warn "About to destroy \$g.\n"; } warn "\$g is no more.\n"; __END__ SCALAR(0x1bbefc0) (Howdy) is born. CODE(0x1bbf074) is born. About to destroy $h. Obit=CODE(0x1bbf074) has died. Obit=SCALAR(0x1bbefc0) (Howdy) has died. $h is no more. SCALAR(0x1bb50d4) (Greetings) is born. CODE(0x1bbf074) is born. About to destroy $g. Obit=CODE(0x1bbf074) has died. Obit=SCALAR(0x1bb50d4) (Greetings) has died. $g is no more.
        - tye (but my friends call me "Tye")

In reply to (tye)Re: Closures and scope by tye
in thread Closures and scope by nop

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.