[Israel.pm] GC for iterator

Pinkhas Nisanov pinkhas at nisanov.com
Tue Aug 23 01:07:31 PDT 2005


I try to work with iterators in perl and
do not really know how garbage collector
will work in this case, example:

    my $itr1 = createIterator( 1, 100000);
    $itr1->() foreach ( 1..20 );

sub createIterator
    my ( $p1, $p2 ) = @_;
    return sub { print "next value: ". ++$p1. "\n" if $p1 < $p2 }

Variables "$p1" and "$p2" stay in memory, so iterator "$itr1"
( reference to function ) remember their values on every
iterator call. My question is, will garbage collector release
variables "$p1" and "$p2" when iterator "$itr1" go out of scope?

Pinkhas Nisanov
pinkhas at nisanov.com
 "The true coder,
 like the true revolutionary,
 is guided by great feelings of love"

More information about the Perl mailing list