++ed by:
FARACO MHOWARD ABRAXXA MDOM NEYASOV

65 PAUSE users
46 non-PAUSE users.

Toby Inkster
and 4 contributors

NAME

Exporter::TypeTiny - a small exporter used internally by Type::Library and friends

SYNOPSIS

   package MyUtils;
   use base "Exporter::TypeTiny";
   our @EXPORT = qw(frobnicate);
   sub frobnicate { my $n = shift; ... }
   1;

   package MyScript;
   use MyUtils "frobnicate" => { -as => "frob" };
   print frob(42);
   exit;

DESCRIPTION

Exporter::TypeTiny supports many of Sub::Exporter's external-facing features including renaming imported functions with the -as, -prefix and -suffix options; explicit destinations with the into option; and alternative installers with the installler option. But it's written in only about 40% as many lines of code and with zero non-core dependencies.

Its internal-facing interface is closer to Exporter.pm, with configuration done through the @EXPORT, @EXPORT_OK and %EXPORT_TAGS package variables.

Although generators are not an explicit part of the interface, Exporter::TypeTiny performs most of its internal duties (including resolution of tag names to function names, resolution of function names to coderefs, and installation of coderefs into the target package) as method calls, which means they can be overridden to provide interesting behaviour, including an equivalent to Sub::Exporter's generators. (Type::Library does this.) These methods are not currently documented, and are still subject to change.

Utility Functions

These are really for internal use, but can be exported if you need them.

mkopt(\@array)

Similar to mkopt from Data::OptList. It doesn't support all the fancy options that Data::OptList does (moniker, require_unique, must_be and name_test) but runs about 50% faster.

mkopt_hash(\@array)

Similar to mkopt_hash from Data::OptList. See also mkopt.

TIPS AND TRICKS

For the purposes of this discussion we'll assume we have a module called MyUtils which exports one function, frobnicate. MyUtils inherits from Exporter::TypeTiny.

Many of these tricks may seem familiar from Sub::Exporter. That is intentional. Exporter::TypeTiny doesn't attempt to provide every feature of Sub::Exporter, but where it does it usually uses a fairly similar API.

Basic importing

   # import "frobnicate" function
   use MyUtils "frobnicate";

   # import all functions that MyUtils offers
   use MyUtils -all;

Renaming imported functions

   # call it "frob"
   use MyUtils "frobnicate" => { -as => "frob" };

   # call it "my_frobnicate"
   use MyUtils "frobnicate" => { -prefix => "my_" };

   # call it "frobnicate_util"
   use MyUtils "frobnicate" => { -suffix => "_util" };

   # import it twice with two different names
   use MyUtils
      "frobnicate" => { -as => "frob" },
      "frobnicate" => { -as => "frbnct" };

Lexical subs

   {
      use Sub::Exporter::Lexical lexical_installer => { -as => "lex" };
      use MyUtils { installer => lex }, "frobnicate";
      
      frobnicate(...);  # ok
   }
   
   frobnicate(...);  # not ok

Import functions into another package

   use MyUtils { into => "OtherPkg" }, "frobnicate";
   
   OtherPkg::frobincate(...);

Import functions into a scalar

   my $func;
   use MyUtils "frobnicate" => { -as => \$func };
   
   $func->(...);

Import functions into a hash

OK, Sub::Exporter doesn't do this...

   my %funcs;
   use MyUtils { into => \%funcs }, "frobnicate";
   
   $funcs{frobnicate}->(...);

HISTORY

Why bundle an exporter with Type-Tiny?

Well, it wasn't always that way. Type::Library had a bunch of custom exporting code which poked coderefs into its caller's stash. It needed this so that it could switch between exporting Moose, Mouse and Moo-compatible objects on request.

Meanwhile Type::Utils, Types::TypeTiny and Test::TypeTiny each used the venerable Exporter.pm. However, this meant they were unable to use the features like Sub::Exporter-style function renaming which I'd built into Type::Library:

   ## import "Str" but rename it to "String".
   use Types::Standard "Str" => { -as => "String" };

And so I decided to factor out code that could be shared by all Type-Tiny's exporters into a single place.

OBLIGATORY EXPORTER COMPARISON

Exporting is unlikely to be your application's performance bottleneck, but nonetheless here are some comparisons.

Comparative sizes according to Devel::SizeMe:

   Exporter                     217.1Kb
   Sub::Exporter::Progressive   263.2Kb
   Exporter::TypeTiny           267.7Kb
   Exporter + Exporter::Heavy   281.5Kb
   Exporter::Renaming           406.2Kb
   Sub::Exporter                701.0Kb

Performance exporting a single sub:

              Rate     SubExp      ExpTT SubExpProg      ExpPM
SubExp      2489/s         --       -56%       -85%       -88%
ExpTT       5635/s       126%         --       -67%       -72%
SubExpProg 16905/s       579%       200%         --       -16%
ExpPM      20097/s       707%       257%        19%         --

(Exporter::Renaming globally changes the behaviour of Exporter.pm, so could not be included in the same benchmarks.)

(Non-Core) Depenendencies:

   Exporter                    -1
   Exporter::Renaming           0
   Exporter::TypeTiny           0
   Sub::Exporter::Progressive   0   
   Sub::Exporter                3

Features:

                                      ExpPM   ExpTT   SubExp  SubExpProg
 Can export code symbols............. Yes     Yes     Yes     Yes      
 Can export non-code symbols......... Yes                              
 Groups/tags......................... Yes     Yes     Yes     Yes      
 Config avoids package variables.....                 Yes              
 Allows renaming of subs.............         Yes     Yes     Maybe    
 Install code into scalar refs.......         Yes     Yes     Maybe    
 Can be passed an "into" parameter...         Yes     Yes     Maybe    
 Can be passed an "installer" sub....         Yes     Yes     Maybe    
 Supports generators.................         Yes     Yes              
 Sane API for generators.............                 Yes              

(Certain Sub::Exporter::Progressive features are only available if Sub::Exporter is installed.)

BUGS

Please report any bugs to http://rt.cpan.org/Dist/Display.html?Queue=Type-Tiny.

SEE ALSO

Type::Library.

Exporter, Sub::Exporter, Sub::Exporter::Progressive.

AUTHOR

Toby Inkster <tobyink@cpan.org>.

COPYRIGHT AND LICENCE

This software is copyright (c) 2013 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.