As others have said, always use warings; use strict;

When you do a math operation, == or +/- etc, Perl will convert a string value to a numeric value according to its rules. Trying to use a string that does not exactly represent a number as a number causes a warning with one exception shown below.

Here is some example code:

use strict; use warnings; $|=1; #turn off stdout buffering so error msgs match my $x = "awsdfasdf"; print "zero\n" if ($x==0); # zero #Argument "awsdfasdf" isn't numeric in numeric eq (==) $x+= 0; print "x\n"; #0 my $y="asdf6"; #ending digits to alpha do not count $y+= 0; #Argument "asdf6" isn't numeric in addition (+) print "$y\n"; #0 my $z = "3asdf"; #beginning digits will be used $z+=0; #Argument "3asdf" isn't numeric in addition (+) print "$z\n"; #3 my $x1 = "0 but true"; #more common now is 0E0 $x1+=0; # NO ERROR! print "$x1\n"; #0 my $x2 = "3 camels"; #can have textual "units" $x2+=0; #Argument "3 camels" isn't numeric in addition (+) print "$x2\n"; #3 __END__ Argument "awsdfasdf" isn't numeric in numeric eq (==) at line 7. zero x Argument "asdf6" isn't numeric in addition (+) at line 14. 0 Argument "3asdf" isn't numeric in addition (+) at line 20. 3 0 Argument "3 camels" isn't numeric in addition (+) at line 31. 3
As the above code shows the "0 but true" exception is hard-coded into Perl as an exception. This allows a function to return a single value that can represent both logically true and numerically zero. Using that string as a numeric value will not cause a warning. This is now seldom seen as the exponential value 0E0 is more commonly used, especially by the DBI.

In reply to Re: Hash value test of zero by Marshall
in thread Hash value test of zero by themcp

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.