in reply to Argument spamming terminal
If I were debugging this code, I would do an experiment by just taking the call to int() out. Then of course int() can't issue a warning. Presumably that would result in the same type of "non-numeric value used in addition" somewhere else in the code. I would be interested in that code to help me understand why this weird conversion of string to numeric 0 is needed in the first place? Sometimes the immediate runtime error is not the "real" error.
use strict; use warnings; use feature 'say'; say force2integer(""); # 0 say force2integer(" "); # 0 # " " is actually True say force2integer("abc"); # 0 say force2integer("a10.5"); # 0 say force2integer(12.5); # 12 say force2integer("13"); # 13 say force2integer("14XYZ"); # 0 (would be 14 with a warning) say force2integer(undef); # 0 # Return 0 if the input cannot be converted to an # integer without a warning. # Does not consider malformed floats like: 10.1.2. sub force2integer { my $val = shift; return 0 unless defined $val; return 0 if ($val =~ /[^0-9.]/); # imperfect float test return 0 if ($val eq ""); # null is not numeric 0! return int($val); }
|
|---|