1

My perl script works on one mechanic but fail in another.

I using perl debugger to debug it by add "-d" in command. i.e. "perl -d my_perl.pl". Then I found that all "integer comparison" only works for TRUE result.

e.g. If I enter debug command

"x (1==1)"

it returns '1' (TRUE). But If I enter debug command

"x (1!=1)"

it returns '' (nothing, the result is expected to be 0(FALSE)).

BTW, the string comparison is the same. x "s" eq "s" return '1' and x "s" eq "s" return ''.

I using perl v5.14.2 (getting by perl -v). What's the problem of my perl? How can I fix it?

carl
  • 305
  • 3
  • 13

1 Answers1

6

There's nothing wrong with your Perl; that's just the way Perl works. Comparisons return 1 for true and a special version of the empty string for false. See 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.

bgoldst
  • 34,190
  • 6
  • 38
  • 64