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

Replies are listed 'Best First'.
Re: Tutorial::What is true and false?
by merlyn (Sage) on May 27, 2001 at 18:32 UTC
    print (defined $a) ? "Defined\n" : "Undefined\n";
    is being parsed as:
    (print defined $a) ? "Defined\n" : "Undefined\n";
    You need to get rid of those parens, or add an extra layer to have print consider the entire rest of the line to be its arguments, like:
    print ((defined $a) ? "Defined\n" : "Undefined\n");

    -- Randal L. Schwartz, Perl hacker

      Or, like I said in the other place this was posted, put the parens in a more normal place: around the arguments to defined:
      print defined($a) ? "defined\n" : "undefined\n";


      japhy -- Perl and Regex Hacker

      Yes Randal, I know that idiom is broken -> as stated, you may also note that I already use your suggestion in the 5 different working idioms presented. :-)

      Thanks for that japhy, totally elegant. Either you were obtuse or I was thick last post!

      tachyon

Re: Tutorial::What is true and false?
by chipmunk (Parson) on May 27, 2001 at 21:13 UTC
    To clarify, I do not like your use of eval here because you're using it just for precedence. If Larry had wanted us to use eval for precedence, he wouldn't have given us parentheses.
    $x = 2; $y = 3; $z = 4; $val1 = $x * eval '$y + $z'; $val2 = $x * ($y + $z);
Re: Tutorial::What is true and false?
by lachoy (Parson) on May 28, 2001 at 03:07 UTC

    One of my funniest yapc (99) moments was Dominus relating how he (or an acquaintance) used:

    'cogito ergo sum';

    at the bottom of his module to return a true value instead of the traditional:

    1;

    Fortunately, there is no end to cleverness (even silly cleverness) in the world.

    Chris
    M-x auto-bs-mode

Re: Tutorial::What is true and false?
by arturo (Vicar) on May 28, 2001 at 21:39 UTC
    At the very least please include undef as a false value not just 0 and ''.

    I mentioned this a while back in the CB, as a *tweak*. A quick glance at the node might lead you into thinking undef isn't false, but if you read the node, it explicitly mentions that undef is false. So only the lazy will get caught.

    Pedantic semantics: undef is not a value, it's the condition of lacking a value; so it is, in fact, true that the only false values are 0 and ''. I know it seems needlessly nitpicky, but it behooves us as programmers to keep 'boney was a warrior" , 7.178, and -18 in a conceptual box distinct from the one where we keep undef. Don't be fooled by the fact that there are operators that do stuff with undef into thinking it's a value like all the others.

      I disagree with you on this. undef is a special false value in a Boolean context. The reason I say this is because in a Boolean context, undef doesn't get promoted to the empty string or zero like it does when used most other places.

      So undef isn't a numeric value nor a string value which is why you can't detect undef by comparing it to a string or a number. But there are other values that have these same problems (references being the most obvious). So, yes, we should keep undef in a different conectual box than strings and numbers, as we should references. But in the case of Boolean logic, undef is a distinct value in my book.

      And a discussion of "what is false" should mention it explicitly.

              - tye (but my friends call me "Tye")