Numerically is the key word here. Perl attempts to convert strings to numbers via a very specific procedure:
- Start at the left. If the string is null, return 0.
- Move right character by character. If you see stuff that isn't numeric or whitespace, stop and return 0. Throw away whitespace. If you never see a number, return 0.
- When you find the first digit, collect characters as long as they still look like they belong to a number (Note that "0x" is a set of characters that could belong to a number). If you hit the end of the string, evaluate the number you have so far using these rules, and return that.
- If you see more "can't belong to this number" characters before you reach the end of the string (notice that this is contextual; e.g., if we'd gotten "0b", anything other any "1" or "0" can't belong), stop, evaluate whatever you have using these rules, and return that.
So you have two strings, each of which has no numbers in it at all. This means that they both are evaluated as empty strings, or zero, so they are equal.
If you had use warnings on, it would have told you that you were comparing two strings and that you would probably not get the intended results.
This is one of those cases where "what you meant" and "what you said" do not match, and the warning would have let you know that.
As a note, this is why "0 but true" - the "true zero" - is indeed that: a value equal numerically to zero, but when evaluated as part of a logical expression is true. It's zero by the number-collecting logic (start with "0", next character is " ", so end of number, value 0) and not a null string, undef, zero, or quoted zero by the test-for-false logic.
Edit: corrected number-detection logic: non-whitespace leading characters cause an immediate drop out with a zero value; whitespace doesn't count until you hit the number; any character that "can't belong" causes evaluation to stop. Added "0 but true" why-does-that-work explanation.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.