CPAN Exporter modules for Perl 5

54
Exporter modules Neil Bowers NEILB [email protected] More than you ever wanted to know about Perl 5's

description

A review of CPAN exporter modules for Perl 5, when and where you should use them, and things to be aware of.

Transcript of CPAN Exporter modules for Perl 5

Page 1: CPAN Exporter modules for Perl 5

Exporter modules

Neil Bowers

NEILB

[email protected]

More than you ever wanted to know about Perl 5's

Page 2: CPAN Exporter modules for Perl 5

44+ modules

on CPAN!

Page 3: CPAN Exporter modules for Perl 5

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

Page 4: CPAN Exporter modules for Perl 5

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);

Page 5: CPAN Exporter modules for Perl 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)

Page 6: CPAN Exporter modules for Perl 5

Function::Exporter

Page 7: CPAN Exporter modules for Perl 5

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)

Page 8: CPAN Exporter modules for Perl 5

Default exports

Specifying what users of your module get when they

don't explicitly list anything for import

Page 9: CPAN Exporter modules for Perl 5

Exporter::Auto

• Doesn't export functions that start with an underscore

• Namespace pollution

• All public functions in module exported by default

Page 10: CPAN Exporter modules for Perl 5

Simpler than Exporter.pm

When you want more than a completely minimal

exporter, but not as much as Exporter

Page 11: CPAN Exporter modules for Perl 5

Exporter::Lite

• Adds the import() function into your namespace

• No support for tags, or other features beyond the above

• A lightweight subset of the classic Exporter functionality

Page 12: CPAN Exporter modules for Perl 5

Exporter::Easy

• Basic usage is clean and simple

• Has nice way to build up tags ...

Page 13: CPAN Exporter modules for Perl 5

Exporter::Shiny

• Exporter::Shiny is syntactic sugar around Exporter::Tiny

• Simple interface for optional exports:

Page 14: CPAN Exporter modules for Perl 5

Tags

Defining named groups of the symbols available for

import

Page 15: CPAN Exporter modules for Perl 5

Exporter::Easy

• Good if you've got a lot of functions / groups

• That's a lot of punctuation though ...

Page 16: CPAN Exporter modules for Perl 5

Exporter::Easiest

Page 17: CPAN Exporter modules for Perl 5

Exporting variables

Page 18: CPAN Exporter modules for Perl 5

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

Page 19: CPAN Exporter modules for Perl 5

Constants

Making it easy to create modules that define and export

constants, for example ones that are system-wide

Page 20: CPAN Exporter modules for Perl 5

Exporter + constant

Page 21: CPAN Exporter modules for Perl 5

Panda::Export

• Function::Exporter + constants, in XS

Page 22: CPAN Exporter modules for Perl 5

Constant::Exporter

• See also• Const::Exporter – exports immutable variables too (deps++)

• Exporter::Constants – support module for Exporter.pm

also supportsdefining of tags

Page 23: CPAN Exporter modules for Perl 5

Constant::Export::Lazy

• Where the value of a constant might take a long time to

compute, look up in a database, etc

Page 24: CPAN Exporter modules for Perl 5

Import control

More flexible ways for your module's users to specify

which symbols to import, and as what

Page 25: CPAN Exporter modules for Perl 5

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

Page 26: CPAN Exporter modules for Perl 5

Exporter::Tiny

Page 27: CPAN Exporter modules for Perl 5

Anonymous functions

Exporting from, and import to, function references

Page 28: CPAN Exporter modules for Perl 5

Exporter::AutoClean

• Export anonymous subs

• People can import, but not dip into your namespace

Page 29: CPAN Exporter modules for Perl 5

Exporter::Tiny

• Import into a scalar reference

• Doesn't pollute your namespace• For example, if you're making all your public functions exportable

Page 30: CPAN Exporter modules for Perl 5

Alternate notations

Other ways to specify what bits of your module to export

Page 31: CPAN Exporter modules for Perl 5

Attribute::Exporter

• Good: export status obvious when you look at each function

• Bad: verbose / noisy

• See also: Exporter::Simple, Perl6::Export::Attrs

• Attributes used to annotate each function with export

rules

Page 32: CPAN Exporter modules for Perl 5

Exporter::NoWork

Page 33: CPAN Exporter modules for Perl 5

Export::Above

• Export config spread through file, but not next to each func

Page 34: CPAN Exporter modules for Perl 5

Exporter::Declare

• Declarative syntax, Moose stylee

Page 35: CPAN Exporter modules for Perl 5

Exporter::Declare

• Or you can put the export status by each sub:

• Or you can export an anonymous sub:

Page 36: CPAN Exporter modules for Perl 5

Exporter::Declare - tags

• You can define tags up front:

• Or specify the tagging with each sub:

Page 37: CPAN Exporter modules for Perl 5

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?

Page 38: CPAN Exporter modules for Perl 5

Exporter::Declare::Magic

• Syntactic sugar for Exporter::Declare

• But: depends on 18 CPAN distributions

Page 39: CPAN Exporter modules for Perl 5

Hybrid modules

Creating modules that can be used either as a class, or

as a function library

Page 40: CPAN Exporter modules for Perl 5

Class::Exporter

Page 41: CPAN Exporter modules for Perl 5

Making mashup modules

Create a module that exports things from multiple other

modules

Page 42: CPAN Exporter modules for Perl 5

Exporter::Cluster

• Create module which export things from other modules

Page 43: CPAN Exporter modules for Perl 5

Import::Base

Page 44: CPAN Exporter modules for Perl 5

Generators, or currying

Fixing arguments to functions at import time

Page 45: CPAN Exporter modules for Perl 5

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...

Page 46: CPAN Exporter modules for Perl 5

Sub::Exporter

Page 47: CPAN Exporter modules for Perl 5

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...?

Page 48: CPAN Exporter modules for Perl 5

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

Page 49: CPAN Exporter modules for Perl 5

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

Page 50: CPAN Exporter modules for Perl 5

Dependencies: the good

Page 51: CPAN Exporter modules for Perl 5

Dependencies: the bad & the ugly

Page 52: CPAN Exporter modules for Perl 5

Reverse dependencies

Page 53: CPAN Exporter modules for Perl 5

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

Page 54: CPAN Exporter modules for Perl 5

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