Perl compare strings with "=="

In perl, you should compare two lines with "eq" or "ne", etc.

I'm a little surprised that the following code snippet will print "yes."

$str = "aJohn"; $x = substr($str, 1); if ($x == "John") { print "yes\n"; } 

My perl version is v5.18.4 on Ubuntu.

Is there a case where "==" on two lines produces a different result from "eq"? Thanks.

+5
source share
1 answer

"foo" == "bar" true. "foo" eq "bar" is false.

The reason for this is: == - a numerical comparison. "foo" and "bar" are numerically evaluated to 0 (for example, "17foo" numerically evaluated to 17 ); since 0 == 0 , "foo" == "bar" . This is usually not the operation you are looking for.

+13
source

Source: https://habr.com/ru/post/1260104/


All Articles