develooper Front page | perl.perl5.porters | Postings from August 2003

Re: [perl #10030] DESTROY not called on code reference objects

Thread Previous | Thread Next
From:
Dave Mitchell
Date:
August 20, 2003 10:55
Subject:
Re: [perl #10030] DESTROY not called on code reference objects
Message ID:
20030820092108.GA8082@fdgroup.com
On Tue, Aug 19, 2003 at 11:31:48PM -0400, Kurt Starsinic wrote:
>     I suggest that it's a closure when the language supports
> associating bindings to the subroutine.  In Perl, this is always
> and only the case for anonymous subroutines, whether or not the
> number of private bindings > 0.
> 
>     So it seems to me that, in Perl, a named subroutine is never a
> closure, and an anonymous subroutine is always a closure.

But named subs also capture their lexical state at creation time,
so I would call them closures too:

X.pm:
    package X;
    sub new { my $x = $_[1]; bless \$x }
    sub DESTROY { print "${$_[0]} destroyed\n" }

    my $x1 = X->new('x1');
    my $x2 = X->new('x2');
    sub f { $x2 }
p:
    #!/usr/bin/perl -w

    print "in main\n";
    use lib '.';
    use X;

gizmo [d]$ ./p
x1 destroyed
in main
x2 destroyed
gizmo [d]$ 

Here, f captures $x2 and so stops it being destroyed at the end of
the compilation of X.pm. That is an important behaviour that deserves
a label. 'Closure' seems as good a label as any to me.

Similar comments aply to, eg

    {
	my $c = 0;
	sub inc { $c++ }
	sub dec { $c-- }
    }



-- 
Never do today what you can put off till tomorrow.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About