# Import defaults
use Importer 'Some::Module';
# Import a list
use Importer 'Another::Module' => qw/foo bar baz/;
# Import a specific version:
use Importer 'That::Module' => '1.00';
# Require a sepcific version of Importer
use Importer 0.001, 'Foo::Bar' => qw/a b c/;
foo()
bar()
baz()
# Remove all subroutines imported by Importer
no Importer;
# Import symbols into variables
my $croak = Importer->get_one(Carp => qw/croak/);
$croak->("This will croak");
my $CARP = Importer->get(Carp => qw/croak confess cluck/);
$CARP->{croak}->("This will croak");
$CARP->{cluck}->("This will cluck");
$CARP->{confess}->("This will confess");
In addition, moving forward, modules can specify exports and have a custom "import()" without conflating the two. A module can tell you to use Importer to get the symbols, and to use the module directly for behaviors. A module could also use Importer within its own "import()" method without the need to subclass Exporter, or bring in its "import()" method.
If you find something that works differently, or not at all when compared to Exporter please report it as a bug, unless it is noted as an intentional feature (like import renaming).
use Importer $IMPORTER_VERSION, $FROM_MODULE, $FROM_MODULE_VERSION, \&SET_SYMBOL, @SYMBOLS;
This is an example that uses this feature to put all the exports into a lexical hash instead of modifying the namespace (This is how the "get()" method is implemented).
my %CARP;
use Importer Carp => sub {
my ($name, $ref) = @_;
$CARP{$name} = $ref;
};
$CARP{cluck}->("This will cluck");
$CARP{croak}->("This will croak");
The first two arguments to the custom sub are the name (no sigil), and the reference. The additional arguments are key/value pairs:
sub set_symbol {
my ($name, $ref, %info) = @_;
}
use Importer 'Some::Thing' => ':tag';
Importer will automatically populate the ":DEFAULT" tag for you. Importer will also give you an ":ALL" tag with ALL exports so long as the exporter does not define a ":ALL" tag already.
use Importer 'Some::Thing' => '/oo/';
use Importer 'Some::Thing' => qr/oo/;
use Importer 'Some::Thing'
'!foo', # Exclude one specific symbol
'!/pattern/', # Exclude all matching symbols
'!' => qr/oo/, # Exclude all that match the following arg
'!:tag'; # Exclude all in tag
You can rename symbols at import time using a specification hash following the import name:
use Importer 'Some::Thing' => (
foo => { -as => 'my_foo' },
);
You can also add a prefix and/or postfix:
use Importer 'Some::Thing' => (
foo => { -prefix => 'my_' },
);
Using this syntax to set prefix and/or postfix also works on tags and patterns that are specified for import, in which case the prefix/postfix is applied to all symbols from the tag/patterm.
no Importer; # Remove all subs brought in with Importer
no Importer qw/foo bar/; # Remove only the specified subs
Only subs can be unimported.
You can only unimport subs imported using Importer.
List of symbols to export. Sigil is optional for subs. Symbols listed here are exported by default. If possible you should put symbols in @EXPORT_OK instead.
our @EXPORT = qw/foo bar &baz $BAT/;
List of symbols that can be imported. Sigil is optional for subs. Symbols listed here are not exported by default. This is preferred over @EXPORT.
our @EXPORT_OK = qw/foo bar &baz $BAT/;
use Importer 'Some::Thing' => ':DEFAULT';
use Importer 'Other::Thing' => ':some_tag';
Tags can be specified this way:
our %EXPORT_TAGS = (
oos => [qw/foo boo zoo/],
ees => [qw/fee bee zee/],
);
Use this to list subs that are not available on all platforms. If someone tries to import one of these, Importer will hit your "$from->export_fail(@items)" callback to try to resolve the issue. See Exporter for documentation of this feature.
our @EXPORT_FAIL = qw/maybe_bad/;
This allows you to export symbols that are not actually in your package symbol table. The keys should be the symbol names, the values are the references for the symbols.
our %EXPORT_ANON = (
'&foo' => sub { 'foo' }
'$foo' => \$foo,
...
);
This allows you to export symbols that are generated on export. The key should be the name of a symbol. The value should be a coderef that produces a reference that will be exported.
When the generators are called they will receive 2 arguments, the package the symbol is being exported into, and the symbol being imported (name may or may not include sigil for subs).
our %EXPORT_GEN = (
'&foo' => sub {
my $from_package = shift;
my ($into_package, $symbol_name) = @_;
...
return sub { ... };
},
...
);
This allows you to define custom actions to run AFTER an export has been injected into the consumers namespace. This is a good place to enable parser hooks like with Devel::Declare. These will NOT be run if a consumer uses a custom assignment callback.
our %EXPORT_MAGIC = (
foo => sub {
my $from = shift; # Should be the package doing the exporting
my %args = @_;
my $into = $args{into}; # Package symbol was exported into
my $orig_name = $args{orig_name}; # Original name of the export (in the exporter)
my $new_name = $args{new_name}; # Name the symbol was imported as
my $ref = $args{ref}; # The reference to the symbol
...; # whatever you want, return is ignored.
},
);
$from should be the package from which to get the exports.
$from should be the package from which to get the exports.
$from should be the package from which to get the exports.
sub IMPORTER_MENU {
my $class = shift;
my ($into, $caller) = @_;
return (
export => \@EXPORT, # Default exports
export_ok => \@EXPORT_OK, # Other allowed exports
export_tags => \%EXPORT_TAGS, # Define tags
export_fail => \@EXPORT_FAIL, # For subs that may not always be available
export_anon => \%EXPORT_ANON, # Anonymous symbols to export
export_magic => \%EXPORT_MAGIC, # Magic to apply after a symbol is exported
generate => \&GENERATE, # Sub to generate dynamic exports
# OR
export_gen => \%EXPORT_GEN, # Hash of builders, key is symbol
# name, value is sub that generates
# the symbol ref.
);
}
sub GENERATE {
my ($symbol) = @_;
...
return $ref;
}
All exports must be listed in either @EXPORT or @EXPORT_OK, or be keys in %EXPORT_GEN or %EXPORT_ANON to be allowed. 'export_tags', 'export_fail', 'export_anon', 'export_gen', and 'generate' are optional. You cannot combine 'generate' and 'export_gen'.
Note: If your GENERATE sub needs the $class, $into, or $caller then your "IMPORTER_MENU()" method will need to build an anonymous sub that closes over them:
sub IMPORTER_MENU {
my $class = shift;
my ($into, $caller) = @_;
return (
...
generate => sub { $class->GENERATE($into, $caller, @_) },
);
}
use Importer;
my $imp = Importer->new(from => 'Some::Exporter');
$imp->do_import('Destination::Package');
$imp->do_import('Another::Destination', @symbols);
Or, maybe more useful:
my $imp = Importer->new(from => 'Carp');
my $croak = $imp->get_one('croak');
$croak->("This will croak");
Note: Please be aware of the difference between "do_import()" and "do_unimport()". For import 'from' us used as the origin, in unimport it is used as the target. This means you cannot re-use an instance to import and then unimport.
The menu structure looks like this:
$menu = {
# every valid export has a key in the lookup hashref, value is always
# 1, key always includes the sigil
lookup => {'&symbol_a' => 1, '$symbol_b' => 1, ...},
# most exports are listed here, symbol name with sigil is key, value is
# a reference to the symbol. If a symbol is missing it may be generated.
exports => {'&symbol_a' => \&symbol_a, '$symbol_b' => \$symbol_b, ...},
# Hashref of tags, tag name (without ':' prefix) is key, value is an
# arrayref of symbol names, subs may have a sigil, but are not required
# to.
tags => { DEFAULT => [...], foo => [...], ... },
# Magic to apply
magic => { foo => sub { ... }, ... },
# This is a hashref just like 'lookup'. Keys are symbols which may not
# always be available. If there are no symbols in this category then
# the value of the 'fail' key will be undef instead of a hashref.
fail => { '&iffy_symbol' => 1, '\&only_on_linux' => 1 },
# OR fail => undef,
# If present, this subroutine knows how to generate references for the
# symbols listed in 'lookup', but missing from 'exports'. References
# this returns are NEVER cached.
generate => sub { my $sym_name = shift; ...; return $symbol_ref },
};
use Importer 'Importer' => qw/import optimal_import/;
If the import is successful this will return true.
If the import is unsuccessful this will return false, and no modifications to the symbol table will occur.
package My::Exporter;
# This will give you 'import()' much like 'use base "Exporter";'
use Importer 'Importer' => qw/import/;
...
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See <http://dev.perl.org/licenses/>