tlm has asked for the wisdom of the Perl Monks concerning the following question:

Perl can handle arithmetic with strings, like '2x' and '3.e00.0.1.2y', consisting of a prefix that perl can recognize as a number followed by non-numeric stuff; for example

% perl -wle 'print "2x" + "3.e00.0.1.2y"' Argument "3.e00.0.1.2y" isn't numeric in addition (+) at -e line 1. Argument "2x" isn't numeric in addition (+) at -e line 1. 5
As far as I can tell1 this is an undocumented feature. Am I correct?

the lowliest monk


1I've scanned perlnumber and perlop for documentation, without success, but, admittedly, I haven't been able to come up with a good grepping strategy for this search.

Replies are listed 'Best First'.
Re: Is '2x' + '3y' == 5 documented?
by itub (Priest) on May 31, 2005 at 02:26 UTC
    The only specific enough mention I've found so far is section 2.6 of the Camel book, 3rd edition, where it says
    Perl converts between the various subtypes as needed, so you can treat a number as a string or a string as a number, and Perl will do the Right Thing. To convert from string to number, Perl internally uses something like the C library's atof(3) function.

    Of course, that assumes that you are familiar with the atof function, which is described in the manpage as

    double atof(const char *nptr);

    The atof() function converts the initial portion of the string pointed to by nptr to double.

    I have to recognize that the PHP documentation is more explicit in this regard; see for example http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

      I had an earlier post removed, presumably because I subconsciously knew this answer, but hadn't explicitly expressed it. (I had mumbled something along the lines of that this is the type of behaviour I would naturally expect).

      I suppose anyone with a good grounding in C would expect this answer.. it would only be a surprise to someone who hadn't used atof or atoi before that the '2x' + '3y' question would give a surprising answer.

      So, indeed, given that the Camel book explicitly states that atof is used, and given that atof is documented, it follows that this is indeed a documented feature of Perl. I will mod up the post to which this is a reply to.

Re: Is '2x' + '3y' == 5 documented?
by Tanktalus (Canon) on May 31, 2005 at 00:06 UTC

    According to the perlnumber I'm reading from my 5.8.5 documentation, +, along with many other binary arithmetic operators, "will attempt to convert arguments to integers" - if that doesn't keep precision, then "arguments are converted to floating point format and the floating point result is used." Since conversions are covered above, we go back to see how "decimal string" is converted to "native" types (floating/integer).

    Is that what you're looking for, or am I misreading your question, the documentation, or both?

      I was referring specifically to the coercion to numbers of strings that have numerical prefix but end in something that is not numeric. I would not have described these as "decimal strings."

      the lowliest monk