in reply to Hash value test of zero
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:
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.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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Hash value test of zero
by eyepopslikeamosquito (Archbishop) on Jul 26, 2023 at 10:30 UTC | |
by tobyink (Canon) on Jul 26, 2023 at 10:39 UTC | |
by eyepopslikeamosquito (Archbishop) on Jul 26, 2023 at 11:11 UTC | |
by NERDVANA (Priest) on Jul 27, 2023 at 05:14 UTC |