sub newprint { my $x = shift; return sub { my $y = shift; print "$x, $y\n"; }; } $h = newprint("Howdy"); $g = newprint("Greetings"); &$h("world"); &$g("earthlings"); #### sub make_closures { my $j = 0; my @closures; while (++$j < 7) { push (@closures, sub {print $j, "\n";}); } } foreach my $sub (&make_closures) { &$sub; } #### 7 7 7 7 7 7
## sub make_closures { my $j = 0; my @closures; while (++$j < 7) { push (@closures, sub {print $j, "\n";}); } } foreach my $sub (&make_closures) { &$sub; } ##
## 7 7 7 7 7 7