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

okay.. going back over my old code with use warnings, use strict, and use diagnostics, i'm getting a few errors i don't understand. All of them are referring to the same misunderstanding, so i'll only post the first error:
Argument "" isn't numeric in addition (+) at perlcomplex.txt line 151 +(#1) (W numeric) The indicated string was fed as an argument to an oper +ator that expected a numeric value instead. If you're fortunate the me +ssage will identify which operator was so unfortunate.
to this error, i say "duh"
What i'm doing is taking numbers out of a string, and then adding them, and then putting them back into a string. So, when i take them out of the string as substrings, they are still of type string. But! I thought perl didn't separate type String and type Integer.. all are type Scalar.. and without the use strict, this works as intended.
What am i doing wrong?

Replies are listed 'Best First'.
Re: "" isn't numeric...
by jeffa (Bishop) on Oct 14, 2005 at 18:06 UTC

    This is just a warning, not an error. You could always use the ||= operator to force an empty string to 0

    use warnings; my $numb = ''; print $numb + 5; # issues warning $numb ||= 0; print $numb +5; # no warning
    It is a minor nuisance, but it is a nice warning to know about it, as it might lead to unexpected results down the road.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      i tend to do that inline, too:
      my $numb = ''; print 5 + ($numb||0); # no warning
      I also do the same thing to avoid "Use of uninitialized value in concatenation (.) or string" warnings:
      print "==> Foo: " . ($s || '') . "<===\n";
Re: "" isn't numeric...
by Roy Johnson (Monsignor) on Oct 14, 2005 at 18:02 UTC
    You're extracting an empty string. There are no digits in it, so it doesn't look like a number.

    Caution: Contents may have been coded under pressure.
Re: "" isn't numeric...
by Zaxo (Archbishop) on Oct 14, 2005 at 18:09 UTC

    Without strict and warnings, numeric context will convert the empty string to 0, (as it will 'abc' and 'foo'). The real question is whether you're expecting or can tolerate an empty string at that point in your code. You'll need to show the code and say what you're trying to do to get advice on that.

    How are you extracting those numbers from your string? You may want to set up an explicit numeric conversion if an empty string is a sane result.

    After Compline,
    Zaxo

      <nit> Strict has no bearing on the empty string's conversion to 0. Warnings just bitch about it - the empty string is still converted.</nit>

      My criteria for good software:
      1. Does it work?
      2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: "" isn't numeric...
by itub (Priest) on Oct 14, 2005 at 18:58 UTC
    If you are certain you want to rely on perl's implicit conversion of empty (or other "non-numeric looking") strings to numeric zero, you can get rid of the warning by adding this line:

    no warnings 'numeric';

Re: "" isn't numeric...
by graff (Chancellor) on Oct 15, 2005 at 14:57 UTC
    Because you said this:
    What i'm doing is taking numbers out of a string, and then adding them, and then putting them back into a string.

    I would guess that you want to pay close attention to this particular warning rather than ignore it. It means that what you have "taken out" is not a numeric substring, but rather an empty string. Depending on how you "put back" the result of your addition, it might not end up being placed where you think it should. For example, if you are doing something like this:

    my ($num) = (/ (\d+) /); my $newnum = $num + $some_other_number; s/ $num / $newnum /;
    If the initial regex match fails, setting $num to an empty string, then $newnum will either be inserted within the first occurrence of two consecutive spaces, or else not be inserted at all (if the original string never has two consecutive spaces).