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

Perl logical operators

by Sogalei (Initiate)
on Jul 18, 2018 at 06:33 UTC ( [id://1218717]=perlquestion: print w/replies, xml ) Need Help??

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

Question: Logical operators giving wrong results. The results are only correct when $a is true and $b is false. Any advice to fix this?

$a = false; $b = true; print "Value of \$a = $a and value of \$b = $b\n"; $c = ($a and $b); print "Value of \$a and \$b = $c\n"; $c = ($a && $b); print "Value of \$a && \$b = $c\n"; $c = ($a or $b); print "Value of \$a or \$b = $c\n"; $c = ($a || $b); print "Value of \$a || \$b = $c\n"; $a = 0; $c = not($a); print "Value of not(\$a)= $c\n"; ***Result*** Value of $a = false and value of $b = true Value of $a and $b = true Value of $a && $b = true Value of $a or $b = false Value of $a || $b = false Value of not($a)= 1

Replies are listed 'Best First'.
Re: Perl logical operators
by choroba (Cardinal) on Jul 18, 2018 at 06:47 UTC
    true and false are barewords with no special meaning in Perl.

    Moreover, you should use strict: it would have told you

    Bareword "false" not allowed while "strict subs" in use at - line 1. Bareword "true" not allowed while "strict subs" in use at - line 2.

    The common value used for true is 1, for false, you can use 0, "" or undef.

    Also, $a and $b are special variables used in sort, it's better not to use them anywhere else.

    #!/usr/bin/perl use warnings; use strict; my $A = 0; my $B = 1; print "Value of \$A = $A and value of \$B = $B\n"; my $c = ($A and $B); print "Value of \$A and \$B = $c\n"; $c = ($A && $B); print "Value of \$A && \$B = $c\n"; $c = ($A or $B); print "Value of \$A or \$B = $c\n"; $c = ($A || $B); print "Value of \$A || \$B = $c\n"; $A = 0; $c = not($A); print "Value of not(\$A)= $c\n";

    ($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,
Re: Perl logical operators
by tobyink (Canon) on Jul 18, 2018 at 07:23 UTC

    In addition to what choroba said, if you really want to be able to use the keywords true and false in your script, there are a few CPAN modules which will provide them for you, such as boolean. Or you can just define them as constants.

    use constant {qw( true 1 false 0 )};

      When did this form become valid? I used on my dev box (5.28) but on production (5.16) I get a syntax error and need to use

      use constant {true=>1,false=>0}
        Interestingly, it's a difference in Perl parser, not in constant. As usually, help the parser by telling it the { doesn't start a block, but a hash reference:
        use constant +{ qw( true 1 false 0 ) };

        map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

        perldelta for 5.18.0 says:

        use no longer tries to parse its arguments as a statement, making use constant { () }; a syntax error (perl #114222).

        which I believe refers to this.

Re: Perl logical operators
by AnomalousMonk (Archbishop) on Jul 18, 2018 at 13:02 UTC
Re: Perl logical operators
by ikegami (Patriarch) on Jul 18, 2018 at 11:19 UTC

    Always use use strict; use warnings qw( all );!!!

      Idly wondering why this hasn't become the default ...
        Idly wondering why this hasn't become the default ...

        Well, at least there's this: use 5.012; and up enables strict. Backwards compatibility is a big thing with Perl 5, and hence there are often fairly long deprecation cycles for incompatible changes, and if one wants to use the newer features of the new Perl versions, use VERSION; is an easy way to do that.

        See also use and feature.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (6)
As of 2024-04-24 11:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found