More than you ever wanted to know about Perl 5's 
Exporter modules 
Neil Bowers 
NEILB 
neil@bowers.com
44+ modules 
on CPAN!
Function library modules 
use Maths qw/ min /; 
$min = min(@values); 
package Maths; 
use parent 'Exporter'; 
sub min { ... } 
package Exporter; 
... 
sub import {...} 
You're writing 
one of these
Exporter.pm 
package Maths; 
use parent 'Exporter'; 
our @EXPORT = qw/ min max average /; 
our @EXPORT_OK = qw/ sin cos tan /; 
our %EXPORT_TAGS = ( 
list => [qw/ min max average /], 
trig => [qw/ sin cos tan /], 
); 
use Maths qw/ :list /; 
$minimum = min(@numbers);
My model for function libraries 
• Only functions exported 
• Don't export variables 
• No functions exported by default 
• You have to ask for ask for everything you want 
• No (unexpected) namespace pollution 
• Documents where functions in your code come from (but: tags)
Function::Exporter
What else might you want? 
• Default exports 
• Exporting variables 
• Different (better?) notation for specifying what and how to export 
• Tags 
• Constants (creating module which define & export them) 
• Import control 
• Anonymous subs (on import & export) 
• Generators (aka currying) 
• Combined class & function module 
• Create a mashup module (exporting things from other modules)
Default exports 
Specifying what users of your module get when they 
don't explicitly list anything for import
Exporter::Auto 
• All public functions in module exported by default 
• Doesn't export functions that start with an underscore 
• Namespace pollution
Simpler than Exporter.pm 
When you want more than a completely minimal 
exporter, but not as much as Exporter
Exporter::Lite 
• A lightweight subset of the classic Exporter functionality 
• Adds the import() function into your namespace 
• No support for tags, or other features beyond the above
Exporter::Easy 
• Basic usage is clean and simple 
• Has nice way to build up tags ...
Exporter::Shiny 
• Simple interface for optional exports: 
• Exporter::Shiny is syntactic sugar around Exporter::Tiny
Tags 
Defining named groups of the symbols available for 
import
Exporter::Easy 
• Good if you've got a lot of functions / groups 
• That's a lot of punctuation though ...
Exporter::Easiest
Exporting variables
Don't export variables! 
• http://c2.com/cgi/wiki?GlobalVariablesAreBad 
• http://programmers.stackexchange.com/questions/14810 
8/why-is-global-state-so-evil 
• and 
• and 
• and
Constants 
Making it easy to create modules that define and export 
constants, for example ones that are system-wide
Exporter + constant
Panda::Export 
• Function::Exporter + constants, in XS
Constant::Exporter 
• See also 
also supports 
defining of tags 
• Const::Exporter – exports immutable variables too (deps++) 
• Exporter::Constants – support module for Exporter.pm
Constant::Export::Lazy 
• Where the value of a constant might take a long time to 
compute, look up in a database, etc
Import control 
More flexible ways for your module's users to specify 
which symbols to import, and as what
Exporter 
• The import list is basically a query, read left to right 
• You can use a regexp to match what to import 
• And prefix with ! to negate a clause
Exporter::Tiny
Anonymous functions 
Exporting from, and import to, function references
Exporter::AutoClean 
• Export anonymous subs 
• People can import, but not dip into your namespace
Exporter::Tiny 
• Import into a scalar reference 
• Doesn't pollute your namespace 
• For example, if you're making all your public functions exportable
Alternate notations 
Other ways to specify what bits of your module to export
Attribute::Exporter 
• Attributes used to annotate each function with export 
rules 
• Good: export status obvious when you look at each function 
• Bad: verbose / noisy 
• See also: Exporter::Simple, Perl6::Export::Attrs
Exporter::NoWork
Export::Above 
• Export config spread through file, but not next to each func
Exporter::Declare 
• Declarative syntax, Moose stylee
Exporter::Declare 
• Or you can put the export status by each sub: 
• Or you can export an anonymous sub:
Exporter::Declare - tags 
• You can define tags up front: 
• Or specify the tagging with each sub:
Exporter::Declare 
• You can rename things when you import them: 
• And a bunch more features. Too many perhaps? 
• Like Moose/Moo, potential for a lightweight subset?
Exporter::Declare::Magic 
• Syntactic sugar for Exporter::Declare 
• But: depends on 18 CPAN distributions
Hybrid modules 
Creating modules that can be used either as a class, or 
as a function library
Class::Exporter
Making mashup modules 
Create a module that exports things from multiple other 
modules
Exporter::Cluster 
• Create module which export things from other modules
Import::Base
Generators, or currying 
Fixing arguments to functions at import time
Sub::Exporter 
• Basic usage – give list of subs for optional export 
• Defines a :all/-all tag 
• You can rename on import 
• The real power is in export generators though...
Sub::Exporter
Using a Sub::Exporter module 
• When importing, you can configure what sub you want 
• Adds an overhead to all sub calls 
• I've recently hit a case where this might be the right 
solution 
• How many users of the module are using generators...?
SEE ALSO 1/2 
• Badger::Exporter – Exporter + constants + hooks ++ 
• Sub::Exporter::Progressive – same interface as 
Sub::Exporter, which is only loaded if advanced features 
are used, otherwise uses Exporter 
• Exporter::LexicalVars – create a module that exports 
lexical variables (!) 
• Exporter::Proxy – like my Function::Exporter, but exports 
the whole typeglob and adds exports() for reflection
SEE ALSO 2/2 
• Exporter::Renaming – monkey-patches Exporter to 
provide a renaming capability 
• Export::Lexical, Exporter::Lexical – subs imported into 
lexical scope 
• Sub::Exporter::Simple – syntactic sugar for basic 
exporting with Sub::Exporter (just use Exporter[::Tiny]) 
• Sub::Import – Sub::Exporter-like semantics, importing 
from any module
Dependencies: the good
Dependencies: the bad & the ugly
Reverse dependencies
Observations 
• Documentation of exporter modules is often sub-optimal 
• Particularly for people writing their first exporting module 
• Very few modules make use of advanced features 
• Very few modules document their import features 
• If your module provides advanced import features, you need to let 
your users know 
• There's only one exporter module shipped with Perl 
• There are gaps above and below it
Conclusion 
• Exporter::Tiny is a good default for most users 
• Or Exporter if you don't want the non-core dependency 
• Exporter::Easy if you've got rich tag needs 
• Would be nice if Exporter::Tiny supported nested tags 
• Exporting constants? 
• Panda::Export for &CONSTANTS 
• Const::Fast + Exporter::Tiny for immutable variables 
• Misc others: 
• Import::Base for mashup modules 
• Sub::Exporter for generators 
• Export::AutoClean / Exporter::Tiny for namespace OCD types 
• Yoda says: 
• variables export you should not 
• OO or functions: pick one you should

