>>>>> "tayers" ==   <[EMAIL PROTECTED]> writes:

tayers> Given the above more complete descriptions I would say the usage in
tayers> L<perlsub> is confusing. 

I agree with that, and with your general observation.  From my hanging
out on P5P, a subroutine is only a closure when it sees lexical
variables and must therefore create a distinct clone of the
environment on the way out.

And both named subroutines and anonymous subroutines can be closures.

Here's how to tell if a subroutine is a closure or not:

    for (1..5) {
      push @a, sub { "hi there" };
    }
    for (1..5) {
      {
        my $b;
        push @b, sub { $b."hi there" };
      }
    }
    print "anon normal: @a\n";
    print "anon closure: @b\n";

which generates

    anon normal: CODE(0x80ce9d4) CODE(0x80ce9d4) CODE(0x80ce9d4) CODE(0x80ce9d4) 
CODE(0x80ce9d4)
    anon closure: CODE(0x80c83ac) CODE(0x80d2440) CODE(0x80d24a0) CODE(0x80d2500) 
CODE(0x80d2560)

Note how each coderef from the non-closure is identical, but the
closure form must generate distinct coderefs to point at the distinct
instances of the closure.

This is the underpinnings of that famous "won't stay shared" message.
A "my" variable in a named subroutine context is generating distinct
coderefs.

So, in summary, "closure" vs "non-closure", and "named subroutine" vs
"anonymous subroutine" are orthogonal concepts.  Pick one from each
column. :) And it's too bad that we have people mixing the terms up,
because cargo-cult meme virus items spread fast in the Perl extended
community.

-- 
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[EMAIL PROTECTED]> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!

Reply via email to