in reply to Baby Steps "if" behavior?
Perl values can be defined or undefined and true or false (all undefined values are also false). Elements in a hash can, in addition, exist or not. The following code might go some way to explaining the differences:
#!/usr/bin/perl -w use strict; my %hash = (a => 1, b => 0, c => undef); foreach ('a' .. 'd') { print "\$hash{$_) : "; if (exists $hash{$_}) { if (defined $hash{$_}) { if ($hash{$_}) { print "exists, defined & true\n"; } else { print "exists, defined & false\n"; } } else { print "exists, undefined & false\n" } } else { print "non-existant, undefined & false\n"; } }
|
|---|