use strict; use warnings; # ---------- for each @list my @each; for my $i ( 1..3 ) { push @each, sub { warn "each: \t$i" }; } $_->() for @each; # ---------- for c-style my @cfor; for ( my $i = 1 ; $i < 4; $i++ ) { push @cfor, sub { warn "cfor: \t$i" }; } $_->() for @cfor; # ---------- generator sub gen { my ($x)=@_; return sub { warn "gen: \t$x" }; } my @gen; for ( my $i = 1 ; $i < 4; $i++ ) { push @gen, gen($i); } $_->() for @gen; #### each: 1 at d:/exp/pm_closure.pl line 10. each: 2 at d:/exp/pm_closure.pl line 10. each: 3 at d:/exp/pm_closure.pl line 10. cfor: 4 at d:/exp/pm_closure.pl line 21. cfor: 4 at d:/exp/pm_closure.pl line 21. cfor: 4 at d:/exp/pm_closure.pl line 21. gen: 1 at d:/exp/pm_closure.pl line 31. gen: 2 at d:/exp/pm_closure.pl line 31. gen: 3 at d:/exp/pm_closure.pl line 31.