Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: What is the correct definition of False and Null in Perl? (OT: Formatting of OP)

by ww (Archbishop)
on Oct 07, 2011 at 23:52 UTC ( [id://930266]=note: print w/replies, xml ) Need Help??


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

It would be nice if the code your next post is self-consistent and reasonably indented. Did someone suggest that you get bonus points for using fewer lines and less white space? If so, whack that someone for giving you a bum steer... and make your next post readily readable. It'll help you get answers.

Also, consider your output: labeling make it easier to read and understand. Your tabs in the lines with which you disagreed was a big step in the right direction but consider how labeling makes the way you wrote your $var...s easier to know, from the output alone.

Something like this may serve as an example:

#!/usr/bin/perl use strict; use warnings; # 930247 my $var = 0; if ( $var ) { print "\$var ($var) True\n"; } else { print "\$var ($var) False\n"; } undef $var; if ( $var ) { print "\$var (undef) True\n"; } else { print "\$var (undef) False\n"; } my $var1 = ''; if ( $var1 ) { print "\$var1 ('') True\n"; } else { print "\$var1 ('') False\n"; } my $var2 = ""; if ( $var2 ) { print "\$var2 (\"\") True\n"; } else { print "\$var2 (\"\") False\n"; } my $var3 = '0'; if ( $var3 ) { print "\t \$var3 ($var3 - as a single-quoted number) True\n"; } else { print "\t \$var3 ($var3 - as a single-quoted number) False\n"; } my $var4 = "0"; if ( $var4 ) { print "\t \$var4 ($var4 - as a double-quoted number) True\n"; } else { print "\t \$var4\ ($var4 - as a double-quoted number) False\n"; } if ( $var4 == 0 ) { print "\t \$var4 ($var4 - as an unquoted number) True\n"; } else { print "\t \$var4 ($var4 - as an unquoted number) False\n"; } if ( $var4 eq '0' ) { print "\t \$var4 ($var4 - as a single-quoted number) True\n"; } else { print "\t \$var4 ($var4 - as a single-quoted number) False\n"; } print "\t \$var4 ($var4 - as a single-quoted number) has length of " +. length($var4) . " and ASCII value of " .ord($var4). "\n"; my $var5 = '1'; if ( $var5 ) { print "\$var5 ($var5 in single quotes) True\n"; } else { print "\$var5 ($var5 in single quotes) False\n"; } my $var6 = "1"; if ( $var6 ) { print "\$var6 ($var6 in double quotes) True\n"; } else { print "\$var6 ($var6 in double quotes) False\n"; } my $var7 = 1; if ( $var7 ) { print "\$var7 ($var7 - as an unquoted number) True\n"; } else { print "$var7 ($var7 - as an unquoted number) False\n"; }

and that produces this output:

$var (0) False $var (undef) False $var1 ('') False $var2 ("") False $var3 (0 - as a single-quoted number) False $var4 (0 - as a double-quoted number) False $var4 (0 - as an unquoted number) True $var4 (0 - as a single-quoted number) True $var4 (0 - as a single-quoted number) has length of 1 and AS +CII value of 48 $var5 (1 in single quotes) True $var6 (1 in double quotes) True $var7 (1 - as an unquoted number) True

You'll also note an (imperfect) effort to do the quoting for output in the same manner each time; that kind of consistency will also help make your code more understandable... and maintainable.

But most inportant -- make sure you grok davidos reply; hold it close until it's second nature. That will save more important problems, later.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://930266]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (6)
As of 2024-04-20 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found