in reply to Autovivification and hash slices
The problem is that the parameters being passed to the user subroutine are being evaluated as they are passed, so the autovivify has already been done before the subroutine receives them. Apparently, (some wild-assed guess here) the built in join has some magic to check for existence before trying to evaluate.
If you pass a hash reference and key value separately to your myjoin, you have a chance to check for existence before the key can be autovivified. It isn't as slick as the builtin, but it's not too bad.
use Data::Dumper; sub mydump($) { print Data::Dumper->Dump( [ $_[0] ], ['*hash'] ); } sub myjoin { return unless exists($_[1]->{$_[2]}); return join $_[0], $_[1]->{$_[2]}; } my %hash; my $s1 = join( ':', @hash{qw(key)} ); mydump( \%hash ); my $s2 = myjoin( ':', \%hash, 'key' ); mydump( \%hash );
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Autovivification and hash slices
by norbert.csongradi (Beadle) on Aug 17, 2011 at 13:55 UTC | |
by thundergnat (Deacon) on Aug 17, 2011 at 14:20 UTC |