in reply to evaluation strategy of perl
However, that will not work correctly:for (my $i = 0; $i<3; $i++) { my $str = "$i\n"; push @d, sub { print $str }; }
0 1 2Oops. You meant
Because of this sort of problems, you should use the for(LIST) form.for (my $i = 1; $i<=3; $i++) { my $str = "$i\n"; push @d, sub { print $str }; }
That eliminates an entire class of mistakes known as "fencepost errors", where the loop limit condition is fudged, usually resulting in one-off errors (one iteration too many or too few).for my $i (1..3) { my $str = "$i\n"; push @d, sub { print $str }; }
Makeshifts last the longest.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: evaluation strategy of perl
by blakem (Monsignor) on Sep 20, 2002 at 15:15 UTC | |
by Aristotle (Chancellor) on Sep 20, 2002 at 15:33 UTC |