Dear Monks,

I recently was surprised that a ascii '0' was interpreted by an if statement as 'FALSE'. I have thought about this alot, and wonder if this really the way it should be.

The following script shows what I'm talking about.

use strict; my $var = 0; if ( $var ) { print "$var True\n"; } else { print "$var + False\n"; } undef $var; if ( $var ) { print "undef True\n"; } else { print "und +ef False\n"; } my $var1 = ''; if ( $var1 ) { print "'$var1' True\n"; } else { print +"'$var1' False\n"; } my $var2 = ""; if ( $var2 ) { print "\"$var2\" True\n"; } else { prin +t "\"$var2\" False\n"; } my $var3 = '0'; if ( $var3 ) { print "\t'$var3' True\n"; } els +e { print "\t'$var3' False\n"; } my $var4 = "0"; if ( $var4 ) { print "\t\"$var4\" True\n"; } e +lse { print "\t\"$var4\" False\n"; } if ( $var4 == 0 ) { print "\t\"\$var4\" True\n"; } else { prin +t "\t\$var4\" False\n"; } if ( $var4 eq '0' ) { print "\t\"\$var4\" True\n"; } else { pr +int "\t\"\$var4\" False\n"; } print "\t","\"\$var4\" has length of ",length($var4)," and ASC +II value of ",ord($var4),"\n"; my $var5 = '1'; if ( $var5 ) { print "'$var5' True\n"; } else { print +"'$var5' False\n"; } my $var6 = "1"; if ( $var6 ) { print "\"$var6\" True\n"; } else { prin +t "\"$var6\" False\n"; } my $var7 = 1; if ( $var7 ) { print "\"$var7\" True\n"; } else { prin +t "\"$var7\" False\n"; }
The script produces:
0 False undef False '' False "" False '0' False '0' False "$var4" True "$var4" True "$var4" has length of 1 and ASCII value of 48 '1' True "1" True "1" True

The first 4 and the last 3 lines I agree with, but the lines in the middle I have a problem with.

I checked several of my perl books and it seems:

But then in "Programming Perl" Glossary is states 'false In Perl, any value that would look like "" or "0"...' but this disagrees with the definition on page 7.

Am I missing something or has it just been too long of a perl week?

Thank you

"Well done is better than well said." - Benjamin Franklin


In reply to What is the correct definition of False and Null in Perl? by flexvault

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.