Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Hash Autovivification Weirdness

by Ido (Hermit)
on Apr 20, 2006 at 22:16 UTC ( [id://544722]=perlquestion: print w/replies, xml ) Need Help??

Ido has asked for the wisdom of the Perl Monks concerning the following question:

Here are a few code examples that work as expected:
1.
$hash{'a'}; print exists $hash{'a'}?'yes':'no'; #no
2.
$hash{'a'}=1; print exists $hash{'a'}?'yes':'no'; #yes
3.
$hash{'a'}=$hash{'a'}; print exists $hash{'a'}?'yes':'no'; #yes
4.
sub foo{$_[0]} foo($hash{'a'}); print exists $hash{'a'}?'yes':'no'; #no
5.
sub foo{$_[0]=1} foo($hash{'a'}); print exists $hash{'a'}?'yes':'no'; #yes
6.
sub foo{$_[0]=$_[0]} foo($hash{'a'}); print exists $hash{'a'}?'yes':'no'; #yes
7.
sub foo{ for($_[0]){ } } foo($hash{'a'}); print exists $hash{'a'}?'yes':'no'; #no
8.
sub foo{ for($_[0]){ $_=1; } } foo($hash{'a'}); print exists $hash{'a'}?'yes':'no'; #yes
Now, this one is, well, it's not consistent with 7..
for($hash{'a'}){ } print exists $hash{'a'}?'yes':'no'; #yes
This one doesn't make sense to me, does it make any to you?
sub foo{ for($_[0]){ $_=$_; } } foo($hash{'a'}); print exists $hash{'a'}?'yes':'no'; #no

Replies are listed 'Best First'.
Re: Hash Autovivification Weirdness
by Fletch (Bishop) on Apr 20, 2006 at 23:41 UTC

    Yeah, that's really weird because your last example and the one numbered 8 produce practically identical output from B::Concise.

    --- /tmp/zsh6abfoh 2006-04-20 19:38:44.000000000 -0400 +++ /tmp/zshlFtUff 2006-04-20 19:38:44.000000000 -0400 @@ -1,6 +1,6 @@ j <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 -2 <;> nextstate(main 4 selfassign:6) v ->3 +2 <;> nextstate(main 4 1assign:6) v ->3 9 <1> entersub[t3] vKS/TARG,1 ->a - <1> ex-list K ->9 3 <0> pushmark s ->4 @@ -10,7 +10,7 @@ 6 <$> const[PV "a"] s ->7 - <1> ex-rv2cv sK/1 ->- 8 <#> gv[*foo] s ->9 -a <;> nextstate(main 4 selfassign:7) v ->b +a <;> nextstate(main 4 1assign:7) v ->b i <@> print vK ->j b <0> pushmark s ->c - <1> null lK/1 ->i $ diff {1,self}assign 3c3 < $_=1; --- > $_=$_;

    The only difference appears to be the filename. Really weird.

Re: Hash Autovivification Weirdness
by Dietz (Curate) on Apr 23, 2006 at 16:15 UTC

    I would have been surprised if your last example printed 'yes', but to be honest I wouldn't have known for sure without trying.
    Technically, it's the same as number 7, besides the unnecessary setting of the (uninitialized) value of $_ to itself.
    Your last example and number 7 do not try to set the key within the subroutine.
    Both examples are working on the alias @_ provides and not directly on the hash key as in your next to last example.

    Maybe you've been bitten by the similarity number 6 provides, where setting the key (through the alias $_[0]) to the undef value $_[0] provides will vivify the key.
    Whereas in your last example trying to reset the value of $_ to $_ will not vivify the key because you are not using the key by trying to initialize the key value with a defined value even if the value is undef.
    Setting $_ to $_ ($_ = $_;) will not change the value, hence there will be no update which could vivify the key.
    Changing the value of $_ (e.g. $_ = undef; or $_ = 1;) will cause an update which will vivify the key.

    The interesting part in this issue is that $_ = $_; looks like it will be handled like a no-op without updating the internal stack or whatever you may call it what is going on behind the scene.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-19 22:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found