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 );
In reply to Re: Autovivification and hash slices
by thundergnat
in thread Autovivification and hash slices
by norbert.csongradi
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |