tags:

views:

19880

answers:

3

I have tried:

$var = false;
$var = FALSE;
$var = False;

None of these work. I get the error message

Bareword "false" not allowed while "strict subs" is in use.
+36  A: 

In Perl, the following evaluate to false in conditionals:

0
'0'
undef
''  # Empty scalar
()  # Empty list
('')

The rest are true. There are no barewords for true or false.

Alan Haggai Alavi
Is the correct comparison, then, a comparison with zero, as in $var=0 ?
Robert Harvey
No. 1. = is not comparison, it's assignment. 2. Do "if ($var) {...}".
Chris Jester-Young
@Robert Harvey: `perl -le 'my $var = 0; print "False" unless($var);'`
Alan Haggai Alavi
Are you allowed to do the following in Perl?$var = ( $var1 eq $var2 );This is done all the time in C. How does this work in Perl?
BlueWaldo
@BlueWaldo: Yes, that works. $var will (IIRC) be either 0 or 1, in this case.
Chris Jester-Young
@BlueWaldo: you can also use cmp and <=> when comparing and assigning the results of the comparison to a scalar.$var = $var1 cmp $var2;'cmp' and '<=>' (used for numeric comparisons) returns -1, 0, or 1 if left argument is less than, equal to, or greater than the right argument. Its not boolean but sometimes you may want to know if one argument ir equal or less than or greater than the other instead of just equal or not equal.
@kevinadc: That "sometimes" is usually in the comparator of a sort invocation. :-)
Chris Jester-Young
there aren't any barewords but there is the boolean module on cpan which adds them and can make code more readable imo.
xenoterracide
+8  A: 

Perl doesn't have a native boolean type, but you can use comparison of integers or strings in order to get the same behavior. Alan's example is a nice way of doing that using comparison of integers. Here's an example

my $boolean = 0;
if ( $boolean ) {
    print "$boolean evaluates to true\n";
} else {
    print "$boolean evaluates to false\n";
}

One thing that I've done in some of my programs is added the same behavior using a constant:

#!/usr/bin/perl

use strict;
use warnings;

use constant false => 0;
use constant true  => 1;

my $val1 = true;
my $val2 = false;

print $val1, " && ", $val2;
if ( $val1 && $val2 ) {
    print " evaluates to true.\n";
} else {
    print " evaluates to false.\n";
}

print $val1, " || ", $val2;
if ( $val1 || $val2 ) {
    print " evaluates to true.\n";
} else {
    print " evaluates to false.\n";
}

The lines marked in "use constant" define a constant named true that always evaluates to 1, and a constant named false that always evaluates by 0. Because of the way that constants are defined in Perl, the following lines of code fails as well:

true = 0;
true = false;

The error message should say something like "Can't modify constant in scalar assignment."

I saw that in one of the comments you asked about comparing strings. You should know that because Perl combines strings and numeric types in scalar variables, you have different syntax for comparing strings and numbers:

my $var1 = "5.0";
my $var2 = "5";

print "using operator eq\n";
if ( $var1 eq $var2 ) {
    print "$var1 and $var2 are equal!\n";
} else {
    print "$var1 and $var2 are not equal!\n";
}

print "using operator ==\n";
if ( $var1 == $var2 ) {
    print "$var1 and $var2 are equal!\n";
} else {
    print "$var1 and $var2 are not equal!\n";
}

The difference between these operators is a very common source of confusion in Perl.

James Thompson
-1 ... please see http://c-faq.com/cpp/slm.html
Sinan Ünür
`use warnings;` instead of `#! perl -w`
Brad Gilbert
Using constants as a poor mans macros that way is dangerous. These code examples aren't equivalent: `if ($exitstatus) { exit; }` vs `if ($exitstatus == true) { exit; }`, which might not be obvious to a casual observer. (And yes, the last example is poor programming style, but that is beside the point).
Zano
+1  A: 

I recommend use boolean;. You have to install the boolean module from cpan though.

xenoterracide