0

On commandline I wrote:

perl -e '$v=false; if($v==true){print 1;}'

and instead of producing an error it returned "1"!

As I remember in past this returned an error to remind me that perl has no boolean type. anything changed in perl in the meantime? or did I remember incorrectly and this always was valid code?

user11566470
  • 53
  • 1
  • 6

1 Answers1

8

Without strict, the bareword false is interpreted as the string ('false'). Using the numeric == numifies it to 0, and "true" as a number is zero, too.

B::Deparse shows it clearly:

$ perl -MO=Deparse -e '$v=false; if($v==true){print 1;}'
$v = 'false';
if ($v == 'true') {
    print 1;
}

warnings would have warned you 4 times: twice about the unquoted bareword, and twice about numeric comparison of strings.

choroba
  • 231,213
  • 25
  • 204
  • 289