A simple way to test for numeric values. It basically asks perl whether it thinks the argument is numeric, by testing whether the argument is "numerically equal" to itself, and trapping any errors.
Update: Dang! I guess one could add !ref($x) && to the last line, but the joy is gone...
Update 2: Thanks Fletch; Scalar::Util::looks_like_number is the way to go.
sub is_numeric { use warnings FATAL => qw( numeric uninitialized ); local $@; my $x = shift; return eval { $x == $x }; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: is_numeric
by Fletch (Bishop) on Apr 11, 2007 at 21:22 UTC | |
by syphilis (Archbishop) on Apr 12, 2007 at 07:09 UTC | |
by Fletch (Bishop) on Apr 12, 2007 at 14:26 UTC | |
by ysth (Canon) on Apr 12, 2007 at 18:31 UTC | |
by Fletch (Bishop) on Apr 12, 2007 at 18:40 UTC | |
by ology (Initiate) on May 02, 2009 at 17:43 UTC | |
Re: is_numeric
by jdporter (Paladin) on Apr 11, 2007 at 21:00 UTC | |
by GrandFather (Saint) on Apr 11, 2007 at 21:25 UTC | |
by ikegami (Patriarch) on Apr 12, 2007 at 01:37 UTC |
Back to
Cool Uses for Perl