in reply to Perl autovivifyies object property when using exists

> how I work around this?

autovivification

perl -MData::Dump -E 'no autovivification; my $fs4 = {_structure => {}}; exists $fs4->{_structure}{BLAH}{TEST}{Crap}; dd $fs4->{_structure};' {}

map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Replies are listed 'Best First'.
Re^2: Perl autovivifyies object property when using exists
by Danny (Chaplain) on Apr 03, 2024 at 19:56 UTC
    Nice. I figured there was some pragma-like way to change this behavior, but never heard of "no autovivification".
      It's worth noting that the default behavior of no autovivification doesn't disable it in case of assignments aka store

      Which is great because that's DWIM for most. But surprising given the name of the pragma...

      DB<5> no autovivification; $h->{a}{b}{c} = 42 DB<6> x $h 0 HASH(0xb400007e0b2fd8b8) 'a' => HASH(0xb400007e0b2ea7f8) 'b' => HASH(0xb400007e0b2eac30) 'c' => 42 DB<7>

      From the autovivification docs

      Possible unimports:

      • no autovivification qw<fetch store exists delete>;
      But store is not default
      • no autovivification; # defaults to qw<fetch exists delete>

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      see Wikisyntax for the Monastery

        Assignment "store" was the first thing I tested when I installed it yesterday :)