2 ms·
Personally i find all of these forms weird. In a one-liner you wouldn't care, and in a full program the inputs would be validated earlier and separately with us
by Mithaldu 13y ago
Personally i find all of these forms weird. In a one-liner you wouldn't care, and in a full program the inputs would be validated earlier and separately with useful error messages.
- kbenson 13y agoHere'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.