Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^3: Non-existent hash and error "not a hash reference at ..."

by Athanasius (Archbishop)
on Aug 17, 2015 at 15:39 UTC ( [id://1138897]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Non existent Hash and error"not a hash refernece at perl line"
in thread Non existent Hash and error"not a hash refernece at perl line"

Yes, the first half of the assertion (if the key $myhash{'x'} does not exist) is false, as you have shown, because Perl creates the missing key through the process of autovivification.

But the second half of the assertion (or if ... it does not contain a hash reference) is more interesting. With use strict, the assertion is true:

1:33 >perl -Mstrict -wE "my %h = (x => 17); $h{x}{y} = 42;" Can't use string ("17") as a HASH ref while "strict refs" in use at -e + line 1. 1:33 >

But without strictures, the assertion is again false, and the result is (to me) quite surprising:

1:33 >perl -MData::Dump -wE "my %h = (x => 17); $h{x}{y} = 42; dd \%h +; dd \%17;" { x => 17 } { y => 42 } 1:34 >

I don’t think I really understand this behaviour, but one thing is clear: it does provide yet another strong argument (if one were needed) to always use strict !

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^4: Non-existent hash and error "not a hash reference at ..."
by stevieb (Canon) on Aug 17, 2015 at 15:49 UTC

    That's extremely interesting... I've never even contemplated testing something like that before. So correct me if I'm wrong, but the %17 hash completely broke free from the hash's namespace, and was created globally? If so, I can see how things like this can smash remote vars by accident, making things really fun to troubleshoot!

    This looks fun:

    perl -MData::Dump -wE ' my %h = (x => "INC"); undef %{$h{x}}; dd \%h; dd \%INC; '

      Hello stevieb,

      From the thread linked in ++choroba’s post below, it appears that when a hash key is used as a reference, and use strict is not in force, Perl applies “autovivification-on-steroids”: it autovifies a new hash or array variable with the name of the key:

      ...the %17 hash completely broke free from the hash's namespace, and was created globally?

      Yes, but that’s because 17 is a special variable name, reserved for true globals. As LanX notes here, variables with standard names are created in the current package, as shown in the example code above.

      Hope that helps,

      Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re^4: Non-existent hash and error "not a hash reference at ..."
by choroba (Cardinal) on Aug 18, 2015 at 11:43 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-20 03:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found