LooksLike - See if a number looks like a number, integer, numeric, infinity, not-a-number, zero, non-zero, positive, negative, even, or odd.
use LooksLike; printf( "%5s|%6s|%3s|%3s|%3s|%7s|%4s|%7s|%3s|%3s|%4s|%3s\n", "", qw( Number Inf NaN Int Numeric Zero NonZero Pos Neg Even Odd ) ); for ( qw( -inf -1 -.23 0 0.0 0e0 .23 1 inf -nan ), -1e9999, -1, -0.23, 0, 0.23, 1e9999, 1e9999/1e9999 ) { printf( "%5s|%6d|%3d|%3d|%3d|%7d|%4d|%7d|%3d|%3d|%4d|%3d\n", $_, 0+ LooksLike::number(), 0+ LooksLike::infinity(), 0+ LooksLike::nan(), 0+ LooksLike::integer(), 0+ LooksLike::numeric(), 0+ LooksLike::zero(), 0+ LooksLike::nonzero(), 0+ LooksLike::positive(), 0+ LooksLike::negative(), 0+ LooksLike::even(), 0+ LooksLike::odd(), ); } # |Number|Inf|NaN|Int|Numeric|Zero|NonZero|Pos|Neg|Even|Odd # -inf| 1| 1| 0| 0| 0| 0| 1| 0| 1| 0| 0 # -1| 1| 0| 0| 1| 1| 0| 1| 0| 1| 0| 1 # -.23| 1| 0| 0| 0| 1| 0| 1| 0| 1| 0| 0 # 0| 1| 0| 0| 1| 1| 1| 0| 0| 0| 1| 0 # 0.0| 1| 0| 0| 0| 1| 1| 0| 0| 0| 0| 0 # 0e0| 1| 0| 0| 0| 1| 1| 0| 0| 0| 0| 0 # .23| 1| 0| 0| 0| 1| 0| 1| 1| 0| 0| 0 # 1| 1| 0| 0| 1| 1| 0| 1| 1| 0| 0| 1 # inf| 1| 1| 0| 0| 0| 0| 1| 1| 0| 0| 0 # -nan| 1| 0| 1| 0| 0| 0| 0| 0| 0| 0| 0 # -inf| 1| 1| 0| 0| 0| 0| 1| 0| 1| 0| 0 # -1| 1| 0| 0| 1| 1| 0| 1| 0| 1| 0| 1 # -0.23| 1| 0| 0| 0| 1| 0| 1| 0| 1| 0| 0 # 0| 1| 0| 0| 1| 1| 1| 0| 0| 0| 1| 0 # 0.23| 1| 0| 0| 0| 1| 0| 1| 1| 0| 0| 0 # inf| 1| 1| 0| 0| 0| 0| 1| 1| 0| 0| 0 # nan| 1| 0| 1| 0| 0| 0| 1| 0| 0| 0| 0
The module Scalar::Util has a useful function called looks_like_number, but it doesn't tell you what kind of number it is, and it also considers NaN and Infinity as numbers, which isn't always what you want. This attempts to be a lot more flexible about letting you ask what kind of number you have, and lets you decide how to handle that.
looks_like_number
NaN
Infinity
The module asks Perl about the value being held in the scalar, and if it has an integer or numeric value, it uses that for comparisons, otherwise it performs the test with a regular expression. This methodology should be more efficient for values that have been converted to a numeric value.
All of the functions will use $_ if there is no parameter given.
$_
The following functions work only on strings, as evaluating them numerially doesn't make sense: binary(), octal(), hex(), decimal(), and grok_number().
binary()
octal()
hex()
decimal()
grok_number()
The following functions operate on the numeric values, if they exist, otherwise they fall back to the regular expression equivalent: number(), integer(), numeric(), comparable(), infinity(), nan(), zero(), nonzero(), positive(), negative(), even(), and odd().
number()
integer()
numeric()
comparable()
infinity()
nan()
zero()
nonzero()
positive()
negative()
even()
odd()
There are numerous regular expressions available, if you'd like to search for numbers of a particular format: $Binary, $Octal, $Hex, $Decimal, $Infinity, $NaN, $Integer, $Numeric, $Zero, $Nonzero, $Positive, $Negative, $Even, and $Odd.
$Binary
$Octal
$Hex
$Decimal
$Infinity
$NaN
$Integer
$Numeric
$Zero
$Nonzero
$Positive
$Negative
$Even
$Odd
A zero character, followed by a "B" (ignoring case), followed by a series of zero and one characters
A zero character, followed by a series of zero through seven characters.
A zero character, followed by an "X" (ignoring case), followed by a series of zero through nine characters and/or "A" through "F" characters (ignoring case).
A series of zero through nine characters, possibly separated by a single period.
The case insensitive words "inf" and "infinity".
Perl version 5.22 and greater recognize a larger set of representations that include "1.#INF", "1.#Infinity", "1.#inf00", among others.
"1.#INF"
"1.#Infinity"
"1.#inf00"
The case insensitive words "nan".
Perl version 5.22 and greater recognize a larger set of representations that include "nanq", "nans", "qnan", "snan", "1.#nans", "1.#qnan", "1.#nan(123)", "1.#nan(0x45)", among others.
"nanq"
"nans"
"qnan"
"snan"
"1.#nans"
"1.#qnan"
"1.#nan(123)"
"1.#nan(0x45)"
A series of digits.
Anything which would be recognized as an integer or floating point number.
Anything which would be regarded as equal to 0.
$NonZero
Anything which looks like a number, but is not 0.
Any number that would compare to greater than 0.
Any number that would compare to less than 0.
Any integer which would divide evenly by 2.
Any integer which would divde oddly by 2.
%representation
The hash used by "representation( $_ ; %representation )" to format various numeric representations. Has three fields:
infinity
How positive infinity should be represented. Defaults to inf.
inf
-infinity
How negative infinity should be represented. Defaults to -inf.
-inf
nan
How not-a-number should be represented. Defaults to nan.
binary($_)
Returns true if the string starts with 0b and finishes with a series of 0 and 1 digits.
0b
0
1
octal($_)
Returns true if the string starts with 0 and finishes with a series of 0 through 7 digits.
7
hex($_)
Returns true if the string starts with 0x and finishes with a series of 0 through 9 or a through f digits.
0x
9
a
f
decimal($_)
Returns true if the string looks like a floating point number without the E exponent portion.
E
grok_number($_)
A pure Perl representation of the internal function of the same name. Returns 6 items:
Any leading + or - sign, or the empty string if there was no leading sign.
+
-
The whole part of the number, before the dot, if there is one. Could be the empty string. If it was an unsuccesful parse, could be undef. Could also be some form of NaN, IND, inf, or Infinity.
undef
IND
The fractional part of the number, after the dot, if there is one. Could be the empty string or undef.
It should not be possible for the number and fraction to both be the empty string.
Any leading + or - sign in the exponent, or the empty string if there was no leading sign. Could be the empty string or undef.
The digits representing the exponent. Could be undef.
If there was any part of the string that remained unparsed, it is returned as this substring. In a complete parse, it is the empty string.
infinity($_)
Returns a true value if the value represents some form of infinity. The strings infinity and inf are both valid (case-insensitively).
nan($_)
Returns a true value if the value represents some form of not-a-number (NaN). The string nan is valid (case-insensitively).
integer($_)
Returns true if the value is a series of ASCII digits 0 through 9. Does not guarantee that the number will fit into any number of bits.
numeric($_)
Returns true for any representation of a floating point number, which includes integers. It does not include the representations of inf and nan.
comparable($_)
Returns true for any representation of a number that can be compared to another number. In other words: true for infinity, integers, and floating point numbers; false for not-a-number, and anything else.
number($_)
Equivalent to "looks_like_number" in Scalar::Util, and returns true for all representations of infinity, not-a-number, integer, and floating point numbers.
zero($_)
Returns true for any value that would be interepreted equal (==) to 0.
==
nonzero($_)
Returns true for any value that would be interepreted not equal (!=) to 0.
!=
positive($_)
Returns true for any value that would be interpreted as greater than (>) 0.
>
negative($_)
Returns true for any value that would be interpreted as less than (<) 0.
<
even($_)
Returns true for any integer that would have no remainder when modulused with 2.
odd($_)
Returns true for any integer that would have a remainder when modulused with 2.
representation( $_ ; %representation )
Condense the large set of representations for infinity and not-a-number to a simple set. Pass in a value (or use $_ if nothing is passed in), and if it's something that matches positive infinity, negative infinity, or not-a-number, then format it as the %representation hash indicates.
The keys and values to override the %representation hash can be passed in, and the values will be used in place of the defaults.
Since v0.20.056.
Nothing, my code is perfect. Please let me know if you think that statement is incorrect.
Please report any bugs or feature requests on the bugtracker website https://github.com/rkleemann/LooksLike/issues
When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.
This document describes version v0.20.060 of this module.
Bob Kleemann <bobk@cpan.org>
This software is Copyright (c) 2020 by Bob Kleemann.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install LooksLike, copy and paste the appropriate command in to your terminal.
cpanm
cpanm LooksLike
CPAN shell
perl -MCPAN -e shell install LooksLike
For more information on module installation, please visit the detailed CPAN module installation guide.