FAQ 4.72 How do I determine whether a scalar is a number/whole/integer/float?

[email protected] (David Canzi) Mon, 11 Oct 2010 18:31:25 -0400
Newsgroups perl.perlfaq.workers
Organization University of Waterloo
Message-ID <[email protected]>
  Suggested change to simplify some regular expressions:

*** FAQ_4.72_before     Mon Oct 11 18:07:09 2010
--- FAQ_4.72_after      Mon Oct 11 18:09:57 2010
***************
*** 9,13 ****
               if (/^[+-]?\d+$/)    { print "is a +/- integer\n" }
!             if (/^-?\d+\.?\d*$/) { print "is a real number\n" }
!             if (/^-?(?:\d+(?:\.\d*)?|\.\d+)$/) { print "is a decimal 
number\n" }
!             if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/)
                               { print "a C float\n" }
--- 9,12 ----
               if (/^[+-]?\d+$/)    { print "is a +/- integer\n" }
!             if (/^-?(?:\d+\.?|\.\d)\d*$/) { print "is a decimal 
number\n" }
!             if (/^[+-]?(?=\.?\d)\d*\.?\d*(e[+-]?\d+)?$/i)
                               { print "a C float\n" }

The regex /^-?\d+\.?\d*$/ is deleted because it doesn't match
numeric strings like '.5' with no digits before the decimal point,
and is otherwise equivalent to the regex on the next line.  The
other two regexes are simplified.