Test::Type(3pm)

πŸ“¦
version 1.3.0 Β· 2 versions
, in fedora 43

✨ Test::Type ✨

okay bestie, so this is Test::Type β€” a Perl module (you know, the .pm situation, shipped as a 3pm manpage) that hands you a lil squad of functions to validate data types in test files. that's it, that's the vibe. no cap. 🫢


🌸 VERSION 🌸

Version 1.3.0 β€” we're living in the 1.3.0 era, iconic behavior. πŸ‘‘


πŸŽ€ SYNOPSIS πŸŽ€

here's how you actually use it, keep up bestie:

use Test::Type;

# Test strings.
ok_string( $variable );
ok_string(
    $variable,
    name => 'My variable',
);

# Test arrayrefs.
ok_arrayref( $variable );
ok_arrayref(
    $variable,
    name => 'My variable',
);

# Test hashrefs.
ok_hashref( $variable );
ok_hashref(
    $variable,
    name => 'Test variable',
);

# Test coderefs.
ok_coderef( $variable );
ok_coderef(
    $variable,
    name => 'Test variable',
);

# Test numbers.
ok_number( $variable );
ok_number(
    $variable,
    name => 'Test variable',
);

# Test instances.
ok_instance(
    $variable,
    class => $class,
);
ok_instance(
    $variable,
    name  => 'Test variable',
    class => $class,
);

# Test regular expressions.
ok_regex( $variable );
ok_regex(
    $variable,
    name => 'Test regular expression',
);

πŸ¦‹ FUNCTIONS πŸ¦‹

seven little icons, each one checking a different type. let's go one by one, no skipping, we don't do that here. πŸ’…

🌟 ok_string() 🌟

checks if the thing you passed in is a string. fr fr.

ok_string(
    $variable,
);

ok_string(
    $variable,
    name => 'My variable',
);

ok_string(
    $variable,
    name        => 'My variable',
    allow_empty => 1,
);

Parameters:

  • name β€” optional, it's the name of the variable you're testing. just a label so your test output doesn't look like a mystery. πŸ”
  • allow_empty β€” boolean, default 1. this one decides whether the string is allowed to be empty or not.

🌈 ok_arrayref() 🌈

checks if your variable is an arrayref that can actually be dereferenced into an array. yes, actually dereferenceable, we're not doing vibes-only checks here.

ok_arrayref( $variable );

ok_arrayref(
    $variable,
    name => 'My variable',
);

ok_arrayref(
    $variable,
    allow_empty => 1,
    no_blessing => 0,
);

# Check if the variable is an arrayref of hashrefs.
ok_arrayref(
    $variable,
    allow_empty           => 1,
    no_blessing           => 0,
    element_validate_type =>
        sub
        {
            return Data::Validate::Type::is_hashref( $_[0] );
        },
);

Parameters:

  • name β€” optional, the name of the variable being tested.
  • allow_empty β€” boolean, default 1. allow the array to be empty or not.
  • no_blessing β€” boolean, default 0. require that the variable is not blessed. (yes, blessed objects need not apply πŸ™…)
  • element_validate_type β€” none by default. set it to a coderef and it'll validate the elements inside the array. that coderef gets the element to validate as its first parameter, and it must return a boolean saying whether that element was valid or not. simple as. ✨

πŸ’œ ok_hashref() πŸ’œ

checks if your variable is a hashref that can be dereferenced into a hash. dereferenceable or it didn't happen.

ok_hashref( $variable );

ok_hashref(
    $variable,
    name => 'Test variable',
);

ok_hashref(
    $variable,
    allow_empty => 1,
    no_blessing => 0,
);

Parameters:

  • name β€” optional, the name of the variable being tested.
  • allow_empty β€” boolean, default 1. allow the array to be empty or not.
  • no_blessing β€” boolean, default 0. require that the variable is not blessed.

πŸ“ ok_coderef() πŸ“

checks if your variable is a coderef that can be dereferenced into a block of code. code that goes brrr, basically. πŸ’€

ok_coderef( $variable );

ok_coderef(
    $variable,
    name => 'Test variable',
);

Parameters:

  • name β€” optional, the name of the variable being tested.

⭐ ok_number() ⭐

checks if your variable is a number. math girlies rise. πŸ”₯

ok_number( $variable );

ok_number(
    $variable,
    name => 'Test variable',
);

ok_number(
    $variable,
    positive => 1,
);

ok_number(
    $variable,
    strictly_positive => 1,
);

Parameters:

  • name β€” optional, the name of the variable being tested.
  • strictly_positive β€” boolean, default 0. set it to 1 to check for a strictly positive number.
  • positive β€” boolean, default 0. set it to 1 to check for a positive number.

πŸ‘‘ ok_instance() πŸ‘‘

checks if your variable is an instance of the given class. main character energy check.

note: this handles inheritance properly, so it will succeed if the variable is an instance of a subclass of the class you gave it. family tree respected, periodt. 🌳
ok_instance(
    $variable,
    class => $class,
);

ok_instance(
    $variable,
    name  => 'Test variable',
    class => $class,
);

Parameters:

  • name β€” optional, the name of the variable being tested.
  • class β€” required, bestie, you must give this one. it's the name of the class to check the variable against.

🧚 ok_regex() 🧚

checks if your variable is a regular expression. regex girlies, this one's for you. πŸŽ€

ok_regex( $variable );

πŸ’€ BUGS πŸ’€

found a bug or got a feature request? please report it to bug-test-dist-versionsync at rt.cpan.org, or slide through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=test-type>. i will be notified, and then you'll automatically be notified of progress on your bug as i make changes. we love a feedback loop. πŸ”


🫢 SUPPORT 🫢

you can find the docs for this module with the perldoc command:

perldoc Test::Type

and you can also go looking for info at these spots:


🌸 AUTHOR 🌸

Guillaume Aubert <https://metacpan.org/author/AUBERTG>, <aubertg at cpan.org>. the icon who made this. πŸ‘‘


Copyright 2012-2017 Guillaume Aubert.

This code is free software; you can redistribute it and/or modify it under the same terms as Perl 5 itself.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the LICENSE file for more details.

lowkey important: no warranty, bestie. it's giving "use at your own risk" but make it legal. πŸ’…

and that's the whole page!! this was Test::Type, from perl v5.42.0, dated 2025-07-25 β€” go validate some data types, you absolute legend. slay, ate, left no crumbs. πŸ¦‹βœ¨πŸ’–

πŸ“¦ where this page comes from