http://qs321.pair.com?node_id=500218


in reply to Re: Autovivification sucking the life out of me
in thread Autovivification sucking the life out of me

Whenever a non-existing hash or array element is used in anything but a simple context, it's autovivificated. The *default* is to autovivificate - it's just that the exceptions happen to be the most often used cases, so it's a surprise when perl autovivifies.
#!/usr/bin/perl use strict; use warnings; my %hash; sub X {} my $x; $x = exists $hash{key1}; printf "key1 %sautovivified\n", exists $hash{key1} ? "" : "not "; $x = $hash{key2}; printf "key2 %sautovivified\n", exists $hash{key2} ? "" : "not "; $x = exists $hash{key4}{key3}; printf "key3 %sautovivified\n", exists $hash{key4}{key3} ? "" : "not +"; printf "key4 %sautovivified\n", exists $hash{key4} ? "" : "not "; ($x) = @hash{key5}; printf "key5 %sautovivified\n", exists $hash{key5} ? "" : "not "; ($x) = @hash{"key6", "key7"}; printf "key6 %sautovivified\n", exists $hash{key6} ? "" : "not "; printf "key7 %sautovivified\n", exists $hash{key7} ? "" : "not "; X($hash{key8}); printf "key8 %sautovivified\n", exists $hash{key8} ? "" : "not "; X(@hash{key9}); printf "key9 %sautovivified\n", exists $hash{key9} ? "" : "not "; X(@hash{"keyA","keyB"}); printf "keyA %sautovivified\n", exists $hash{keyA} ? "" : "not "; printf "keyB %sautovivified\n", exists $hash{keyB} ? "" : "not "; __END__ key1 not autovivified key2 not autovivified key3 not autovivified key4 autovivified key5 not autovivified key6 not autovivified key7 not autovivified key8 not autovivified key9 autovivified keyA autovivified keyB autovivified
Perl --((8:>*

Replies are listed 'Best First'.
Re^3: Autovivification sucking the life out of me
by shenme (Priest) on Oct 14, 2005 at 18:04 UTC
    Wow, thanks for the examples. It would seem "simple usage" is not what people would assume it is. I will go file a bug report against PathTools, but it may be instructive to see how easy it is to fall into this trap. Here is the line of code in File::Spec::Win32.pm that caused me all the grief:
    $tmpdir = $_[0]->_tmpdir( @ENV{qw(TMPDIR TEMP TMP)}, 'SYS:/temp', 'C:\system\temp', 'C:/temp', '/tmp', '/' );
    Sure looks innocent, huh? Of course the 'fix' will be to change the references to 'simple' ones,
    $tmpdir = $_[0]->_tmpdir( $ENV{TMPDIR}, $ENV{TEMP}, $ENV{TMP},

    And in answer to Skeeve, yes, I am a "bug-magnet"