Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

A young initiate shuffles into the cold sanctuary and stops, cut by a single beam of winter sunlight. Raising his eyes, and in a voice weary with frustration, he exclaims to no one in particular, "Why, in the name of the everlasting while (1), doth less-than and its brethren, when truth they reveal, proclaim 1, the most singular of pure numbers, but when falsehood they uncover, remain mute rather than lay bare the lie with 0?" An aged abbot, meditating off to one side and hearing the blasphemous question that no Undo could reclaim, lifts his head. With a riveting look at once pitying and disdainful, and in a clear voice unsubdued by his years, he addresses the troubled youth thus: "________________"

Replies are listed 'Best First'.
RE: Falsehoods & empty strings
by clemburg (Curate) on Nov 14, 2000 at 15:22 UTC

    Young one, see there are two of them comparison ways, the way of the erudite and the way of the counters. And it is said in our holy book, on the page with the number of 100, in its third incarnation, that: These operators return 1 for true and "" for false.. And also in this book, on the page with the number of 30, it is explained in the utmost detail that "" is one of the values considered to be false by this what we adore, the others being "0" and 0. And of all those values to choose from, only that what you questioned will prevent the demon inconsistencius from appearing. Since note this little transcript from the apocryphal writings of the heretic clemburg, who has repented and is one of us today, and understand, and be satisfied in your curiosity:

    > perl -le 'print ( undef == 0 ? "yes" : "no" )' yes > perl -le 'print ( undef == "0" ? "yes" : "no" )' yes > perl -le 'print ( undef == "" ? "yes" : "no" )' yes > perl -le 'print ( undef eq 0 ? "yes" : "no" )' no > perl -le 'print ( undef eq "0" ? "yes" : "no" )' no > perl -le 'print ( undef eq "" ? "yes" : "no" )' yes

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

RE: Falsehoods & empty strings
by Dominus (Parson) on Nov 14, 2000 at 22:29 UTC
    I don't know the answer, but I just discovered that in Perl 1, the result of the == operator was 0 for false and 1 for true, instead of ""/1 as it is now. So there must have been a conscious decision to change it somewhere along the way. Why, I do not know. I will post more information is I find out anything else.
    Update: The change occurred in Perl 5.000. But it doesn't even appear to be documented. (If someone does find that it is documented, please let me know.)
    Tye just pointed out that the "" value is special. Normally, "" raises a warning if you convert it to a number. But the "" that results from a comparison does not:

    #!/bin/perl -w $x1 = "" + 0; $x2 = (1 == 0) + 0;
    This generates an "isn't numeric" warning for line 2 but not line 3.

Re: Falsehoods & empty strings
by a (Friar) on Nov 14, 2000 at 10:20 UTC
    Um,

    I think it has something to do w/ free will.

    God (Time Bandits)
    No, wasn't there some LW trouble w/ zero not being false the same way undef is, though, so that:
    print "hey \n" if ( (2 < 2) == 0); print "hee \n" if ( (2 < 2) eq undef ); print "ho \n" unless (2 < 2);
    will all work.

    a

RE: Falsehoods & empty strings
by tame1 (Pilgrim) on Nov 14, 2000 at 22:02 UTC
    Thou fails to understand. Thy angst should be not towards the one true God,
    but to the morons he hath made. 0 shall be one and one shall be zero.

    Besides, God hath a sense of humor. '-)
Re: Falsehoods & empty strings
by Anonymous Monk on Nov 14, 2000 at 10:05 UTC
    Werd.