Exporter::Easy(3pm)

πŸ“¦
version 0.18 Β· 5 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Exporter::Easy(3pm) πŸ’–

okay bestie, welcome to the User Contributed Perl Documentation section of the manual, and specifically to Exporter::Easy(3pm) β€” the module that takes the drudgery out of exporting symbols. yes that's its actual job description. and yes it's amazing. πŸŽ€


✨ NAME ✨

Exporter::Easy β€” takes the drudgery out of Exporting symbols. πŸ’…


🌸 SYNOPSIS 🌸

so in your module, YourModule.pm, you're gonna write this:

package YourModule;
use Exporter::Easy (
  OK => [ '$munge', 'frobnicate' ] # symbols to export on request
);

and then in other files that wanna use YourModule:

use ModuleName qw(frobnicate);      # import listed symbols
frobnicate ($left, $right)          # calls YourModule::frobnicate

that's it. that's the whole vibe. πŸ¦‹


πŸ‘‘ DESCRIPTION πŸ‘‘

Exporter::Easy makes using Exporter easy. no cap. in its simplest case, it lets you drop all that boilerplate code that comes with using Exporter, so this whole situation:

require Exporter;
use base qw( Exporter );
use vars qw( @EXPORT );
@EXPORT = ( 'init' );

literally just becomes this:

use Exporter::Easy ( EXPORT => [ 'init' ] );

and then when things get more complicated β€” like when you're using tags to build lists, and then building more tags out of those tags β€” it stays 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. slay. ✨


πŸŽ€ HOW TO USE IT πŸŽ€

just put

use Exporter::Easy ( KEY => value, ...);

in your package. arguments are passed as key-value pairs, and these are the keys you've got available:

  • TAGS β€” the value should be a reference to a list shaped like (TAG_NAME, TAG_VALUE, TAG_NAME, TAG_VALUE, ...), where TAG_NAME is a string and TAG_VALUE is a reference to an array of symbols and tags. for example:
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. and here's the iconic part β€” you can build tags out of other tags. in the example above, the all tag will contain all the symbols from file, string and hash. you can also subtract symbols and tags β€” in that same example, some contains the symbols from all but with open removed, and all the symbols from hash removed.

the rule is: any symbol starting with a ':' is taken to be a tag which has been defined previously (and if it's not defined, you'll get an error, sorry bestie 😭). 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 to your package's @EXPORT_OK array. 🫢

  • OK β€” the value should be a reference to a list of symbols and tags (which will be expanded). these symbols will be added to the @EXPORT_OK array in your package. using OK and OK_ONLY together will give an error.
  • OK_ONLY β€” the value should be a reference to a list of symbols and tags (which will be expanded). the @EXPORT_OK array in your package will contain only these symbols. this totally overrides the automatic population of this array. if you just wanna add some symbols to the list that Exporter::Easy has automatically built, then you should use OK instead. using OK_ONLY and OK together will give an error.
  • EXPORT β€” the value should be a reference to a list of symbol names and tags. any tags will be expanded and the resulting list of symbol names will be placed in the @EXPORT array in your package. heads up: the tag created by the ALL key is not available at this stage.
  • FAIL β€” the value should be a reference to a list of symbol names and tags. the tags will be expanded and the resulting list of symbol names will be placed in the @EXPORT_FAIL array in your package. they will also be added to the @EXPORT_OK list.
  • ALL β€” the value should be the name of a tag that doesn't yet exist. this tag will contain a list of all symbols which can be exported.
  • ISA β€” if you set this to 0 then Exporter will not be added to your @ISA list.
  • VARS β€” if this is set to 1, or not provided at all, then all $, @ and % variables mentioned previously will be available to use in your package as if you had done a use vars on them. if it's set to a reference to a list of symbols and tags, then only those symbols will be available. if it's set to 0 then you'll have to do your own use vars in your package.

🧚 PROCESSING ORDER 🧚

we need to 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. which means you cannot use the tag created by ALL anywhere except in VARS (although vars defaults to using all symbols anyway). so like, keep that in mind. πŸ’œ


🌈 SEE ALSO 🌈

  • Exporter is the grandaddy of all Exporter modules, and it's bundled with Perl itself, unlike the rest of the modules listed here. go look at the documentation for this module to see more explanation of the OK, EXPORT and other variables.
  • Attribute::Exporter defines attributes which you use to mark which subs and variables you want to export, and how.
  • Exporter::Simple also uses attributes to control the export of functions and variables from your module.
  • Const::Exporter makes it easy to create a module that exports constants.
  • Constant::Exporter is another module that makes it easy to create modules that define and export constants.
  • Sub::Exporter is a "sophisticated exporter for custom-built routines"; it lets you provide generators that can be used to customise what gets imported when someone uses your module.
  • Exporter::Tiny provides the same features as Sub::Exporter, but relying only on core dependencies.
  • Exporter::Shiny is a shortcut for Exporter::Tiny that provides a more concise notation for providing optional exports.
  • Exporter::Declare provides syntactic sugar to make the export status of your functions part of their declaration. kind of.
  • AppConfig::Exporter lets you export part of an AppConfig-based configuration.
  • Exporter::Lexical lets you export lexical subs from your module.
  • Constant::Exporter::Lazy lets you write a module that exports function-style constants, which are instantiated lazily.
  • Exporter::Auto will export everything from your module that it thinks is a public function (name doesn't start with an underscore).
  • Class::Exporter lets you export class methods as regular subroutines.
  • Xporter is like Exporter, but with persistent defaults and auto-ISA.

⭐ REPOSITORY ⭐

the repo is right here bestie: https://github.com/neilb/Exporter-Easy 🫢


πŸ’œ AUTHOR πŸ’œ

written by Fergal Daly <fergal@esatclear.ie>. iconic of him honestly.


πŸ“ LICENSE πŸ“

under the same license as Perl itself.


okay that's the whole page, you're literally a Perl exporter expert now, main character energy fr fr πŸ’…βœ¨

this was perl v5.36.0, dated 2022-10-22 β€” catch you in the next Exporter::Easy(3pm) era, mwah πŸ˜­πŸ’–

πŸ“¦ where this page comes from