Exporter::Easy
Takes the drudgery out of Exporting symbols
- Provided by: libexporter-easy-perl (Version: 0.16-1)
- Report a bug
Takes the drudgery out of Exporting symbols
In module YourModule.pm:
package YourModule;
use Exporter::Easy (
OK => [ '$munge', 'frobnicate' ] # symbols to export on request
);
In other files which wish to use YourModule:
use ModuleName qw(frobnicate); # import listed symbols frobnicate ($left, $right) # calls YourModule::frobnicate
Exporter::Easy makes using Exporter easy. In it's simplest case it allows you to drop the boilerplate code that comes with using Exporter, so
require Exporter; use base qw( Exporter ); use vars qw( @EXPORT ); @EXPORT = ( 'init' );
becomes
use Exporter::Easy ( EXPORT => [ 'init' ] );
and more complicated situations where you use tags to build lists and more tags become easy, like this
use Exporter::Easy (
EXPORT => [qw( init :base )],
TAGS => [
base => [qw( open close )],
read => [qw( read sysread readline )],
write => [qw( print write writeline )],
misc => [qw( select flush )],
all => [qw( :base :read :write :misc)],
no_misc => [qw( :all !:misc )],
],
OK => [qw( some other stuff )],
);
This will set @EXPORT, @EXPORT_OK, @EXPORT_FAIL and %EXPORT_TAGS in the current package, add Exporter to that package's @ISA and do a "use vars" on all the variables mentioned. The rest is handled as normal by Exporter.
Put
use Exporter::Easy ( KEY => value, ...);
in your package. Arguments are passes as key-value pairs, the following keys are available
TAGS => [
file => [ 'open', 'close', 'read', 'write'],
string => [ 'length', 'substr', 'chomp' ],
hash => [ 'keys', 'values', 'each' ],
all => [ ':file', ':string', ':hash' ],
some => [':all', '!open', ':hash'],
]
This is used to fill the %EXPORT_TAGS in your package. You can build tags from other tags - in the example above the tag "all" will contain all the symbols from "file", "string" and "hash". You can also subtract symbols and tags - in the example above, "some" contains the symbols from all but with "open" removed and all the symbols from "hash" removed.
The rule is that any symbol starting with a ':' is taken to be a tag which has been defined previously (if it's not defined you'll get an error). If a symbol is preceded by a '!' it will be subtracted from the list, otherwise it is added.
If you try to redefine a tag you will also get an error.
All the symbols which occur while building the tags are automatically added your package's @EXPORT_OK array.
We need take the information provided and build @EXPORT, @EXPORT_OK, @EXPORT_FAIL and %EXPORT_TAGS in the calling package. We may also need to build a tag with all of the symbols and to make all the variables useable under strict.
The arguments are processed in the following order: TAGS, EXPORT, OK, OK_ONLY and FAIL, ALL, VARS and finally ISA. This means you cannot use the tag created by ALL anywhere except in VARS (although vars defaults to using all symbols anyway).
For details on what all these arrays and hashes actually do, see the Exporter documentation.
Written by Fergal Daly <fergal@esatclear.ie>.
Under the same license as Perl itself