Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Why my variable is not 0 ?

by pcouderc (Monk)
on Jan 08, 2016 at 13:52 UTC ( [id://1152301]=perlquestion: print w/replies, xml ) Need Help??

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

Hi venerable monks, I get :
a=(6) uuu=(0) ported=(1) porterb=() porter=() porter is defined
Why "porter" is not 0 ?
my $a = 6; my $porter = ($a & 1) != 0; my $ported = ($a & 2) != 0; my $uuu = ($a & 1); $porterb = ($uuu) != 0; $ppp=(defined($porter)? 'defined':'undefined' ); print STDERR "a=($a) uuu=($uuu) ported=($ported) porterb=($porterb) porter=($porter) porter is $ppp \n" ;
Thank you
PC

Replies are listed 'Best First'.
Re: Why my variable is not 0 ?
by choroba (Cardinal) on Jan 08, 2016 at 14:04 UTC
    As documented in perlop:
    Perl operators that return true or false generally return values that can be safely used as numbers. For example, the relational operators in this section and the equality operators in the next one return 1 for true and a special version of the defined empty string, "", which counts as a zero but is exempt from warnings about improper numeric conversions, just as "0 but true" is.

    Update: Here's the difference:

    use Devel::Peek; my $f = (0 != 0); my $e = q(); Dump($f); Dump($e);
    SV = PVNV(0x60003a5a0) at 0x600077cc8 REFCNT = 1 FLAGS = (IOK,NOK,POK,pIOK,pNOK,pPOK) IV = 0 NV = 0 PV = 0x6000b60c0 ""\0 CUR = 0 LEN = 10 SV = PV(0x60003c250) at 0x600077d70 REFCNT = 1 FLAGS = (POK,IsCOW,pPOK) PV = 0x60009a810 ""\0 CUR = 0 LEN = 10 COW_REFCNT = 1
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Thank you all.
      It is more clear to me.
      My conclusion is that false is not always 0...
      But it is not a problem to take care of.

        Hello pcouderc,

        My conclusion is that false is not always 0...

        No, indeed. The definition of false in Perl is given in perlsyn#Truth-and-Falsehood:

        The number 0, the strings '0' and "", the empty list (), and undef are all false in a boolean context. All other values are true. Negation of a true value by ! or not returns a special false value. When evaluated as a string it is treated as "", but as a number, it is treated as 0. Most Perl operators that return true or false behave this way.

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: Why my variable is not 0 ?
by toolic (Bishop) on Jan 08, 2016 at 14:04 UTC
    Why "porter" is not 0 ?
    Why do you expect it to be 0? This is one way to get what you want:
    use warnings; use strict; my $a = 6; my $porter = (($a & 1) != 0) ? 1 : 0; print "porter=$porter=\n"; __END__ porter=0=
    Truth and Falsehood
Re: Why my variable is not 0 ?
by LanX (Saint) on Jan 08, 2016 at 14:04 UTC
    different values - including 0 - are considered false in Perl but not the other way round.

    DB<9> use Data::Dump "dd" DB<10> dd { true => !0, false => !1 } { false => "", true => 1 }
    If you want Perl's internal value for false˛ to be mapped to 0 you have to do a $porter ||= 0

    Cheers Rolf
    (addicted to the Perl Programming Language and ☆☆☆☆ :)
    Je suis Charlie!

    ˛) NB: magical value! It is more than just an empty string to evaluate correctly in different contexts.
Re: Why my variable is not 0 ?
by Tommy (Chaplain) on Jan 08, 2016 at 23:20 UTC

    This doesn't answer your question, but is rather an attempt to help you do what it looks like you're after. I think what you are looking for is:

    $ppp = defined $porter ? 'defined ' : 'undefined';

    If you have perl 5.10 or better:

    use 5.10; $ppp = $porter; $ppp //= 0;
    Tommy
    A mistake can be valuable or costly, depending on how faithfully you pursue correction

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1152301]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-26 00:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found