in reply to Re^2: Erroneous defined detection of array elements
in thread Erroneous defined detection of array elements

I should have been more clear - defined does not distinguish between "never existed" and "is undef" for hash and array elements. I therefore consider exists a better test for unexpected auto-vivification.
  • Comment on Re^3: Erroneous defined detection of array elements

Replies are listed 'Best First'.
Re^4: Erroneous defined detection of array elements
by ikegami (Patriarch) on Nov 23, 2009 at 20:44 UTC

    defined does not distinguish between "never existed" and "is undef" for hash and array elements.

    Neither does exist. It checks if the element currently exists, not if the element has ever existed.

    Either way, so what?

    I therefore consider exists a better test for unexpected auto-vivification.

    That makes no sense. It doesn't prevent autovivification. It doesn't tell you whether something was autovivified or not. It doesn't help at all.

    $ perl -le'{ my @a; $a[15][0]; print "After autovivification: ", exists($a[14]) || 0, exists($a[15]) || 0, defined($a[14]) || 0, defined($a[15]) || 0; }{ my @a; $a[15] = []; print "After explicit vivification: ", exists($a[14]) || 0, exists($a[15]) || 0, defined($a[14]) || 0, defined($a[15]) || 0; }' After autovivification: 0101 After explicit vivification: 0101