I am not able to understand why an empty slot is created.
The feature is called "autovivification".
When you dereference a non-existing element, such as {LEVEL2}->{LEVEL2_2}, the element (the LEVEL2 hashref entry here) is automagically being created as needed.
#!/usr/bin/perl -w use strict; my $var; if (defined $var->{FOO}->{BAR}) { print "found" } use Data::Dumper; print Dumper $var; __END__ $VAR1 = { 'FOO' => {} };
Here, in order to check if the entry BAR is defined, a hash(ref) with an entry FOO is being created. The value associated with the entry FOO is created as another hash(ref) which could potentially hold the checked-for entry BAR.
You might argue this doesn't make sense when just testing for existence or definedness, but that's the way it's currently implemented. See the threads turned up by the above google search for more in-depth discussion.
In reply to Re: usage of if on nested hash creating empty slot for non existing value
by Anonyrnous Monk
in thread usage of if on nested hash creating empty slot for non existing value
by rakeshrocks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |