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(\@_))
});
|