in reply to understanding next

next/last and the comma can be very useful when used with a flag.

You want to loop over the whole array and then test if certain conditions were met (and how often).

my @l_test = (1,2,3,4,5,6,7); my $found; foreach my $l_rec (@l_test) { $found++, next if $l_rec == 4; print $l_rec; } print qq{\nskipped 4\n} if $found;
You need to detect if a loop exited early.
my @l_test = (1,2,3,4,5,6,7); my ($found, $bad); foreach my $l_rec (@l_test) { $found++, next if $l_rec == 8; $bad++, last if $l_rec > 10; print $l_rec; } die qq{8 found\n} if $found; die qq{high number found\n} if $bad;

Replies are listed 'Best First'.
Re^2: understanding next
by ruzam (Curate) on Jan 31, 2008 at 13:53 UTC
    That's good stuff. I can think of all kinds of places where I can eliminate an "if () {$x++;next}" block and slip in a "$x++, next if" line :)