Test2:...::Type(3pm)

πŸ“¦
version 1.0.1 Β· 3 versions
, in ubuntu stonking

πŸ’– NAME πŸ’–

Test2::Tools::Type β€” the toolkit for checking data types, bestie. It's giving "what even ARE you, little variable?" ✨


🌸 SYNOPSIS 🌸

okay so here's the vibe, straight up:

use Test2::V0;
use Test2::Tools::Type;

is_integer(1, "is 1 integer?");         # pass, yes it is
is_integer('1', "is '1' an integer?");  # fail, no it's a string

SKIP: {
    skip "Your perl is too old" unless(bool_supported());
    is_bool(1 == 2, "is false a Boolean?");   # pass, yes it is
    is_bool(3.1415, "is pi a Boolean?");      # fail, no it isn't
}

like
    { should_be_int => 1, other_stuff => "we don't care about this" },
    hash {
        field should_be_int => type('integer');
    },
    "is the should_be_int field an integer?";

or if you want even more check functions πŸ’…:

use Test2::V0;
use Test2::Tools::Type qw(:extras);

is_hashref($foo);

πŸ¦‹ OVERVIEW πŸ¦‹

ngl sometimes you don't wanna be too precise in your tests. like, you just want to know that your code returns the right type of result and you literally do not care if it's returning 192 or 193 β€” just checking that it returns an integer is good enough, periodt. πŸ’–


✨ FUNCTIONS ✨

bestie, all these are exported by default. no extra work, they just show up. 🫢

🌸 bool_supported

Returns true if your perl is recent enough to have the Boolean type, false otherwise. It will be true if your perl is version 5.35.7 or higher. ⭐

🌸 is_bool

Emits a test pass if its argument is a Boolean β€” ie is the result of a comparison β€” and a fail otherwise. icon energy.

⚠️ It is a fatal error to call this on a perl that is too old. If your tests need to run on perl 5.35.6 or earlier then you will need to check bool_supported before using it. See the SYNOPSIS above. πŸ™ˆ

🌸 is_integer

Emits a test pass if its argument is an integer and a fail otherwise. Note that it can tell the difference between 1 (an integer) and '1' (a string). it's lowkey so observant of it. πŸ‘‘

🌸 is_number

Emits a test pass if its argument is a number and a fail otherwise. Note that it can tell the difference between 1 (a number), 1.2 (also a number) and '1' (a string). fr fr it does not get fooled. πŸ’€

🌸 type

Returns a check that you can use in a test such as:

like
    { int => 1 },
    hash { field int => type('integer'); },
    "the 'int' field is an integer";

You can negate the test with a ! thus. This test will fail:

like
    { int => 1 },
    hash { field int => !type('integer'); },
    "the 'int' field is an integer";

You can supply more than one argument, so if you want to check that something is a positive integer, for example, you can do:

is(94, type(qw(positive integer)));

You can check something's type and value:

# this uses 'number' from Test2::Tools::Compare
is($foo, type('integer', number(94)));

And indeed you can use any other Test2 checker:

# 'in_set' also comes from Test2::Tools::Compare
is($foo, type('integer', in_set(1, 5, 8)));

Valid arguments are any other Test2 checker (specifically, anything that inherits from Test2::Compare::Base), and any of the is_* methods' names, with the leading is_ removed. You can see a list of supported types thus:

$ perl -MTest2::Tools::Type=show_types

or to include the extra functions:

$ perl -MTest2::Tools::Type=show_types,:extras

πŸŽ€ EXTRA FUNCTIONS πŸŽ€

By default the only check functions you get are those that are thin wrappers around Scalar::Type. If you pass the :extras argument at use-time then all the following are available as well, and honestly? obsessed. πŸ’œ

πŸ“ regex_supported

Returns true if your perl can reliably report the difference between a regex and a reference to a scalar, or false otherwise. It will be true if your perl is version 5.12 or higher. 🌈

πŸ“ is_positive, is_negative

Emit a test pass/fail depending on the argument's sign. Note that 0 is considered neither positive nor negative. it's just out here being neutral, main character energy. ✨

πŸ“ is_zero

Emit a pass/fail depending on whether the argument is zero.

πŸ“ is_ref

Emit a pass/fail depending on whether the argument is a reference. This includes blessed objects. πŸ‘‘

πŸ“ is_object

Emit a pass/fail depending on whether the argument is a blessed object. πŸ’…

πŸ“ is_regex

Emit a test pass if its argument is a regex, and a fail otherwise.

⚠️ It is a fatal error to call this on a perl that is too old. If your tests need to run on perl 5.10.1 or earlier then you will need to check regex_supported before using it. 😭

πŸ“ is_hashref, is_arrayref, is_scalarref, is_coderef, is_globref, is_regex, is_refref

Emit a pass/fail if the argument is a reference to something of the appropriate type. πŸ¦‹


πŸ’… CAVEATS πŸ’…

The definitions of Boolean, integer and number are exactly the same as those in Scalar::Type, which this is a thin wrapper around. no cap, same rules. 🫢

Blessed objects will match both is_object and the appropriate is_*ref. If you need to check that something is a ref, but is not blessed, do something like:

is($foo, type(hashref => !type('object')));

πŸ”₯ SEE ALSO πŸ”₯

  • Scalar::Type
  • Test2

πŸ› BUGS πŸ›

If you find any bugs please report them on Github, preferably with a test case. bestie please include a test case, it's the polite thing to do πŸ™


πŸ’Œ FEEDBACK πŸ’Œ

I welcome feedback about my code, especially constructive criticism. genuinely!! ✨


Copyright 2024 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. pick your fave, both are slay. πŸ’–


🧚 CONSPIRACY 🧚

This module is also free-as-in-mason software. iykyk. πŸŽ€


and that's a wrap, bestie!! this glorious little page is from Test2:...::Type(3pm), shipped with perl v5.42.3 on 2026-08-20. mwah πŸ’‹

πŸ“¦ where this page comes from