3 ms·
Here's my use case for most post conditionals: for my $item ( @items ) { # Weed out the things we don't want to process next unless $item; next u
by kbenson 13y ago
Here's my use case for most post conditionals:
for my $item ( @items ) {
# Weed out the things we don't want to process
next unless $item;
next unless $item->{foo} < 3;
next if $item->{bar} < 2 and $item->{baz} =~ /quux/i;
# Yep, these are good for processing
$item->{othername} //= "$item->{foo} - $item->{baz}";
process( $item );
}
Could that be done using maps and greps, and even be faster? Sure. Would it be harder to reason about and add documentation to? Probably.
Additionally, I use them for parameter verification in subs:
sub foo {
my ($cowbell,%attrs) = @_;
die "Needs moar cowbell!" unless $cowbell;
die "Invalid color!" unless $attrs{color}//'' =~ /\A(bronze|silver)\Z/i;
# blah
}
P.S. That's not to say I don't love a good map and grep combo, I do. After a certain point of craziness I convert to a loop and document a bit. I tried to add enough items to the loop that it would be a cumbersome map and grep.