Perhaps you might like to consider adding a portion of this post to the true false tutorial. This was posted in reply to a question on undef/false. At the very least please include undef as a false value not just 0 and ''.

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


In reply to Tutorial::What is true and false? by tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.