On 02/15/2017 01:39 PM, SSC_perl wrote:
I’ve read where writing a one-liner like this is frowned upon:
my $show_ref = delete $log{'show_ref'} if (exists $log{'show_ref'});
there is no need to check for existence when doing delete. it will
return undef if it doesn't exist.
and also the big issue with that code is the conditionally modified
assignment with a declaration. the infamous worst example was my $x = 1
if 0;. that would create a static variable in scope. that is very nasty
and supplanted by the state declaration.
but what about this?
my $show_ref = exists $log{'show_ref'} ? delete $log{'show_ref'} : 'no’;
that is poor code as it does a side effect inside ?:. the general rule
is for ?: to only return a value based on the boolean test and not to do
side effects.
They both seem to work without a problem in my tests.
I prefer the 2nd one anyway as you can assign a default value if that
hash key doesn’t exist. Is this proper Perl or is it a problem waiting to
happen?
try the even simpler and cleaner use of ||
my $show_ref = delete $log{'show_ref'} || 'no’;
uri
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/