Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: What are all the false values in Perl ((conditional

by Fastolfe (Vicar)
on Sep 22, 2000 at 19:41 UTC ( [id://33643]=note: print w/replies, xml ) Need Help??


in reply to What are all the false values in Perl ((conditional, boolean, whitespace, unprintable characters)

I believe an empty string, the undefined value, an empty list/array and the number zero (numeric or "0") will return a false value. A non-empty string, an array with 1 or more elements, or a non-zero number will return a true value.

If you're seeing a zero, be sure it's not a string with other characters besides a zero in it. I believe spaces or other characters before or after it may cause the variable to be evaluated as a numeric zero when you're doing math, but since there's more than a number in the string, a boolean test will look at it from a string point of view, and will return 'true' since it's a non-empty string.

If $script::debug is being set to a numeric zero (or, better, undef), there's no reason the test you're describing should return a true value. I'd like to see how you're setting $script::debug.

(Updated 'list' vs. 'array'.)

Replies are listed 'Best First'.
RE (tilly) 2: What are all the false values in Perl ((conditional
by tilly (Archbishop) on Sep 22, 2000 at 22:10 UTC
    Your list statement is incorrect:
    print "hello\n" unless (1, 0);
    Otherwise you are right. The false scalars are:
    undef, '', '0', 0.0;
    Truth is evaluated in void (essentially scalar) context so something that looks like a list (array, hash, etc) is coerced into a scalar before the boolean test.

    EDIT
    My bad for saying void when I meant Boolean.

    But note that when I said "looks like a list" I used that phrase very, very carefully. Many list-like things exist in Perl, but there is no list per se. For an earlier post of mine on this exact topic try Arrays are not lists.

    I would give an array slice example, but tye gave a better one than the one I was going to do... :-)

      That's not a list, that's the binary comma operator. Any non-empty list is true. It may look like a list, but it's not one.

      UPDATE:

      Hmm, "Any non-empty list is true" was a dumb thing to say, as tye and tilly pointed out.

      It reminds me of an MJD quote:

      "If there is a giant purple water buffalo returned from a function, then $h = func() will always give you the length of its nose."

      In other words, any non-empty list is true because you never have a list in boolean context.

      However, I do stand by my first and third sentences in the original post. I think it's misleading to say if (1,0) or even if @array[0,2,1] is a list.

      It is, obviously, a complicated topic.

        I completely agree that the so-called "list" in tilly's example is better referred to as "use of the comma operator". I also (think that I mostly) agree with the statement that "an empty list is false". I can't come up with a (non-contrived) situation where that is not the case. But I think that phrase is on a slippery slope to much confusion.

        The term "list" has no formal definition in Perl lingo. This is a source of much confusion for people ranging from Perl newbie to the Perl illuminati. The problem is made worse by several knowledgeable people talking as if there was a formal meaning (including in the Perl standard documentation where "list" is used to mean contradictory things without noting that "list" on one page may not be talking about the same thing as an occurrance of "list" on another page). This is also made worse because the terms "list context", "scalar context", and "scalar" all have formal meanings in Perl (which makes it easy to infer that "list" would also have a formal meaning).

        I'll also disagree that boolean evaluation provides a void context. It provides a scalar context as proven by:

        sub want { if( ! defined wantarray ) { warn "void context\n"; } elsif( ! wantarray ) { warn "scalar context\n"; } else { warn "array context\n"; } return 1; } if( want ) { # scalar context print want,"\n"; # array context } want; # void context __END__ This prints: scalar context array context 1 void context

        Note that scalar context has several "subcontexts" that wantarray can't detect. These include "scalar" ($x= want), "string" ($x= "".want), "numeric" ($x=  0+want), "integer" ($x= 0|want), and "boolean" ($x= !want). It is a bit of a stretch to call these "contexts" with any current version of Perl because most operators don't know which of these contexts they are in.

        Finally, I have to disagree with the statement that "a non-empty list is true". This is further down the slope of confusion. There are so many things that could be called "a non-empty list". Many of those will yield a true value in a boolean context. But there are many operations that would return a non-empty list in a list context but would return a false value in a boolean context. tilly gives one example. Perhaps this is a better example:

        my @array=(-1,0,1); print "true\n" if @array[0,2,1];
        which doesn't print anything.

                - tye (but my friends call me "Tye")
RE: Re: What are all the false values in Perl ((conditional
by japhy (Canon) on Sep 23, 2000 at 01:09 UTC
    For an in-depth look at the topic of "list context", I point you to my PerlMonth.com article, "List" Is a Four-Letter Word.

    $_="goto+F.print+chop;\n=yhpaj";F1:eval

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2024-04-16 12:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found