Export...Declare(3pm)
โจ NAME โจ
Exporter::Declare โ Exporting done right ๐
okay bestie, this is the Export...Declare(3pm) manual page from the User Contributed Perl Documentation corner of the universe. It's giving main character energy fr fr. We're doing part 1, and ngl, it's lowkey iconic.
๐ธ DESCRIPTION ๐ธ
Exporter::Declare is a meta-driven exporting tool. It tries to adopt all the good features of other exporting tools, while throwing away horrible interfaces. It also provides hooks that allow you to add options and arguments for import. Finally, its meta-driven system allows for top-notch introspection. No cap.
๐ FEATURES ๐
- Declarative exporting (like Moose for exporting)
- Meta-driven for introspection
- Customizable
import()method - Export groups (tags)
- Export generators for subs and variables
- Clear and concise OO API
- Exports are blessed, allowing for more introspection
- Import syntax based off of
Sub::Exporter - Packages export aliases
periodt. the way that it just does all of this? obsessed.
๐ SYNOPSIS ๐
๐ง EXPORTER
package Some::Exporter;
use Exporter::Declare;
default_exports qw/ do_the_thing /;
exports qw/ subA subB $SCALAR @ARRAY %HASH /;
# Create a couple tags (import lists)
export_tag subs => qw/ subA subB do_the_thing /;
export_tag vars => qw/ $SCALAR @ARRAY %HASH /;
# These are simple boolean options, pass '-optionA' to enable it.
import_options qw/ optionA optionB /;
# These are options which slurp in the next argument as their value, pass
# '-optionC' => 'foo' to give it a value.
import_arguments qw/ optionC optionD /;
export anon_export => sub { ... };
export '@anon_var' => [...];
default_export a_default => sub { 'default!' }
our $X = "x";
default_export '$X';
my $iterator = 'a';
gen_export unique_class_id => sub {
my $current = $iterator++;
return sub { $current };
};
gen_default_export '$my_letter' => sub {
my $letter = $iterator++;
return \$letter;
};
# You can create a function to mangle the arguments before they are
# parsed into a Exporter::Declare::Spec object.
sub alter_import_args {
my ($class, $importer, $args) = @_;
# fiddle with args before importing routines are called
@$args = grep { !/^skip_/ } @$args
}
# There is no need to fiddle with import() or do any wrapping.
# the $specs data structure means you generally do not need to parse
# arguments yourself (but you can if you want using alter_import_args())
# Change the spec object before export occurs
sub before_import {
my $class = shift;
my ( $importer, $specs ) = @_;
if ($specs->config->{optionA}) {
# Modify $spec attributes accordingly
}
}
# Use spec object after export occurs
sub after_import {
my $class = shift;
my ( $importer, $specs ) = @_;
do_option_a() if $specs->config->{optionA};
do_option_c( $specs->config->{optionC} )
if $specs->config->{optionC};
print "-subs tag was used\n"
if $specs->config->{subs};
print "exported 'subA'\n"
if $specs->exports->{subA};
}
...
๐ IMPORTER
package Some::Importer;
use Some::Exporter qw/ subA $SCALAR !%HASH /,
-default => { -prefix => 'my_' },
qw/ -optionA !-optionB /,
subB => { -as => 'sub_b' };
subA();
print $SCALAR;
sub_b();
my_do_the_thing();
...
๐ฆ IMPORT INTERFACE ๐ฆ
Importing from a package that uses Exporter::Declare will be familiar to anyone who has imported from modules before. Arguments are all assumed to be export names, unless prefixed with - or : โ in which case they may be a tag or an option. Exports without a sigil are assumed to be code exports; variable exports must be listed with their sigil.
Items prefixed with the ! symbol are forcefully excluded, regardless of any listed item that may normally include them. Tags can also be excluded, and this will effectively exclude everything in the tag.
Tags are simply lists of exports, and the exporting class may define any number of tags. Exporter::Declare also has the concept of options, which have the same syntax as tags. Options may be boolean or argument based. Boolean options are actually 3 value: undef, false !, or true. Argument based options will grab the next value in the arguments list as their own, regardless of what type of value it is.
When you use the module, or call import(), all the arguments are transformed into an Exporter::Declare::Specs object. Arguments are parsed for you into a list of imports, and a configuration hash in which tags/options are keys. Tags are listed in the config hash as true, false, or undef depending on if they were included, negated, or unlisted. Boolean options will be treated in the same way as tags. Options that take arguments will have the argument as their value.
๐๏ธ SELECTING ITEMS TO IMPORT
Exports can be subs, or package variables (scalar, hash, array). For subs, simply ask for the sub by name โ you may optionally prefix the sub's name with the sub sigil &. For variables, list the variable name along with its sigil $, %, or @.
use Some::Exporter qw/ somesub $somescalar %somehash @somearray /;
๐ท๏ธ TAGS
Every exporter automatically has the following 3 tags; in addition, they may define any number of custom tags. Tags can be specified by their name prefixed by either - or :.
-allโ This tag may be used to import everything the exporter provides.-defaultโ This tag is used to import the default items exported. This will be used when no argument is provided to import.-aliasโ Every package has an alias that it can export. This is the last segment of the package's namespace. IEMy::Long::Package::Name::Foocould export theFoo()function. These alias functions simply return the full package name as a string, in this case'My::Long::Package::Name::Foo'. This is similar toaliased.
The -alias tag is a shortcut so that you do not need to think about what the alias name would be when adding it to the import arguments.
use My::Long::Package::Name::Foo -alias;
my $foo = Foo()->new(...);
โจ RENAMING IMPORTED ITEMS โจ
You can prefix, suffix, or completely rename the items you import. Whenever an item is followed by a hash in the import list, that hash will be used for configuration. Configuration items always start with a dash -.
The 3 available configuration options that affect import names are -prefix, -suffix, and -as. If -as is seen it will be used as is. If prefix or suffix are seen they will be attached to the original name (unless -as is present, in which case they are ignored).
use Some::Exporter subA => { -as => 'DoThing' },
subB => { -prefix => 'my_', -suffix => '_ok' };
The example above will import subA() under the name DoThing(). It will also import subB() under the name my_subB_ok().
You may also specify a prefix and/or suffix for tags. The following example will import all the default exports with my_ prefixed to each name.
use Some::Exporter -default => { -prefix => 'my_' };
๐๏ธ OPTIONS
Some exporters will recognise options. Options look just like tags, and are specified the same way. What options do, and how they effect things, is exporter-dependant.
use Some::Exporter qw/ -optionA -optionB /;
๐ฃ๏ธ ARGUMENTS
Some options require an argument. These options are just like other tags/options except that the next item in the argument list is slurped in as the option value.
use Some::Exporter -ArgOption => 'Value, not an export',
-ArgTakesHash => { ... };
Once again, available options are exporter specific.
๐ PROVIDING ARGUMENTS FOR GENERATED ITEMS
Some items are generated at import time. These items may accept arguments. There are 3 ways to provide arguments, and they may all be mixed (though that is not recommended).
As a hash:
use Some::Exporter generated => { key => 'val', ... };
As an array:
use Some::Exporter generated => [ 'Arg1', 'Arg2', ... ];
As an array in a config hash:
use Some::Exporter generated => { -as => 'my_gen', -args => [ 'arg1', ... ]};
You can use all three at once, but this is really a bad idea, documented for completeness:
use Some::Exporter generated => { -as => 'my_gen, key => 'value', -args => [ 'arg1', 'arg2' ]}
generated => [ 'arg3', 'arg4' ];
The example above will work fine; all the arguments will make it into the generator. The only valid reason for this to work is that you may provide arguments such as -prefix to a tag that brings in generator(), while also desiring to give arguments to generator() independently.
๐ PRIMARY EXPORT API ๐
With the exception of import(), all the following work equally well as functions or class methods.
import( @args )โ Theimport()class method. This turns the@argslist into anExporter::Declare::Specsobject.exports( @add_items )โ Add items to be exported.@list = exports()โ Retrieve list of exports.default_exports( @add_items )โ Add items to be exported, and add them to the-defaulttag.@list = default_exports()โ List of exports in the-defaulttag.import_options(@add_items)โ Specify boolean options that should be accepted at import time.import_arguments(@add_items)โ Specify options that should be accepted at import that take arguments.export_tag( $name, @add_items );โ Define an export tag, or add items to an existing tag.
๐ EXTENDED EXPORT API ๐
These all work fine in function or method form, however the syntax sugar will only work in function form.
reexport( $package )โ Make this exporter inherit all the exports and tags of$package. Works forExporter::DeclareorExporter.pmbased exporters. Re-Exporting ofSub::Exporterbased classes is not currently supported.export_to( $package, @args )โ Export to the specified class.export( $name )export( $name, $ref )โexportis a keyword that lets you export any 1 item at a time. The item can be exported by name, or name + ref. When a ref is provided, the export is created, but there is no corresponding variable/sub in the package's namespace.default_export( $name )default_export( $name, $ref )gen_export( $name )gen_export( $name, $ref )gen_default_export( $name )gen_default_export( $name, $ref )โ These all act just likeexport(), except that they add subrefs as generators, and/or add exports to the-defaulttag.
๐ฎ MAGIC ๐ฎ
okay bestie, real talk time: please use Exporter::Declare::Magic directly from now on. Like, from now on from now on. That's the vibe. โจ
๐ DEPRECATED USAGE OF MAGIC ๐
use Exporter::Declare '-magic';
This adds Devel::Declare magic to several functions. It also lets you easily create or use parsers on your own exports โ no big deal, just casually unlocking parser powers. ๐ See Exporter::Declare::Magic for more details, she explains it all.
You can also provide import arguments to Devel::Declare::Magic:
# Arguments to -magic must be in an arrayref, not a hashref.
use Exporter::Declare -magic => [ '-default', '!export', -prefix => 'magic_' ];
โ ๏ธ bestie listen to me: arguments to -magic must be in an arrayref, not a hashref. Not a hashref!! I'm literally begging. ๐
๐ ๏ธ INTERNAL API ๐ ๏ธ
ngl, Exporter/Declare.pm does not have much logic to speak of. Like, at all. It's giving minimal. Rather, Exporter::Declare is sugar on top of class meta data stored in Exporter::Declare::Meta objects. Arguments are parsed via Exporter::Declare::Specs, and also turned into objects. Even exports are blessed references to the exported item itself, and handle the injection on their own. (See Exporter::Declare::Export.) ๐
the way that everything is an objectโฆ we love a consistent design language fr fr.
๐ง META CLASS ๐ง
All exporters have a meta class. The only way to get the meta object is to call the export_meta() method on the class/object that is an exporter. Any class that uses Exporter::Declare gets this method, and a meta-object. ๐ export_meta() is your girl. That's the whole access path. Periodt. ๐
๐ AUTHORS ๐
Chad Granum exodist7@gmail.com
iconic. legendary. the moment. ๐ซถ
๐ COPYRIGHT ๐
Copyright (C) 2010 Chad Granum
Exporter-Declare is free software; Standard perl licence.
Exporter-Declare is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the license for more details.
โจ yes bestie, that's the actual all-caps warranty disclaimer โ it's real, it's legal, and it's not me being dramatic for once. ๐
and that's a wrap on Export...Declare(3pm) ๐ฆ โ this beauty lives in perl v5.34.0 and was dated 2022-06-13, courtesy of the User Contributed Perl Documentation crew. go forth and export responsibly, my love ๐ธ๐โญ
๐ฆ where this page comes from
- debian bookworm: libexporter-declare-perl 0.114-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libexporter-declare-perl 0.114-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libexporter-declare-perl 0.114-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libexporter-declare-perl 0.114-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libexporter-declare-perl 0.114-2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms