undef is false by definition in Perl and so is absolutely fine whenever in a boolean context.
Autovivification only happens when required, such as during an assignment (but not for a test). However if a chain of references is required all the intermediate references will autovivify, but not the last item in the chain. Consider:
#!/usr/bin/perl use strict; use warnings; my $x; my $y; if ($y) { } if (! $y) { } if ($y && ! $y) { } if (exists $x->{notdef}) { } if (defined $x->{notdef}) { } if ($x->{notdef}) { } print ref $x, "\n"; print "\$y is undef\n" if ! defined $y; if (exists $x->{notdef}{notdeftoo}) {} print "Now \$x->{notdef} exists\n" if exists $x->{notdef};
Prints:
HASH $y is undef Now $x->{notdef} exists
Note though that $x became a hash ref because it was treated as one even before any autovification took place which makes the dereference ok - $x is no longer undef.
In reply to Re: When DOESN'T "Use of uninitialized value" show up?
by GrandFather
in thread When DOESN'T "Use of uninitialized value" show up?
by Tim McDaniel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |