in reply to Re: Test if a subhash in a referenced hash exists
in thread Test if a subhash in a referenced hash exists

If $group, $id, $stage etc don't exist in $coordinates, then that will auto-vivify them. You need to check the existence of each level in the hash if magically creating them merely by looking at them is a problem for your application. Something like this (ignoring your application's logic for the sake of making my example code clearer):
if(exists($coordinates->{$group})) { if(exists($coordinates->{$group}->{$id})) { if(exists($coordinates->{$group}->{$id}->{$stage})) { ... } } }
or to generalise (untested. haven't even tried to compile it) ...
my $result = do_stuff_in_hash_without_autovivifying( sub { my $hash_to_work_on = shift; # do stuff here }, $coordinates, # initial hash $group, $id, $stage, 'coords' # list of keys to traverse ); sub do_stuff_in_hash_without_autovivifying { my($sub, $hash, @keys) = @_; if(@keys && exists($hash->{$keys[0]}) { return do_stuff_in_hash_without_autovivifying( $sub, $hash->{$keys[0]}, @keys[1 .. $#keys] ); } else { return $sub->($hash); } }
If you're *really* paranoid about auto-vivification, then you can subvert Tie::Hash::Vivify to turn it into a fatal error instead of silent data corruption:
use Tie::Hash::Vivify; use Data::Dumper; my $hash = Tie::Hash::Vivify->new(sub { die("No auto-vivifying! Bad programmer! No bikkit!\n".Dumper(\@_)) });

Replies are listed 'Best First'.
Re^3: Test if a subhash in a referenced hash exists
by moritz (Cardinal) on May 28, 2010 at 11:22 UTC
    Another way to prevent autovivification is to use Data::Diver.
    Perl 6 - links to (nearly) everything that is Perl 6.
Re^3: Test if a subhash in a referenced hash exists
by Henri (Novice) on May 29, 2010 at 13:21 UTC
    Thanks for pointing out that autovivificated hash parts stay around. Reading about autovivification I always thought that those parts would appear on the fly and be gone again after they have been looked at. Since I actually might test hashes several times, I will be on the outlook for this situation.