in reply to Automatic Loop Counter not in perl
*arguably; because prototypes require that non-explicit subs be the first arg, I can't duplicate the for syntax, and of course I can't make a predicate form. Maybe it should be a map replacement instead.use strict; use warnings; sub counted_for (&@) { local our $LOOPCOUNT=-1; my $sub = shift; for (@_) { ++$LOOPCOUNT; $sub->(); } } # Define a modestly complicated structure to loop through my $LoH; my $count = 3; @{$LoH->[$count]}{'a'..'e'} = (); my @y = ('X'..'Z'); counted_for { print "$LOOPCOUNT: $_\n"; counted_for { print " $LOOPCOUNT $_\n" } keys %{$LoH->[$count]}; } @y;
Update: And of course last and next won't work with it.will give you warnings.
|
|---|