Nobody has mentioned the Perl Cookbook yet. Recipe 2.1 discusses this topic, I quote here the regex for a C float (does not take IEEE 'NaN' and 'Infinity' values into account) and another suggested way using a wrapper around the POSIX function strtod:
warn "not a C float" unless /^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/; 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; } }
-- Hofmator
In reply to Re: Checking whether a $var is a number
by Hofmator
in thread Checking whether a $var is a number
by George_Sherston
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |