Exporter::Lite(3)

πŸ’… Exporter::Lite(3) β€” like, the lite babe of exporters

okay so bestie, you've stumbled into Exporter::Lite, part of the whole User Contributed Perl Documentation situation, and honestly? she's giving minimal icon energy. let's get into it ✨


🌸 NAME 🌸

Exporter::Lite β€” lightweight exporting of functions and variables. yes, that's the whole name, she's lowkey not doing too much πŸŽ€


✨ SYNOPSIS ✨

so you're making a package, right? here's the setup:

  package Foo;
  use Exporter::Lite;

  our @EXPORT    = qw($This That);      # default exports
  our @EXPORT_OK = qw(@Left %Right);    # optional exports

then in the code that's using the module:

  use Foo;
  # $This and &That are imported here

and the optional ones? you gotta explicitly ask for those, no mind-reading here:

 use Foo qw/ @Left %Right /;

πŸ¦‹ DESCRIPTION πŸ¦‹

Exporter::Lite is the alternative to Exporter β€” like, an intentionally lightweight subset of the most commonly-used functionality. she supports import(), @EXPORT and @EXPORT_OK and honestly not a whole lot else. and that's on periodt πŸ’…

Exporter::Lite simply exports its import() function into your namespace. you could call that a "mix-in" or a "role" β€” iykyk.

back when Exporter::Lite was written, if you wanted to use Exporter you had to write something like this whole thing:

 use Exporter;
 our @ISA = qw/ Exporter /;

Exporter::Lite saved you from writing that second line. BUT β€” and this is the plot twist 🫢 β€” since before 2010 you've been able to write:

 use Exporter qw/ import /;

which imports the import function into your namespace from Exporter. so as a result, I would recommend that you use Exporter now, because it's a core module (shipped with Perl). like, she's already in the group chat, no extra download needed ✨

to make sure you get a version of Exporter that supports the above usage, specify a minimum version when you use it:

 use Exporter 5.57 qw/ import /;

πŸŽ€ Back to "Exporter::Lite" πŸŽ€

setting up a module to export its variables and functions is simple, bestie:

     package My::Module;
     use Exporter::Lite;

     our @EXPORT = qw($Foo bar);

functions and variables listed in the @EXPORT package variable are automatically exported if you use the module and don't explicitly list any imports. so now, when you use My::Module, $Foo and bar() will show up. no cap 🧚

optional exports are listed in the @EXPORT_OK package variable:

     package My::Module;
     use Exporter::Lite;

     our @EXPORT_OK = qw($Foo bar);

when My::Module is used, $Foo and bar() will not show up, unless you explicitly ask for them:

     use My::Module qw($Foo bar);
⚠️ note that when you specify one or more functions or variables to import, then you must also explicitly list any of the default symbols you want to use. the defaults don't come along for the ride automatically. πŸ™ˆ

so if you have an exporting module:

     package Games;
     our @EXPORT    = qw/ pacman defender  /;
     our @EXPORT_OK = qw/ galaga centipede /;

then if you want to use both pacman and galaga, then you'd write:

     use Games qw/ pacman galaga /;

yes, you have to name pacman even though it's a default. that's the whole entire bit. πŸ’€

🌈 Methods 🌈

Export::Lite has one public method, import(), which is called automatically when your modules is use()'d. (yes, that's exactly how the original says it, we're keeping it πŸ’œ)

in normal usage you don't have to worry about this at all. she handles herself, main character energy πŸ‘‘

⭐ import ⭐

       Some::Module->import;
       Some::Module->import(@symbols);

works just like Exporter::import() excepting it only honors @Some::Module::EXPORT and @Some::Module::EXPORT_OK.

the given @symbols are exported to the current package provided they are in @Some::Module::EXPORT or @Some::Module::EXPORT_OK. otherwise an exception is thrown (ie. the program dies). like, flatlines. πŸ’€

if @symbols is not given, everything in @Some::Module::EXPORT is exported.


πŸ”₯ DIAGNOSTICS πŸ”₯

so when things go sideways, here's what you might see:

  • '"%s" is not exported by the %s module' β€” you attempted to import a symbol which is not in @EXPORT or @EXPORT_OK. rude of you tbh, but okay 🫢
  • 'Can\'t export symbol: %s' β€” you attempted to import a symbol of an unknown type (ie. the leading $@% salad wasn't recognized). yes, "salad", that's the technical term. πŸ₯—

πŸ’– SEE ALSO πŸ’–

okay this list is long, buckle up bestie, we're meeting the whole extended family:

  • Exporter is the grandaddy of all Exporter modules, and bundled with Perl itself, unlike the rest of the modules listed here. πŸ‘‘
  • 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. (yes, two of them. yes, they're different. we don't make the rules πŸ’…)
  • 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::Export::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 πŸ“

<https://github.com/neilb/Exporter-Lite> β€” go say hi, star it, whatever, she's literally right there 🫢


🌸 AUTHORS 🌸

Michael G Schwern <schwern@pobox.com> β€” icon, legend, the moment ✨


πŸ’œ LICENSE πŸ’œ

this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

see http://www.perl.com/perl/misc/Artistic.html


and that's the whole page bestie! brought to you by perl v5.42.0, dated 2022-03-05 β€” this manual page said slay and clocked out πŸ’…πŸŽ€βœ¨

πŸ“¦ where this page comes from