in reply to Why are these undefs different?

Thanks for the replies! The goal was to replace [...]->[0] with first to make the code a bit more readable but at this point I might as well leave it alone (performance isn't really a concern). Out of curiosity, why does this still work:
no autovivification; my @x = ({"aaa" => 100}); say Dumper([grep { $_->{"aaa"} < 4 } @x]->[0]->{"aaa"}); #$VAR1 = undef;

Replies are listed 'Best First'.
Re^2: Why are these undefs different?
by haukex (Archbishop) on Sep 14, 2023 at 06:16 UTC
    Out of curiosity, why does this still work

    My understanding of autovivification is that by default it doesn't prevent the dereferencing, it prevents the creation of the new refs (= the autovivification). Compare:

    $ perl -MData::Dump -e 'my $x; $x->[0]->{"aaa"}; dd $x' [{}] $ perl -MData::Dump -M-autovivification -e \ 'my $x; $x->[0]->{"aaa"}; dd $x' undef

    So in the second case $x was not autovivified to an arrayref containing a hashref. If you want the pragma to throw errors, you need to tell it:

    $ perl -MData::Dump -M-autovivification=strict,fetch -e \ 'my $x; $x->[0]->{"aaa"}; dd $x' Reference vivification forbidden at -e line 1.