TEST2::TOOLS::EXPORTS(3perl)
π Test2::Tools::Exports β the perl manual, but make it cute π
okay bestie, this is Test2::Tools::Exports(3perl) from the Perl Programmers Reference Guide, and it's lowkey one of the most useful little test helpers ever. it's giving "did that import actually happen" energy. let's get into it π«Ά
β¨ NAME β¨
Test2::Tools::Exports β tools for validating exporters. no cap, that's the whole job: checking that your imports actually landed.
πΈ DESCRIPTION πΈ
these are the tools you reach for when you need to check that symbols have been imported into your namespace. that's it. that's the vibe. you said use Something qw/foo/ and now you wanna prove foo is really sitting there in your package. iconic behavior honestly. π
π¦ SYNOPSIS π¦
use Test2::Tools::Exports
use Data::Dumper;
imported_ok qw/Dumper/;
not_imported_ok qw/dumper/;
see how Dumper passes and dumper doesn't? case sensitivity said "not today" π
π EXPORTS π
all subs are exported by default. free. gifted. no strings attached. β¨
imported_ok(@SYMBOLS)
check that the specified symbols exist in the current package.
okay so important caveats, bestie, don't skip these:
- this will not find inherited subs.
- this will only find symbols in the current package's symbol table.
- this WILL NOT confirm that the symbols were defined outside of the package itself.
imported_ok( '$scalar', '@array', '%hash', '&sub', 'also_a_sub' );
@SYMBOLS can contain any number of symbols. each item in the array must be a string. the string should be the name of a symbol. if a sigil is present then it will search for that specified type; if no sigil is specified it will be used as a sub name. simple as that π
not_imported_ok(@SYMBOLS)
check that the specified symbols do not exist in the current package.
same disclaimers, same energy:
- this will not find inherited subs.
- this will only look at symbols in the current package's symbol table.
not_imported_ok( '$scalar', '@array', '%hash', '&sub', 'also_a_sub' );
@SYMBOLS can contain any number of symbols. each item in the array must be a string. the string should be the name of a symbol. if a sigil is present, then it will search for that specified type. if no sigil is specified, it will be used as a sub name. π§
π CAVEATS π
okay this is the part where we get real. buckle up.
before Perl 5.10, it is very difficult to distinguish between a package scalar that is undeclared vs declared and undefined. currently imported_ok and not_imported_ok cannot see package scalars declared using our $var unless the variable has been assigned a defined value. π
this will pass on recent perls, but fail on perls older than 5.10:
use Test2::Tools::Exports;
our $foo;
# Fails on perl onlder than 5.10
imported_ok(qw/$foo/);
(yes, "onlder" β that typo is straight from the source, we keep the facts and the typos, fr fr π)
if $foo is imported from another module, or imported using use vars qw/$foo/; then it will work on all supported perl versions. slay. β¨
use Test2::Tools::Exports;
use vars qw/$foo/;
use Some::Module qw/$bar/;
# Always works
imported_ok(qw/$foo $bar/);
so like. use vars is the cheat code for old perls. iykyk π
π₯ SOURCE π₯
the source code repository for Test2-Suite can be found at: https://github.com/Test-More/test-more/
go look at it. main character repo behavior. π
π MAINTAINERS π
- Chad Granum
<exodist@cpan.org>
π AUTHORS π
- Chad Granum
<exodist@cpan.org>
π COPYRIGHT π
Copyright Chad Granum <exodist@cpan.org>.
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
see http://dev.perl.org/licenses/
and that's the whole page bestie!! π this one shipped as part of perl v5.44.0, dated 2026-10-17. thank you for coming to my ted talk, go write some tests, drink water, and remember: imported_ok is not a vibe check on where the symbol came from, it's just a vibe check on whether it's there. mwah ππ«Άβ¨
π¦ where this page comes from
- arch rolling: perl 5.44.0-1 Β· Artistic-1.0-Perl AND GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms