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

use strict; use warnings; my ($a, $b) = ("1_334", 1334); $a += 1; $b += 1; print "$a $b\n"; print "Unequal\n" if ($a != $b); print "$a $b\n";
I got the output:
2 1335 Argument "1_334" isn't numeric in addition (+) at noname.pl line 6. Unequal 2 1335
My question is if $a has already become 2, why does the comparison see "1_334". Also I would have thought that the numeric operator would have considered it as a string (in which case $a should have been 0+1) and evaluated to 1 or as a number 1334 and evaluated to 1335; either way being coverted to a number...

Replies are listed 'Best First'.
Re: _ in a number within quotes
by ELISHEVA (Prior) on Apr 19, 2009 at 15:31 UTC

    What you are seeing is a timing issue. Warnings get printed out via STDERR. Your print statements print out via STDOUT. These are two separate output streams. Sometimes one stream gets ahead of the other. $a has not yet become 2. The warning you are seeing is, in fact, coming from the $a +=1; statement.

    You can see this for yourself by either finding line 6 (it is the one containing $a += 1;) or by changing all of the print statements to begin with print STDERR "...".

    Best, beth

      That this is an OS artifact can also be seen by being explicit about the numbers of all the lines at which print statements are executed (note that on my machine, STDOUT and STDERR have not (yet) gotten out of sync):
      use strict; use warnings; my ($a, $b) = ("1_334", 1334); printf "line %ld: $a $b \n", __LINE__; $a += 1; $b += 1; printf "line %ld: $a $b \n", __LINE__; printf "line %ld: Unequal \n", __LINE__ if ($a != $b); printf "line %ld: $a $b \n", __LINE__;
      Output:
      C:\@Work\Perl\monks\dilpane>perl ubar_in_qqted_n.pl line 39: 1_334 1334 Argument "1_334" isn't numeric in addition (+) at ubar_in_qqted_n.pl l +ine 41. line 43: 2 1335 line 45: Unequal line 46: 2 1335
Re: _ in a number within quotes
by linuxer (Curate) on Apr 19, 2009 at 16:40 UTC
    Also I would have thought that the numeric operator would have considered it as a string (in which case $a should have been 0+1) and evaluated to 1 or as a number 1334 and evaluated to 1335;

    $a with its content "1_1334" is not treated as 0 in numeric context. Perl checks the value and sees that the string starts with a number. So it takes and uses that number in numeric context. That's why your $a + 1 results in a 1 + 1 =2 instead of a 0 + 1 = 1.

    Other example:

    perl -wle '$a = "7__hello"; print "yes" if $a == 7;' Argument "7__hallo" isn't numeric in numeric eq (==) at -e line 1. yes

    But beware: $a = "1_334"; is different to $a = 1_334;. While the first one is a string, which is treated as 1 in numeric context, the second one is a fully valid numeric value 1334. The underscore (_) can be inserted to improve readability.

Re: _ in a number within quotes
by moritz (Cardinal) on Apr 19, 2009 at 15:48 UTC

    Argument "1_334" isn't numeric in addition (+) at noname.pl line 6.

    ...

    why does the comparison see "1_334".

    (bold font added by me).

    It does not. It's the addition that warns.