Perl does not "think it is a number". In perl, one can use any value (any scalar value) in a numeric context, a string context, or a boolean context. Perl has very strict, and fairly reasonable rules for how to interpret values in different contexts. For numeric contexts:
-If a number is used in a numeric context, it is interpreted as itself (duh)
-If a string is used in a numeric context, and any prefix of that string can be read as a decimal number, then it is interpreted as having that numeric value (e.g., "12.5 dogs" + "7 cats" == 19.5). Note that there actually is a difference between a number and a character string containing a number (e.g., 1000000 and "1000000" have different internal representations), but they behave in the same manner (aside from possible differences in memory use and runtime)
-any non-numeric, or non-string, or string without a leading number is zero (e.g., "hello" + "goodbye" == 0)
Likewise, there are rules for how values are interpreted in a boolean context:
-a number 0, the character string "0", the empty string "", and undef (a special value akin to null, nil, undefined in other languages) are all false
-anything else is true
Now, if you combine these two rules, then you find that, even though the number 0 is false, there are values (infinitely many values, in fact) which will be equal to zero if used in a numeric context, but which will evaluate to true in a boolean context. There are many occasions when this can be a useful. For example, if there is a function which should return a number of records processed but which should also report success or failure, then how would one report that zero records were successfully processed? "0 but true" means that function call was successful and processed zero records, which is distinct from returning 0 (or more likely undef), indicating that the function call failed for some reason.
(Note that perl also has a rich exception handling system. However, there are situations when a return-value indicating success or failure is more appropriate than throwing an exception, or at least that the author considers it more appropriate. I generally tend towards the "you're better off throwing an exception" camp... but perl is a language and a culture that frowns on holding a gun to your head.)
So, in short... no, perl does not 'think that "0 but true" is a number.' It is willing to treat anything as a number. And it is willing to evaluate anything as a boolean. The value "0 but true" is numerically 0, and it is boolean true. (Hence it is actually a remarkably self documenting value!)
Yep. The only thing special about '0 but true' is that, like the stackoverflow answer states, it doesn't warn when coerced from a string:
$ perl -w -le 'print 3 + "0"' #no non-numeric values in string, no problem
3
$ perl -w -le 'print 3 + "0 but bob dole for erectile dysfunction awareness"'
Argument "0 but bob dole for erectile dysfunction awareness" isn't numeric in addition (+) at -e line 1.
3
$ perl -w -le 'print 3 + "0 but true"'
3
$ perl -w -le 'print 3 + "3 but bob dole for erectile dysfunction awareness"'
Argument "3 but bob dole for erectile dysfunction awareness" isn't numeric in addition (+) at -e line 1.
6
$ perl -w -le 'print 3 + "3 but true"'
Argument "3 but true" isn't numeric in addition (+) at -e line 1.
6
The other approach to allowing this kind of coercion this involves using == and ===, like in PHP/JS.
Very true. I was just trying to make the point that this isn't special interpretation of a magical character string. It is just convention that this is how to do this particular thing. In recognition of that convention, the "warnings" module gives a pass on this special case.
Also, note that "0.0", "0e0" and "0E0" all get the same behavior (without even requiring a special case in warnings.pm!), but are less self-documenting. They don't require a special case because they are just as numeric as strings like "42" (being various ways of representing the floating point value for 0), but they are character strings other than "0" or "" (which are the only two character strings which evaluate to false in a boolean context).
Well, I wanted to distinguish between what the OP claimed, that "perl thinks this is a number" and the true story which is "perl can treat any value as a number, if told to, including this one." "0 but true" is not given special treatment by the numerical processing system. It is given special treatment by a commonly used library named "warnings," in that if I turn on warnings and say "0 but true" + 1, I will get no warning, but if I say "0 but bob dole" + 1, then I will get THE EXACT SAME BEHAVIOR accompanied by a warning that I MAY be using a non-numeric value in a numeric context.
So, yes, the "warnings" library treats this string somewhat specially, but the perl language does not, really. (I qualify that with "really" because the warnings library is sort of part of the core language, and there are hooks written into the language to support that library. I bet that if you grep the perl language source-code for "0 but true", you'll find it, but it's only there to support the warnings library.)
Overloaded operators? Fine as long as they only except properly typed values and act consistently by those types.
Operators that coerce values to the proper types? Fine as long as the coercion is to a single type and the action taken always as though that type were passed.
Overloaded operators that act differently dependent on complex coercion tables dependent on the values of variables passed in? Fuck you javascript and PHP.
Personally, I like that "24" + "1" == 25 and NOT "241". Especially when compared to, say, javascript in which "24" - 1 == 23, yet "24" + 1 is "241". I've always been sort of upset by the notion that + means concatenation in several dynamic languages... there are so many other symbols that could be used for string concatenation... why screw with +, which has such an unambiguous meaning?
It's only ambiguous if your language is not strongly typed. String concatenation with + makes intuitive sense to most people, because the operations "append" and "add" are related in their minds.
28
u/etcshadow Mar 17 '11 edited Mar 17 '11
Perl does not "think it is a number". In perl, one can use any value (any scalar value) in a numeric context, a string context, or a boolean context. Perl has very strict, and fairly reasonable rules for how to interpret values in different contexts. For numeric contexts:
-If a number is used in a numeric context, it is interpreted as itself (duh)
-If a string is used in a numeric context, and any prefix of that string can be read as a decimal number, then it is interpreted as having that numeric value (e.g., "12.5 dogs" + "7 cats" == 19.5). Note that there actually is a difference between a number and a character string containing a number (e.g., 1000000 and "1000000" have different internal representations), but they behave in the same manner (aside from possible differences in memory use and runtime)
-any non-numeric, or non-string, or string without a leading number is zero (e.g., "hello" + "goodbye" == 0)
Likewise, there are rules for how values are interpreted in a boolean context:
-a number 0, the character string "0", the empty string "", and undef (a special value akin to null, nil, undefined in other languages) are all false
-anything else is true
Now, if you combine these two rules, then you find that, even though the number 0 is false, there are values (infinitely many values, in fact) which will be equal to zero if used in a numeric context, but which will evaluate to true in a boolean context. There are many occasions when this can be a useful. For example, if there is a function which should return a number of records processed but which should also report success or failure, then how would one report that zero records were successfully processed? "0 but true" means that function call was successful and processed zero records, which is distinct from returning 0 (or more likely undef), indicating that the function call failed for some reason.
(Note that perl also has a rich exception handling system. However, there are situations when a return-value indicating success or failure is more appropriate than throwing an exception, or at least that the author considers it more appropriate. I generally tend towards the "you're better off throwing an exception" camp... but perl is a language and a culture that frowns on holding a gun to your head.)
So, in short... no, perl does not 'think that "0 but true" is a number.' It is willing to treat anything as a number. And it is willing to evaluate anything as a boolean. The value "0 but true" is numerically 0, and it is boolean true. (Hence it is actually a remarkably self documenting value!)
(edited to fix list formatting)
7
u/blockeduser Mar 17 '11
Apparently it is because some functions return zero on success but people are used to getting "true" on success
8
u/nickburlett Mar 17 '11
Ah yes, one of my favorite perl "features"
(ok, that last one is really bad joke, but I wish the first one also was...)
1
u/[deleted] Mar 18 '11
I don't think it's funny.
-3
u/[deleted] Mar 17 '11
and
Translated to sane people's language: Don't use perl.
-4
u/[deleted] Mar 17 '11
It is like a sick joke.