As BrowserUk said, autovivification is very useful most of the time. It is thanks to it that you can create directly a hash item such as:
$time{2016}{November}{03}{sales} = 2500;
even if $time{2016} (and, consequently, also $time{2016}{November}, and so on) does not exit yet.

Disabling autovivification might bring some bugs if your code is creating on the fly nested HoH items.

In some cases, of course, autovivification creates unwanted elements in a nested data structure, most notably when you check for the existence of a deeply nested element as in your example.

I would suggest that it is probably better to leave autovivification enabled and to perform your checks step by step, i.e. to change:

if(!defined $hash_ref->{$block}->{$libName}) { print "This is a test\n"; }
to something like:
if(exists $hash_ref{$block} and !defined $hash_ref{$block}{$libName}) +{ print "This is a test\n"; }
This will prevent $hash_ref->{$block} from springing into existence due to autovivification when this is unwanted, as in the case of an existence test such as the one you're doing.

But you'll keep autovivification enabled when it is useful (i.e. in most cases).

To sum it up, it's not a bug, it's a feature. Although, to tell the truth, there could be a better middle way where autovivification would be enabled only when the nested reference appears in a Lvalue assignment statement. I think that the Camel book says something about it to the effect that it might be fixed one day but that it's not a priority; I was not able to find where, though, in the limited time I was ready to devote to this.


In reply to Re^3: Strange Hash related bug, keys are created by themselves! by Laurent_R
in thread Strange Hash related bug, keys are created by themselves! by mak007

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.