in reply to Re: Test if a subhash in a referenced hash exists
in thread Test if a subhash in a referenced hash exists
or to generalise (untested. haven't even tried to compile it) ...if(exists($coordinates->{$group})) { if(exists($coordinates->{$group}->{$id})) { if(exists($coordinates->{$group}->{$id}->{$stage})) { ... } } }
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: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); } }
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 | |
|
Re^3: Test if a subhash in a referenced hash exists
by Henri (Novice) on May 29, 2010 at 13:21 UTC |