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(\@_))
});
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.