CPAN Exporter modules for Perl 5

  • 1.
    More than youever wanted to know about Perl 5's Exporter modules Neil Bowers NEILB [email protected]
  • 2.
  • 3.
    Function library modules use Maths qw/ min /; $min = min(@values); package Maths; use parent 'Exporter'; sub min { ... } package Exporter; ... sub import {...} You're writing one of these
  • 4.
    Exporter.pm package Maths; use parent 'Exporter'; our @EXPORT = qw/ min max average /; our @EXPORT_OK = qw/ sin cos tan /; our %EXPORT_TAGS = ( list => [qw/ min max average /], trig => [qw/ sin cos tan /], ); use Maths qw/ :list /; $minimum = min(@numbers);
  • 5.
    My model forfunction libraries • Only functions exported • Don't export variables • No functions exported by default • You have to ask for ask for everything you want • No (unexpected) namespace pollution • Documents where functions in your code come from (but: tags)
  • 6.
  • 7.
    What else mightyou want? • Default exports • Exporting variables • Different (better?) notation for specifying what and how to export • Tags • Constants (creating module which define & export them) • Import control • Anonymous subs (on import & export) • Generators (aka currying) • Combined class & function module • Create a mashup module (exporting things from other modules)
  • 8.
    Default exports Specifyingwhat users of your module get when they don't explicitly list anything for import
  • 9.
    Exporter::Auto • Allpublic functions in module exported by default • Doesn't export functions that start with an underscore • Namespace pollution
  • 10.
    Simpler than Exporter.pm When you want more than a completely minimal exporter, but not as much as Exporter
  • 11.
    Exporter::Lite • Alightweight subset of the classic Exporter functionality • Adds the import() function into your namespace • No support for tags, or other features beyond the above
  • 12.
    Exporter::Easy • Basicusage is clean and simple • Has nice way to build up tags ...
  • 13.
    Exporter::Shiny • Simpleinterface for optional exports: • Exporter::Shiny is syntactic sugar around Exporter::Tiny
  • 14.
    Tags Defining namedgroups of the symbols available for import
  • 15.
    Exporter::Easy • Goodif you've got a lot of functions / groups • That's a lot of punctuation though ...
  • 16.
  • 17.
  • 18.
    Don't export variables! • http://c2.com/cgi/wiki?GlobalVariablesAreBad • http://programmers.stackexchange.com/questions/14810 8/why-is-global-state-so-evil • and • and • and
  • 19.
    Constants Making iteasy to create modules that define and export constants, for example ones that are system-wide
  • 20.
  • 21.
  • 22.
    Constant::Exporter • Seealso also supports defining of tags • Const::Exporter – exports immutable variables too (deps++) • Exporter::Constants – support module for Exporter.pm
  • 23.
    Constant::Export::Lazy • Wherethe value of a constant might take a long time to compute, look up in a database, etc
  • 24.
    Import control Moreflexible ways for your module's users to specify which symbols to import, and as what
  • 25.
    Exporter • Theimport list is basically a query, read left to right • You can use a regexp to match what to import • And prefix with ! to negate a clause
  • 26.
  • 27.
    Anonymous functions Exportingfrom, and import to, function references
  • 28.
    Exporter::AutoClean • Exportanonymous subs • People can import, but not dip into your namespace
  • 29.
    Exporter::Tiny • Importinto a scalar reference • Doesn't pollute your namespace • For example, if you're making all your public functions exportable
  • 30.
    Alternate notations Otherways to specify what bits of your module to export
  • 31.
    Attribute::Exporter • Attributesused to annotate each function with export rules • Good: export status obvious when you look at each function • Bad: verbose / noisy • See also: Exporter::Simple, Perl6::Export::Attrs
  • 32.
  • 33.
    Export::Above • Exportconfig spread through file, but not next to each func
  • 34.
  • 35.
    Exporter::Declare • Oryou can put the export status by each sub: • Or you can export an anonymous sub:
  • 36.
    Exporter::Declare - tags • You can define tags up front: • Or specify the tagging with each sub:
  • 37.
    Exporter::Declare • Youcan rename things when you import them: • And a bunch more features. Too many perhaps? • Like Moose/Moo, potential for a lightweight subset?
  • 38.
    Exporter::Declare::Magic • Syntacticsugar for Exporter::Declare • But: depends on 18 CPAN distributions
  • 39.
    Hybrid modules Creatingmodules that can be used either as a class, or as a function library
  • 40.
  • 41.
    Making mashup modules Create a module that exports things from multiple other modules
  • 42.
    Exporter::Cluster • Createmodule which export things from other modules
  • 43.
  • 44.
    Generators, or currying Fixing arguments to functions at import time
  • 45.
    Sub::Exporter • Basicusage – give list of subs for optional export • Defines a :all/-all tag • You can rename on import • The real power is in export generators though...
  • 46.
  • 47.
    Using a Sub::Exportermodule • When importing, you can configure what sub you want • Adds an overhead to all sub calls • I've recently hit a case where this might be the right solution • How many users of the module are using generators...?
  • 48.
    SEE ALSO 1/2 • Badger::Exporter – Exporter + constants + hooks ++ • Sub::Exporter::Progressive – same interface as Sub::Exporter, which is only loaded if advanced features are used, otherwise uses Exporter • Exporter::LexicalVars – create a module that exports lexical variables (!) • Exporter::Proxy – like my Function::Exporter, but exports the whole typeglob and adds exports() for reflection
  • 49.
    SEE ALSO 2/2 • Exporter::Renaming – monkey-patches Exporter to provide a renaming capability • Export::Lexical, Exporter::Lexical – subs imported into lexical scope • Sub::Exporter::Simple – syntactic sugar for basic exporting with Sub::Exporter (just use Exporter[::Tiny]) • Sub::Import – Sub::Exporter-like semantics, importing from any module
  • 50.
  • 51.
  • 52.
  • 53.
    Observations • Documentationof exporter modules is often sub-optimal • Particularly for people writing their first exporting module • Very few modules make use of advanced features • Very few modules document their import features • If your module provides advanced import features, you need to let your users know • There's only one exporter module shipped with Perl • There are gaps above and below it
  • 54.
    Conclusion • Exporter::Tinyis a good default for most users • Or Exporter if you don't want the non-core dependency • Exporter::Easy if you've got rich tag needs • Would be nice if Exporter::Tiny supported nested tags • Exporting constants? • Panda::Export for &CONSTANTS • Const::Fast + Exporter::Tiny for immutable variables • Misc others: • Import::Base for mashup modules • Sub::Exporter for generators • Export::AutoClean / Exporter::Tiny for namespace OCD types • Yoda says: • variables export you should not • OO or functions: pick one you should