TEST2::TOOLS::EXPORTS(3pm)

πŸ’… Test2::Tools::Exports β€” from the Perl Programmers Reference Guide (3pm section, bestie, we're fancy)

okay so picture this: you just used a module, and now you're squinting at your namespace like "did that actually import anything or am I being gaslit rn??" That's literally the whole job of this page. Welcome to Test2::Tools::Exports. 🫢


✨ NAME ✨

Test2::Tools::Exports β€” tools for validating exporters. Yes, that's it, that's the vibe. πŸŽ€


🌸 DESCRIPTION 🌸

These are tools for checking that symbols have actually been imported into your namespace. Like, actually actually. No cap. It's the vibe check your use statements deserve. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

use Test2::Tools::Exports

use Data::Dumper;
imported_ok qw/Dumper/;
not_imported_ok qw/dumper/;

See how one is Dumper and the other is dumper? Case matters, bestie, Perl is not doing your lowercase homework for you. 😭


πŸ’– EXPORTS πŸ’–

All subs are exported by default. Every single one, no gatekeeping. πŸ‘‘

🌟 imported_ok(@SYMBOLS)

Checks that the specified symbols exist in the current package. So, like, a few things it is not doing, and I need you to hear me on this:

  • It will not find inherited subs.
  • It 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. (Yeah β€” it just proves the name is sitting there in your symbol table. Could be from anywhere. Could be from you. It's not doing a background check. πŸ™ˆ)

The example, exactly as written:

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. Here's the rule:

  • If a sigil is present β†’ it will search for that specified type.
  • If no sigil is specified β†’ it will be used as a sub name.

Slay, sigil-based dispatch, we love to see it. ✨

🌟 not_imported_ok(@SYMBOLS)

The inverse queen. Checks that the specified symbols do not exist in the current package. Again:

  • This will not find inherited subs.
  • This will only look at symbols in the current package's symbol table.

The example, verbatim:

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. And the sigil rules are the same deal:

  • If a sigil is present β†’ it will search for that specified type.
  • If no sigil is specified β†’ it will be used as a sub name.

Consistency is the moment. 🧚


πŸ’€ CAVEATS πŸ’€ (the dramatic part, sorry)

Before Perl 5.10, it is very difficult to distinguish between a package scalar that is undeclared vs. declared and undefined. Like, genuinely cursed territory. πŸ•―οΈ

Currently, imported_ok and not_imported_ok cannot see package scalars declared using our $var unless the variable has been assigned a defined value. So if it's declared-but-empty, the tools are basically squinting into the void. 😭

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's the comment exactly as it appears in the docs, typo and all, I don't make the rules πŸ™ˆ)

BUT. If $foo is imported from another module, or imported using use vars qw/$foo/;, then it will work on all supported perl versions. Redemption arc. 🌈

use Test2::Tools::Exports;

use vars qw/$foo/;
use Some::Module qw/$bar/;

# Always works
imported_ok(qw/$foo $bar/);

πŸ”₯ SOURCE πŸ”₯

The source code repository for Test2-Suite can be found at https://github.com/Test-More/test-more/. Go lurk, it's fine, we all do it. πŸ‘€


πŸ‘‘ MAINTAINERS πŸ‘‘

Chad Granum <exodist@cpan.org>


⭐ AUTHORS ⭐

Chad Granum <exodist@cpan.org>


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/


okay that's the whole page, you ate that πŸ’… β€” this was Test2::...:Exports(3pm) from the Perl Programmers Reference Guide, with the footer reading perl v5.42.3 dated 2026-11-03. mwah, go write some tests bestie πŸ’œβœ¨

πŸ“¦ where this page comes from