Dave's Free Press

Technology: Scalar-Type-0.0.4

 

Older stuff

No Title


NAME

Scalar::Type


DESCRIPTION

Figure out what type a scalar is


SYNOPSIS

  use Scalar::Type qw(is_number);
  if(is_number(2)) {
      # yep, 2 is a number
      # it is_integer too
  }
  if(is_number("2")) {
      # no, "2" is a string
  }


OVERVIEW

Perl scalars can be either strings or numbers, and normally you don't really care which is which as it will do all the necessary type conversions automagically. This means that you can perform numeric operations on strings and provided that they looks like a number you'll get a sensible result:

    my $string = "4";
    my $number = 1;
    my $result = $string + $number; # 5

But in some rare cases, generally when you are serialising data, the difference matters. This package provides some useful functions to help you figure out what's what. The following functions are available. None of them are exported by default. If you want them all, export ':all':

    use Scalar::Type qw(:all);

and if you just want the 'is_*' functions you can get them all in one go:

    use Scalar::Type qw(is_*);

For Reasons, :is_* is equivalent.


FUNCTIONS

type

Returns the type of its argument. If the argument is a reference then it returns either blessed($argument) (if it's an object) or 'REF_TO_'.ref($argument). Otherwise it looks for the IOK or NOK flags on the underlying SV and returns INTEGER or NUMBER as appropriate. Finally, if neither of those are set it returns SCALAR.

is_integer

Returns true if its argument is an integer. Note that ``1'' is not an integer, it is a string. 1 is an integer. 1.1 is obviously not an integer. 1.0 is also not an integer, as it makes a different statement about precision - 1 is *exactly* one, but 1.0 is only one to two significant figures.

All integers are of course also numbers.

is_number

Returns true if its argument is a number. ``1'' is not a number, it is a string. 1 is a number. 1.0 and 1.1 are numbers too.


SEE ALSO

Scalar::Util in particular its blessed function.


BUGS

If you find any bugs please report them on Github, preferably with a test case.

Integers that are specifed using exponential notation, such as if you say 1e2 instead of 100, are *not* internally treated as integers. The perl parser is lazy and only bothers to convert them into an integer after you perform int-ish operations on them, such as adding 0. Likewise if you add 0 to the thoroughly non-numeric ``100'' perl will convert it to an integer. These edge cases are partly why you almost certainly don't care about what this module does. If they irk you, complain to p5p.


FEEDBACK

I welcome feedback about my code, especially constructive criticism.


AUTHOR, COPYRIGHT and LICENCE

Copyright 2021 David Cantrell <david@cantrell.org.uk>

This software is free-as-in-speech software, and may be used, distributed, and modified under the terms of either the GNU General Public Licence version 2 or the Artistic Licence. It's up to you which one you use. The full text of the licences can be found in the files GPL2.txt and ARTISTIC.txt, respectively.


CONSPIRACY

This module is also free-as-in-mason software.