Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Re: Numification of strings

by ww (Archbishop)
on Aug 02, 2010 at 01:24 UTC ( #852385=note: print w/replies, xml ) Need Help??

in reply to Numification of strings

Not an in-depth answer... nor a completely satisfactory answer to your explicit question about bugs, but no!

All your examples except the first involve mixed alphanumerics... which, when numified -- via automatic conversion -- are converted by discarding "(t)railing non-number stuff and leading whitespace" 1 (and use warnings will call that to your attention) while treating leading (after discarding whitespace) numerals as a number.

What's more, if $x = "LanX" (no numerals, as in your line 1, "DB<9>") numification will treat $x as 0, whether you add, subtract, multiply or perform any other arithmetic operation...

perl -e "$x="LanX"; print ($x * 3);" 0

I don't know enough about perlguts to be sure morgon's suppostion about atoi is correct, but it certainly sounds plausible. On the other hand, I can't see how a well-defined and well-documented behaviour is "hiding ugly bugs."

1   Learning Perl, 3rd Ed (paper), Schwartz & Phoenix, p26.

Replies are listed 'Best First'.
Re^2: Numification of strings
by LanX (Bishop) on Aug 02, 2010 at 12:14 UTC
    Learning Perl, 3rd Ed (paper), Schwartz & Phoenix, p26.

    no offense against Merlyn or O'Reilly ... but this should be clearly documented in the perldocs and for all affected operators and not only in the section for "unary -".

    And yes I missed the warnings .. I was recently playing around with shells of 4 other dynamic languages and the perl-debugger surprised me in not showing warnings by default.

    Cheers Rolf

      Actually, there's a fairly explicit explanation in perldoc perlnumber:

      Arithmetic operators The binary operators "+" "-" "*" "/" "%" "==" "!=" ">" "<" ">=" "< +=" and the unary operators "-" "abs" and "--" will attempt to convert arguments to integers. If both conversions are possible without lo +ss of precision, and the operation can be performed without loss of precision then the integer result is used. Otherwise arguments are converted to floating point format and the floating point result i +s used. The caching of conversions (as described above) means that t +he integer conversion does not throw away fractional parts on floatin +g point numbers. ++ "++" behaves as the other operators above, except that if it is a string matching the format "/^[a-zA-Z]*[0-9]*\z/" the string increment described in perlop is used. Arithmetic operators during "use integer" In scopes where "use integer;" is in force, nearly all the operato +rs listed above will force their argument(s) into integer format, and return an integer result. The exceptions, "abs", "++" and "--", do not change their behavior with "use integer;"

      Note the exception for "++" -- which several of us, esp. /me -- forgot to mention...and the additional exceptions while using use integer.

      BTW, there's some related matter (much less clear, to me, anyway) in perlvar... and perhaps (probably?) in other docs? How say you, Monks?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://852385]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2018-05-27 05:42 GMT
Find Nodes?
    Voting Booth?