sub makecounter { my $count = 0; return sub { ++$count }; } my $counter1 = makecounter; my $counter2 = makecounter; for (1 .. 5) { print $counter1->(), "\n"; } print $counter2->(), "\n"; #### { # create a scope for the closure my $count = 0; sub counter { ++$count } } for (1 .. 5) { print counter, "\n"; }