Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^2: Strange Hash related bug, keys are created by themselves!

by Anonymous Monk
on Nov 03, 2016 at 19:04 UTC ( [id://1175250]=note: print w/replies, xml ) Need Help??


in reply to Re: Strange Hash related bug, keys are created by themselves!
in thread Strange Hash related bug, keys are created by themselves!

Thanks!
"no autovivification" fixed the issue.
I wonder how autovivification was enabled recently on our servers without my team knowing about it.

  • Comment on Re^2: Strange Hash related bug, keys are created by themselves!

Replies are listed 'Best First'.
Re^3: Strange Hash related bug, keys are created by themselves!
by Laurent_R (Canon) on Nov 03, 2016 at 23:11 UTC
    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.

Re^3: Strange Hash related bug, keys are created by themselves!
by BrowserUk (Patriarch) on Nov 03, 2016 at 20:21 UTC
    I wonder how autovivification was enabled recently on our servers without my team knowing about it.

    You don't enable autovivification, it is and has been an integral part of the language since forever.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I knew I was on the right track :)
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1175250]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2024-04-23 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found