perldoc > Exporter::Tiny::Manual::Exporting

📖 NAME

Exporter::Tiny::Manual::Exporting — creating an exporter using Exporter::Tiny

🚀 Quick Reference

Use CaseCommandDescription
🧩 Simple exporteruse parent qw(Exporter::Tiny); our @EXPORT = qw(foo);Inherit and list subs to export
🏷️ Export tagsour %EXPORT_TAGS = (rgb => [qw(red green blue)]);Group exports under named tags
⚙️ Generated subssub _generate_foo { ... }Dynamically create exported subroutines
🪝 Validation hookssub _exporter_validate_opts { ... }Pre‑export validation / setup
🛠️ Override internalssub _exporter_expand_tag { ... }Customise tag expansion, sub resolution, installation

📝 SYNOPSIS

Read Exporter::Tiny::Manual::QuickStart first!

📚 DESCRIPTION

Simple configuration works the same as Exporter; inherit from Exporter::Tiny, and use the @EXPORT, @EXPORT_OK, and %EXPORT_TAGS package variables to list subs to export.

Unlike Exporter, Exporter::Tiny performs most of its internal duties (including resolution of tag names to sub names, resolution of sub names to coderefs, and installation of coderefs into the target package) as method calls, which means that your module (which is a subclass of Exporter::Tiny) can override them to provide interesting behaviour.

🔧 Advanced Tag Stuff

You can define tags using other tags:

   use Exporter::Shiny qw(
      black white red green blue cyan magenta yellow
   );

   our %EXPORT_TAGS = (
      rgb        => [qw( red green blue )],
      cym        => [qw( cyan magenta yellow )],
      cymk       => [qw( black :cym )],
      monochrome => [qw( black white )],
      all        => [qw( :rgb :cymk :monochrome )],
   );

CAVEAT: If you create a cycle in the tags, this could put Exporter::Tiny into an infinite loop expanding the tags. Don't do that.

⚙️ More on Generators

Exporter::Tiny has always allowed exported subs to be generated (like Sub::Exporter), but until version 0.025 did not have an especially nice API for it.

Now, it's easy. If you want to generate a sub foo to export, list it in @EXPORT or @EXPORT_OK as usual, and then simply give your exporter module a class method called _generate_foo.

   push @EXPORT_OK, 'foo';

   sub _generate_foo {
      my $class = shift;
      my ($name, $args, $globals) = @_;

      return sub {
         ...;
      }
   }

We showed how to do that in Exporter::Tiny::Manual::QuickStart, but one thing we didn't show was that $globals gets passed in there. This is the global options hash, as described in Exporter::Tiny::Manual::Importing. It can often be useful. In particular it will tell you what package the generated sub is destined to be installed into.

To generate non‑code symbols, name your generators like this:

  sub _generateScalar_Foo { ... }  # generate a symbol $Foo
  sub _generateArray_Bar  { ... }  # generate a symbol @Bar
  sub _generateHash_Baz   { ... }  # generate a symbol %Baz

You can also generate tags:

   my %constants;
   BEGIN {
      %constants = (FOO => 1, BAR => 2);
   }
   use constant \%constants;

   $EXPORT_TAGS{constants} = sub {
      my $class = shift;
      my ($name, $args, $globals) = @_;

      return keys(%constants);
   };

🪝 Hooks

Sometimes as well as exporting stuff, you want to do some setup or something.

You can define a couple of class methods in your package, and they'll get called at the appropriate time:

   package MyUtils;

   ...;

   sub _exporter_validate_opts {
      my $class = shift;
      my ($globals) = @_;

      ...;   # do stuff here

      $class->SUPER::_exporter_validate_opts(@_);
   }

   sub _exporter_validate_unimport_opts {
      my $class = shift;
      my ($globals) = @_;

      ...;   # do stuff here

      $class->SUPER::_exporter_validate_unimport_opts(@_);
   }

The $globals variable is that famous global options hash. In particular, $globals->{into} is useful because it tells you what package has imported you.

As you might have guessed, these methods were originally intended to validate the global options hash, but can be used to perform any general duties before the real exporting work is done.

🛠️ Overriding Internals

An important difference between Exporter and Exporter::Tiny is that the latter calls all its internal functions as class methods. This means that your subclass can override them to alter their behaviour.

The following methods are available to be overridden. Despite being named with a leading underscore, they are considered public methods. (The underscore is there to avoid accidentally colliding with any of your own function names.)

👀 SEE ALSO

Exporter::Shiny, Exporter::Tiny.

👤 AUTHOR

Toby Inkster <tobyink AT cpan.org>.

📄 COPYRIGHT AND LICENCE

This software is copyright (c) 2013‑2014, 2017 by Toby Inkster.

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

⚠️ DISCLAIMER OF WARRANTIES

THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.

Exporter::Tiny::Manual::Exporting
📖 NAME 🚀 Quick Reference 📝 SYNOPSIS 📚 DESCRIPTION
🔧 Advanced Tag Stuff ⚙️ More on Generators 🪝 Hooks 🛠️ Overriding Internals
👀 SEE ALSO 👤 AUTHOR 📄 COPYRIGHT AND LICENCE ⚠️ DISCLAIMER OF WARRANTIES

Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-16 11:15 @216.73.216.208
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 Transitional!Valid CSS!

^_top_^