Dave Mitchell <davem@fdgroup.com> writes: >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, Things get confusing: sub harry { my $foo = shift; sub fred { print "$foo\n" } } harry('Ouch'); harry('Weird'); fred(); >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-- } > }Thread Previous | Thread Next