You want to check whether a string represents a valid number. This is a common problem when validating input, as in a CGI script.
Compare it against a regular expression that matches the kinds of numbers you're interested in.
if ($string =~ /PATTERN/) { # is a number } else { # is not }
This problem gets to the heart of what we mean by a number. Even things that sound simple, like integer , make you think hard about what you will accept ("Is a leading + for positive numbers optional, mandatory, or forbidden?"). The many ways that floating-point numbers can be represented could overheat your brain.
You must decide what you will and will not accept. Then, construct a regular expression to match those things alone. Here are some precooked solutions (the cookbook's equivalent of just-add-water meals) for most common cases.
warn "has nondigits" if /\D/; warn "not a natural number" unless /^\d+$/; # rejects -3 warn "not an integer" unless /^-?\d+$/; # rejects +3 warn "not an integer" unless /^[+-]?\d+$/; warn "not a decimal number" unless /^-?\d+\.?\d*$/; # rejects .2 warn "not a decimal number" unless /^-?(?:\d+(?:\.\d*)?|\.\d+)$/; warn "not a C float" unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/;
These lines do not catch the IEEE notations of "Infinity" and "NaN", but unless you're worried that IEEE committee members will stop by your workplace and beat you over the head with copies of the relevant standards documents, you can probably forget about these strange numbers.
If your number has leading or trailing whitespace, those patterns won't work. Either add the appropriate logic directly, or call the
trim
function from
Recipe 1.14
.
If you're on a POSIX system, Perl supports the
POSIX::strtod
function. Its semantics are cumbersome, so here's a
getnum
wrapper function for more convenient access. This function takes a string and returns the number it found or
undef
for input that isn't a C float. The
is_numeric
function is a front end to
getnum
for when you just want to say "Is this a float?"
sub getnum { use POSIX qw(strtod); my $str = shift; $str =~ s/^\s+//; $str =~ s/\s+$//; $! = 0; my($num, $unparsed) = strtod($str); if (($str eq '') || ($unparsed != 0) || $!) { return; } else { return $num; } } sub is_numeric { defined scalar &getnum }
The regular expression syntax in perlre (1) and Chapter 2 of Programming Perl ; your system's strtod (3) manpage; the documentation for the standard POSIX module (also in Chapter 7 of Programming Perl )
Copyright © 2001 O'Reilly & Associates. All rights reserved.