Hi perl-diddler,
I think BrowserUk made a good point, I just wanted to expand on that: numeric literals really are different from strings. Numeric literals are numbers; the literal 0xAA means the number 170. In this example, note especially the last item in the list:
$ perl -wMstrict -le '$,=", "; print 42, 0xFF, 0b1111, 42 + 0xFF + 0b1
+111, 42 . 0xFF . 0b1111'
42, 255, 15, 312, 4225515
Perl converts the literals internally before doing anything else with them.
Consider that there are limits on the size of integers that you can normally store in memory:
$ perl -wMstrict -le 'print 0xFFFFFFFF'
4294967295
$ perl -wMstrict -le 'print 0xFFFFFFFFF'
Hexadecimal number > 0xffffffff non-portable at -e line 1.
68719476735
$ perl -wMstrict -le 'print 0xFFFFFFFFFFFFFFFFF'
Integer overflow in hexadecimal number at -e line 1.
Hexadecimal number > 0xffffffff non-portable at -e line 1.
2.95147905179353e+20
Note that in each of the above examples, the results are exactly the same if you put a hex("...") around the literals.
Next, consider that there isn't a similar limit on the size of strings:
$ perl -wMstrict -le 'print hex( "0x".("F" x 2**29) )'
Integer overflow in hexadecimal number at -e line 1.
Hexadecimal number > 0xffffffff non-portable at -e line 1.
Inf
That string, which is over 500MB, is much more likely to be some kind of data than a number - does it make sense that it should be converted to Inf or some other floating-point number if the user accidentally uses it in numeric context? (Note even Math::BigInt can't handle interpreting a hex number that big on my system!)
Now, in theory, Perl could convert hexadecimal strings that are "small enough" to numbers automagically. But in such a theoretical solution, would it make sense that 0+"0xFFFFFFFF" eq "4294967295", but 0+"0x1FFFFFFFF" eq "0"? Where does one draw the line which numbers are "small enough", and won't such a division cause confusion for the users? (Let's ignore for now the potential performance impact of adding the code to check for hexadecimal to Perl's grok_number_flags.)
Instead, the design choice that literal numbers are fundamentally different from strings makes things much more consistent. Sure, one will have to call hex and oct a few times, but on the other hand, the argument can be made that calling those functions explicitly gives the code more clarity.
Hope this helps, -- Hauke D |