while (my ($key, $value) = each %ids) {
for my $item (@names) {
next if $item !~ /$key/;
push @groups, $item;
last;
}
}
which avoids a level of nesting and maybe provides a stronger hint that the task of the current loop is done.
I this case saving a level of nesting by using an "early exit" doesn't make much difference. But this technique scales very well so when the code that would be inside the if statement becomes larger and includes its own nested code program flow is much easier to see.
The same sort of technique can be used in subs by using return to exit early (hence the name). Used well and combined with dealing with simple cases first program flow is generally much easier to see and understand.
Perl is the programming world's equivalent of English
|