The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

LooksLike - See if a number looks like a number, integer, numeric, infinity, not-a-number, zero, non-zero, positive, negative, even, or odd.

SYNOPSIS

    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

DESCRIPTION

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.

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.

Regexp Only Functions

The following functions work only on strings, as evaluating them numerially doesn't make sense: binary(), octal(), hex(), decimal(), and grok_number().

Numeric or Regexp Functions

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().

Regular Expressions

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.

VARIABLES

$Binary

A zero character, followed by a "B" (ignoring case), followed by a series of zero and one characters

$Octal

A zero character, followed by a series of zero through seven characters.

$Hex

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).

$Decimal

A series of zero through nine characters, possibly separated by a single period.

$Infinity

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.

$NaN

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.

$Integer

A series of digits.

$Numeric

Anything which would be recognized as an integer or floating point number.

$Zero

Anything which would be regarded as equal to 0.

$NonZero

Anything which looks like a number, but is not 0.

$Positive

Any number that would compare to greater than 0.

$Negative

Any number that would compare to less than 0.

$Even

Any integer which would divide evenly by 2.

$Odd

Any integer which would divde oddly by 2.

FUNCTIONS

binary($_)

Returns true if the string starts with 0b and finishes with a series of 0 and 1 digits.

octal($_)

Returns true if the string starts with 0 and finishes with a series of 0 through 7 digits.

hex($_)

Returns true if the string starts with 0x and finishes with a series of 0 through 9 or a through f digits.

decimal($_)

Returns true if the string looks like a floating point number without the E exponent portion.

grok_number($_)

A pure Perl representation of the internal function of the same name. Returns 6 items:

sign

Any leading + or - sign, or the empty string if there was no leading sign.

number

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.

fraction

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.

exponent sign

Any leading + or - sign in the exponent, or the empty string if there was no leading sign. Could be the empty string or undef.

exponent number

The digits representing the exponent. Could be undef.

excess

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.

TODO

Nothing, my code is perfect. Please let me know if you think that statement is incorrect.

BUGS

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.

VERSION

This document describes version v0.20.045 of this module.

AUTHOR

Bob Kleemann <bobk@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2020 by Bob Kleemann.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)