Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

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

by Athanasius (Archbishop)
on Aug 18, 2015 at 14:57 UTC ( [id://1139023]=note: print w/replies, xml ) Need Help??


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

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:

no strict; use warnings; package foo { use Data::Dump; my %h = (x => new_array); $h{x}[2] = 42; print "foo\n"; dd \%h; dd \@new_array; print "-----------\n"; } package bar { use Data::Dump; print "bar\n"; dd \@{ ::new_array}; dd \@{foo::new_array}; }

Output:

0:50 >perl 1347_SoPW.pl foo { x => "new_array" } [undef, undef, 42] ----------- bar [] [undef, undef, 42] 0:50 >
...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,

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-25 10:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found