From the "Perl Cookbook" chapter about hashes by Christiansen and Torkington:
Quote:
... problems caused by confusing existence, definedness, and truth can multiply like rabbits.
|
They give the following example:
Code:
#!/usr/bin/perl
use warnings;
use strict;
my %age = ();
my $thing;
$age{'Toddler'} = 3;
$age{'Unborn'} = 0;
$age{'Phantasm'} = undef;
foreach $thing('Toddler', 'Unborn', 'Phantasm', 'Relic') {
print "$thing: ";
print "Exists " if exists $age{$thing};
print "Defined " if defined $age{$thing};
print "True " if $age{$thing};
print "\n";
}
The output:
Code:
Toddler: Exists Defined True
Unborn: Exists Defined
Phantasm: Exists
Relic:
A summary of their explanation:
$age{'Unborn'} fails the truth test because the number 0 is one of Perl's false values.
$age{'Phantasm'} only exists because it has been given a value in the hash. Because that value is 'undef' it does not pass the test for definedness.
'undef' is also a Perl false value.