Pug,
What you are using is a one-use closure. Because it is in a naked block it can not be used multiple times. And as
broquaint points out, it must be "seen" prior to being used. Here is an example of a re-usable closure that can be used before or after the sub has been "seen".
#!/usr/bin/perl
use strict;
use warnings;
my $count_1 = iterator( 1 );
print $count_1->(), "\n" for 1 .. 10;
my $count_2 = iterator( 'a' );
print $count_2->(), "\n" for 1 .. 10;
sub iterator {
my $iterator = shift;
return sub { $iterator++ };
}
Cheers -
L~R