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'});
but what about this?
my $show_ref = exists $log{'show_ref'} ? delete $log{'show_ref'} : 'no’;
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?
Thanks,
Frank
#!/usr/bin/perl
use 5.022;
use warnings;
my %log = (
filename => 'trap-generic.log',
show_uri => 'yes',
# show_ref => 'yes',
to_db => 'no',
error_type => '',
);
my $show_ref = exists $log{'show_ref'} ? delete $log{'show_ref'} : 'no';
my $log_line;
foreach my $key (sort keys %log) {
my $value = $log{$key};
$log_line .= "\n$key : " . $value;
}
say $log_line;
say '-----';
say "The deleted show_ref = $show_ref";
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/