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 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)
7. 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)
8. Default exports
Specifying what users of your module get when they
don't explicitly list anything for import
9. Exporter::Auto
• All public 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
• 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
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 it easy to create modules that define and export
constants, for example ones that are system-wide
22. Constant::Exporter
• See also
also supports
defining of tags
• Const::Exporter – exports immutable variables too (deps++)
• Exporter::Constants – support module for Exporter.pm
24. Import control
More flexible ways for your module's users to specify
which symbols to import, and as what
25. 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
29. Exporter::Tiny
• Import into a scalar reference
• Doesn't pollute your namespace
• For example, if you're making all your public functions exportable
31. 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
35. Exporter::Declare
• Or you 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
• You can rename things when you import them:
• And a bunch more features. Too many perhaps?
• Like Moose/Moo, potential for a lightweight subset?
45. 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...
47. 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...?
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
53. 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
54. 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