Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl comparison operator output

Tags:

perl

I am not exactly sure what the output of a comparison is. For instance, consider

$rr = 1>2;
$qq = 2>1;

print $rr; #nothing printed
print $qq; #1 printed

Is $rr the empty string? Is this behavior documented somewhere? Or how can one tell for sure?

I was looking for the answer in Learning Perl by Schwartz et al., but could not immediately resolve the answer.

like image 220
bilodeau1 Avatar asked Sep 10 '17 22:09

bilodeau1


1 Answers

http://perldoc.perl.org/perlop.html#Relational-Operators:

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.

So it what is returned is something that is an empty string in string context, and 0 in numeric context.

like image 86
ysth Avatar answered Nov 15 '22 03:11

ysth