diff options
author | brian d foy <brian.d.foy@gmail.com> | 2010-10-13 02:40:49 -0500 |
---|---|---|
committer | brian d foy <brian.d.foy@gmail.com> | 2010-10-13 02:40:49 -0500 |
commit | a13ded5516803d9baff49d9177619361dcc9d083 (patch) | |
tree | 591cfae67a84d5efb798e873ba22b716364e4d27 /pod/perlfaq4.pod | |
parent | 92f9d56c664ae354430f66e7a870fb3f4b0f44a3 (diff) | |
download | perl-a13ded5516803d9baff49d9177619361dcc9d083.tar.gz |
perlfaq4: use given() in number/float/integer answer
How do I determine whether a scalar is a number/whole/integer/float?
David Canzi also adjusted some of the regexes. The
real number and decimal number tests are really the
same thing, and we don't have to capture in the float
case.
Diffstat (limited to 'pod/perlfaq4.pod')
-rw-r--r-- | pod/perlfaq4.pod | 34 |
1 files changed, 21 insertions, 13 deletions
diff --git a/pod/perlfaq4.pod b/pod/perlfaq4.pod index e972d770f0..139885baf6 100644 --- a/pod/perlfaq4.pod +++ b/pod/perlfaq4.pod @@ -2506,31 +2506,39 @@ some gotchas. See the section on Regular Expressions. =head2 How do I determine whether a scalar is a number/whole/integer/float? Assuming that you don't care about IEEE notations like "NaN" or -"Infinity", you probably just want to use a regular expression. +"Infinity", you probably just want to use a regular expression: - if (/\D/) { print "has nondigits\n" } - if (/^\d+\z/) { print "is a whole number\n" } - if (/^-?\d+\z/) { print "is an integer\n" } - if (/^[+-]?\d+\z/) { print "is a +/- integer\n" } - if (/^-?\d+\.?\d*\z/) { print "is a real number\n" } - if (/^-?(?:\d+(?:\.\d*)?|\.\d+)\z/) { print "is a decimal number\n" } - if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?\z/) - { print "a C float\n" } + use 5.010; + + given( $number ) { + when( /\D/ ) + { say "\thas nondigits"; continue } + when( /^\d+\z/ ) + { say "\tis a whole number"; continue } + when( /^-?\d+\z/ ) + { say "\tis an integer"; continue } + when( /^[+-]?\d+\z/ ) + { say "\tis a +/- integer"; continue } + when( /^-?(?:\d+\.?|\.\d)\d*\z/ ) + { say "\tis a real number"; continue } + when( /^[+-]?(?=\.?\d)\d*\.?\d*(?:e[+-]?\d+)?\z/i) + { say "\tis a C float" } + } There are also some commonly used modules for the task. L<Scalar::Util> (distributed with 5.8) provides access to perl's internal function C<looks_like_number> for determining whether a -variable looks like a number. L<Data::Types> exports functions that +variable looks like a number. L<Data::Types> exports functions that validate data types using both the above and other regular expressions. Thirdly, there is C<Regexp::Common> which has regular expressions to match various types of numbers. Those three modules are available from the CPAN. If you're on a POSIX system, Perl supports the C<POSIX::strtod> -function. Its semantics are somewhat cumbersome, so here's a -C<getnum> wrapper function for more convenient access. This function +function. Its semantics are somewhat cumbersome, so here's a +C<getnum> wrapper function for more convenient access. This function takes a string and returns the number it found, or C<undef> for input -that isn't a C float. The C<is_numeric> function is a front end to +that isn't a C float. The C<is_numeric> function is a front end to C<getnum> if you just want to say, "Is this a float?" sub getnum { |