Summary
- Boolean tests
- Incrementing or decrementing an undefined value
- Appending to an undefined value
- Autovivification
- Other mutators
Boolean tests
According to the perlsyn documentation,
The number 0
, the strings 0
and
, the empty list ()
, and undef
are all false in a boolean context. All other values are true.
Because the undefined value is false, the following program
#! /usr/bin/perl
use warnings;
my $var;
print "A
" if $var;
$var && print "B
";
$var and print "C
";
print "D
" if !$var;
print "E
" if not $var;
$var or print "F
";
$var || print "G
";
outputs D
through G
with no warnings.
Incrementing or decrementing an undefined value
There s no need to explicitly initialize a scalar to zero if your code will increment or decrement it at least once:
#! /usr/bin/perl
use warnings;
my $i;
++$i while "aaba" =~ /a/g;
print $i, "
";
The code above outputs 3
with no warnings.
Appending to an undefined value
Similar to the implicit zero, there s no need to explicitly initialize scalars to the empty string if you ll append to it at least once:
#! /usr/bin/perl
use warnings;
use strict;
my $str;
for (<*>) {
$str .= substr $_, 0, 1;
}
print $str, "
";
Autovivification
One example is "autovivification." From the Wikipedia article:
Autovivification is a distinguishing feature of the Perl programming language involving the dynamic creation of data structures. Autovivification is the automatic creation of a variable reference when an undefined value is dereferenced. In other words, Perl autovivification allows a programmer to refer to a structured variable, and arbitrary sub-elements of that structured variable, without expressly declaring the existence of the variable and its complete structure beforehand.
For example:
#! /usr/bin/perl
use warnings;
my %foo;
++$foo{bar}{baz}{quux};
use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper \%foo;
Even though we don t explicitly initialize the intermediate keys, Perl takes care of the scaffolding:
$VAR1 = {
bar => {
baz => {
quux => 1
}
}
};
Without autovivification, the code would require more boilerplate:
my %foo;
$foo{bar} = {};
$foo{bar}{baz} = {};
++$foo{bar}{baz}{quux}; # finally!
Don t confuse autovivification with the undefined values it can produce. For example with
#! /usr/bin/perl
use warnings;
my %foo;
print $foo{bar}{baz}{quux}, "
";
use Data::Dumper;
$Data::Dumper::Indent = 1;
print Dumper \%foo;
we get
Use of uninitialized value in print at ./prog.pl line 6.
$VAR1 = {
bar => {
baz => {}
}
};
Notice that the intermediate keys autovivified.
Other examples of autovivification:
Sadly, Perl does not (yet!) autovivify the following:
my $code;
$code->("Do what I need please!");
Other mutators
In an answer to a similar question, ysth reports
Certain operators deliberately omit the "uninitialized" warning for your convenience because they are commonly used in situations where a 0 or "" default value for the left or only operand makes sense.
These are: ++
and --
(either pre- or post-), +=
, -=
, .=
, |=
, ^=
, &&=
, ||=
.
Being "defined-or," //=
happily mutates an undefined value without warning.