in reply to Closures and lexical scope

That's a lot of extra lexicals that aren't being used in the closure. Does it carry them around?

That's easy to test:

#!/usr/bin/perl use strict; use warnings; sub DESTROY { print "DESTROY called!\n"; } sub make_accessor { my $self = shift; my ($name, $index) = (bless ([] => 'main'), 1); no strict 'refs'; *{ref($self) . "::$name"} = sub { my $self = shift; return $self->[$index] unless @_; $self->[$index] = $_[0]; $self; }; 1; } my $foo = make_accessor; print "End of program\n"; __END__ DESTROY called! End of program

So the answer is "no".

Abigail

Replies are listed 'Best First'.
Re2: Closures and lexical scope
by dragonchild (Archbishop) on Aug 27, 2003 at 17:32 UTC
    I understand now. By adding $name = $name; to the closure, the DESTROY() call is done after the "End of program" line is printed.

    Thank you! :-)

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.