A few weeks ago I asked people to predict, without trying it first, what this would print:perl -le 'print(two + two == five ? "true" : "false")'
(If you haven’t seen this yet, I recommend that you guess, and then test your guess, before reading the rest of this article.)
People familiar with Perl guess that it will print
true; that is what I guessed. The reasoning is as follows: Perl is willing to treat the unquoted strings
fiveas strings, as if they had been quoted, and is also happy to use the
==operators on them, converting the strings to numbers in its usual way. If the strings had looked like
"5"Perl would have treated them as 2 and 5, but as they don’t look like decimal numerals, Perl interprets them as zeroes. (Perl wants to issue a warning about this, but the warning is not enabled by default. Since the
fiveare treated as zeroes, the result of the
==comparison are true, and the string
"true"should be selected and printed.
Of course, that’s not what it does.
Stay up-to-date by subscribing to the Comments RSS Feed for this post.