Test2:...::Type(3pm)
π 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::TypeTest2
π 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!! β¨
π AUTHOR, COPYRIGHT and LICENCE π
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
- ubuntu stonking: libscalar-type-perl 1.0.1-1build2 Β· Artistic-1.0-Perl OR GPL-2.0-only
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms