You seem unclear between the difference between defined and false. This code should show you the difference.
In perl falseness includes these three miscreants only: 0 "" and undef. All else is true
UPDATE: Chipmunk did not like my evals so I have used 5 different perl idioms for if (condition){print this}else{print that}. Choices, choices. TIMTOWTDI TIMTOWODI. To avoid confusion these idioms all do exactly the same thing - test a condition and print someting if it is true and something different if it is not.
tachyon
my $a; print "my \$a;\n"; print "Undefined\n" unless defined $a; print "False\n" unless $a; $a=0; print "\nmy \$a=0;\n"; if (defined $a) { print "Defined\n"; } else { print "Undefined\n"; } if ($a) { print "True\n"; } else { print "False\n"; } $a=''; print "\nmy \$a='';\n"; print eval'(defined $a) ? "Defined\n" : "Undefined\n"'; print eval'($a) ? "True\n" : "False\n"'; $a=1; print "\nmy \$a=1;\n"; print ((defined $a) ? "Defined\n" : "Undefined\n"); print (($a) ? "True\n" : "False\n"); $a='foo';print "\nmy \$a='foo';\n"; print '',(defined $a) ? "Defined\n" : "Undefined\n"; print '',($a) ? "True\n" : "False\n"; undef $a; print "\nundef \$a;\n"; (defined $a) ? print "Defined\n" : print "Undefined\n"; ($a) ? print "True\n" : print "False\n";
This is about the only printing idiom that does not work! Sadly it is also the most elegant to my eyes.
print (defined $a) ? "Defined\n" : "Undefined\n";
This only works with a null string and comma after the print as shown above print '', (cond)? foo : bar
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Tutorial::What is true and false?
by merlyn (Sage) on May 27, 2001 at 18:32 UTC | |
by japhy (Canon) on May 27, 2001 at 19:47 UTC | |
by tachyon (Chancellor) on May 27, 2001 at 20:42 UTC | |
|
Re: Tutorial::What is true and false?
by chipmunk (Parson) on May 27, 2001 at 21:13 UTC | |
|
Re: Tutorial::What is true and false?
by lachoy (Parson) on May 28, 2001 at 03:07 UTC | |
|
Re: Tutorial::What is true and false?
by arturo (Vicar) on May 28, 2001 at 21:39 UTC | |
by tye (Sage) on May 28, 2001 at 21:55 UTC |