in reply to gotchas with hash element autovivification

raybies:

The gotcha is this: before exists or defined can operate on the hashref, perl has to navigate through the data structure to get to the hash reference, and it creates 'em as needed to get there. For example:

$ cat autovivify.pl #!/usr/bin/perl use strict; use warnings; use Data::Dump qw(dump); my %h = (A=>0); print "Foo" if defined $h{Apple}{Banana}{Seahorse}; print dump(\%h); $ perl autovivify.pl { A => 0, Apple => { Banana => {} } }

As I understand it[1], under the hood, the defined operator wants a scalar to test. So perl has to navigate through the hash to get *to* that scalar. In this case, it had to create a hash reference keyed from Apple, and inside that it needed to create another hash reference keyed from Banana. Now perl has a scalar value (the empty hash reference pointed to by Banana), and passes it to defined who's checking for the definedness of the Seahorse entry.

Notes:

[1] I've never spelunked in the perl source code, but I've studied compilers, and have an inkling of how things might go.

[2] I was surprised that there wasn't a Seahorse entryhash as well. Before testing it, I would've expected to see a Seahorse entry as well. But it's clearly not there!

...roboticus

When your only tool is a hammer, all problems look like your thumb.

Update: Fixed, ++ to tobyink for the catch.

Replies are listed 'Best First'.
Re^2: gotchas with hash element autovivification
by tobyink (Canon) on Apr 03, 2012 at 07:08 UTC

    I was surprised that there wasn't a Seahorse hash as well. Before testing it, I would've expected to see a Seahorse entry as well. But it's clearly not there!

    Of course there's no Seahorse hash. If it created a Seahorse hash, then it would have to print "Foo", and this would make the defined keyword kinda useless.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      tobyink:

      Yes, but that's not what I said. I didn't say a Seahorse hash, I said Seahorse *entry*: Whoops! I certainly did!

      $ cat autovivify.pl #!/usr/bin/perl use strict; use warnings; use Data::Dump qw(dump); my %h = (A=>0); print "Foo" if defined $h{Apple}{Banana}{Seahorse}; $h{Apple}{Banana}{Seahorse}=undef; print "Bar" if defined $h{Apple}{Banana}{Seahorse}; print dump(\%h); $ perl autovivify.pl { A => 0, Apple => { Banana => { Seahorse => undef } } }

      As you can see, if the Seahorse entry exists defined can still return false.

      In my (clearly incorrect) mental model of hashes, the defined function and the perl compiler, defined would be passed the contents of the *value* slot of the Seahorse entry. So I expected that the Seahorse entry would be created so that the compiler could hand defined the undefined value. This is one of perl's irksome (to me) quirks.

      ...roboticus

      When your only tool is a hammer, all problems look like your thumb.

      Update: Repaired, per tobyink's note.

        Autovivification requires an lvalue "context". Just reading a value won't autovivify it. Both defined and exists are known to Perl to be read-only and so won't autovivify the last level of dereferencing.

        Just testing a hash also won't autovivify (the last level). So I disagree with tobyink's "this would make the defined keyword kinda useless" because it hints that defined is somehow preventing autovivification. That's particularly problematic because that is actually a common misconception. Just testing defined or exists has no different behavior with regard to autovivification than directly testing the hash entry does.

        And even when some function "would be passed the contents of the *value* slot of [an] entry", that isn't always enough to cause autovivification. Perl is (sometimes) smart enough to make the autovivification dependent on whether the function actually makes a modification.

        my %hash; sub list { print join( ' ', sort keys %hash ), $/ } for( $hash{for} ) { # Don't do a single thing to $_ } list(); # 'for' entry autovivified unconditionally sub maybeset { for( $_[0] ) { $_ = $_[1] if 1 < @_ }; list() } maybeset( $hash{maybe} ); # no 'maybe', despite the for() in th +e sub maybeset( $hash{set}, 'hello' ); # now 'for set'

        - tye        

        I didn't say a Seahorse hash, I said Seahorse *entry*

        You said both.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'