Are you suggesting postfix if should never be used?
Yes. Well, I have happily never used it for fifteen years. Conway made a convincing argument IMHO.
In case you're interested, I would write your:
return encode_json(\%result) if lc($attrs{'format'}) eq 'json';
as:
lc($attrs{'format'}) eq 'json' and return encode_json(\%result);
... which is similar in style to the classic:
open(my $fh, '<', $file) or die "error opening '$file': $!";
with flow of control on the right. I guess my brain has got used to scanning the right of the screen for flow of control.
Curiously, the instant I glanced at your original code:
return encode_json(\%result) if lc($attrs{'format'}) eq 'json';
return $result{'message'} or "$result{'api-key'}:$result{'session'}";
my spidey sense went off. Ding! Ding! Ding!
This reaction was pure instinct, far too quick to be based on any rational analysis.
Every atom in my body screamed at me that this code is wrong!
It was morning in Sydney Bod, so looking at your code woke me up nicely and got me going for the day.
Thanks for that BTW. :)
|