Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re^2: What is the correct definition of False and Null in Perl?

by Marshall (Canon)
on Oct 08, 2011 at 15:42 UTC ( [id://930365]=note: print w/replies, xml ) Need Help??


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

There are some special case true/false situations!

(1) In Perl, the string: "0 but true" is an exception. This string will evaluate to a numeric 0, but a logical true! The logical "true" is not surprising, but the numeric 0 is! That is because this is a special case coded into Perl. Even under use strict; use warnings;, "0 but true" can be used in a numeric expression/computation.

#!/usr/bin/perl -w use strict; my $x = "0 but true"; print $x + 1; #prints 1!!! my $xx = "1 but true"; # print $xx +1; #Ooops ERROR! #non-numeric addition!
(2) There is a more "modern way" of returning the "0 but true" value. That is the string "0E0". The DBI will return this to mean: "hey, I worked!, but I modified zero rows". 10**0 is one. So 0*10**0=0*1=0. So this enables the conveyance of (a)I worked (no error) and (b)result is zero within a single string value "0E0".

I have not seen recent code with the case (1) above. But I have verified that it does indeed work. You don't have to get too far into the DBI before you encounter (2) like when you are updating a table - the table update was just fine "true", but it just didn't do anything (rows modified = 0).

Replies are listed 'Best First'.
Re^3: What is the correct definition of False and Null in Perl?
by LanX (Saint) on Oct 08, 2011 at 17:12 UTC
    > That is because this is a special case coded into Perl.

    every string starting with a number (plus leading whitespaces) evaluates to that number.

    The only specialty about "0 but true" are the missing messages under "warnings".

    DB<102> $a=" 41 The ultimate answer!" DB<103> print ++$a 42

    I would rather prefer "0.0" or "0E0", if I was calling functions in scalar context.

    Cheers Rolf

      The only specialty about "0 but true" are the missing messages under "warnings".

      Yes. That is correct. "0 but true" is a special case that does not emit a warning when used in a numeric expression.

      0E0 is the "standard way" of "true but zero".

      0.0 is not quite the same as 0E0. The DBI uses 0E0. Ok, maybe 0.0 could have been used, but it wasn't. I think that it is pointless to debate 0E0 vs 0.0, 0E0 has won and so let's go to another question.

        > 0.0 is not quite the same as 0E0.

        I was talking about strings not literal numbers:

        DB<105> p !! "0.0" 1 DB<106> p !! "0E0" 1 DB<107> p !! 0E0 DB<108> p !! 0.0 DB<109> p 1 + "0E0" 1 DB<110> p 1 + "0.0" 1

        and by the way IMHO thats all hardly necessary.

        Can't think of a use case which can't be solved by calling a function in list context.

        Cheers Rolf

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2024-03-19 11:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found