Linked Questions
20 questions linked to/from How do I use boolean variables in Perl?
13
votes
3
answers
27k
views
Is there a built-in true/false boolean value in Perl? [duplicate]
Possible Duplicate:
How do I use boolean variables in Perl?
[root@ ~]$ perl -e 'if(true){print 1}'
1
[root@ ~]$ perl -e 'if(false){print 1}'
1
I'm astonished both true and false passes the if...
1
vote
2
answers
180
views
What's the meaning of plain string when using as condition? [duplicate]
Below perl one-liner outputs hello to console, so how is false interpreted here since it is not a variable or literal string?
perl -e"if (false) {print 'hello'}"
-5
votes
2
answers
163
views
How to specify multiple boolean comparisons in Perl? [duplicate]
Is it while ($x > 3 || $y < 2) like in other programming languages? I literally can't find anywhere online that tells me how to do this in Perl.
1
vote
1
answer
109
views
strange perl issue with logical operators [duplicate]
Am doing this on mac, perl version is
This is perl 5, version 16, subversion 3 (v5.16.3) built for darwin-thread-multi-2level
$ perl -e "$t=false;while(!($t)){print 1}"
$ perl -e "$t=true;while(!($t)...
1
vote
3
answers
111
views
Learning conditionals in perl [duplicate]
I am learning scripting in perl. I have encountered an example which I don't understand:
my $num = 50;
if ($num) {
print "True: $num\n";
} else {
print "False: $num\n";
}
This is very simple ...
0
votes
0
answers
58
views
print result of comparison operators [duplicate]
#! /usr/bin/perl
$var1 = 20;
$var2 = 15;
print ("var1 = ", $var1, "\n");
print ("var2 = ", $var2, "\n");
# Comparing numbers
print ("var1 == var2 : ", ($var1 == $var2), "\n");
print ("var1 != var2 :...
12
votes
7
answers
975
views
Why does Perl think -1 is true?
This is a piece of common example code:
while (1) {
print "foo\n";
}
which prints 'foo' forever.
perl foo.pl
foo
foo
foo
...
and
while (0) { print "foo\n"; }
dies quietly as you expect:
...
18
votes
5
answers
4k
views
Why does Perl use the empty string to represent the boolean false value?
When evaluating an expression in a scalar (boolean) context, Perl uses the explicit value 1 as a result if the expression evaluates to true and the empty string if the expression evaluates to false.
I'...
6
votes
9
answers
23k
views
What does "if (1)" do?
What does the syntax
if (1) {}
do?
I can't find documentation for this syntax and 1 is not being treated as a boolean. Am I right?.
13
votes
3
answers
9k
views
Perl equivalent of (Python-) list comprehension
I'm looking for ways to express this Python snippet in Perl:
data = {"A": None, "B": "yes", "C": None}
key_list = [k for k in data if data[k]]
# in this case the same as filter(lambda k: data[k], ...
4
votes
4
answers
387
views
Why doesn't 2==4 return false?
I come from C++ background and am trying to learn perl with Beginning Perl. However, this code in the second chapter has left me confused:
#!/usr/bin/perl
use warnings;
print"Is two equal to four? ", ...
1
vote
3
answers
3k
views
What the best practice for Boolean types
I recently had to maintain a legacy project, the code was a mess with no coding pattern however one thing caught my attention, in some cases Boolean types were created in three different ways:
const ...
2
votes
2
answers
151
views
How do i interpret this if statement
if ( $2 && $3 && $3 != 0 )
what is the above logic in Perl? I've never seen an if condition like this in other languages. $2 and $3 are just capturing groups of some regex.
or this:
...
0
votes
1
answer
2k
views
Result of grep as a boolean
I am using the below code. On the right of and is a grep without a numerical comparison. Does that make sense?
if ( scalar( grep { $_ eq $ServerTypeId } keys %ServerTypes ) > 0
and grep { ...
5
votes
1
answer
573
views
Scalar vs List Assignment Operator
Please help me understand the following snippets:
my $count = @array;
my @copy = @array;
my ($first) = @array;
(my $copy = $str) =~ s/\\/\\\\/g;
my ($x) = f() or die;
my $count = () = f();
print($x = $...