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

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

Thread Previous | Thread Next
Dave Mitchell
August 20, 2003 10:55
Re: [perl #10030] DESTROY not called on code reference objects
Message ID:
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:
    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 }
    #!/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 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 Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About