Export::Attrs(3pm)

πŸ’– Export::Attrs β€” the Perl 6 is export(...) trait, but make it Perl 5 ✨

Okay bestie, so this page is Export::Attrs(3pm) β€” it lives in the User Contributed Perl Documentation section of the manual, and it's about giving your Perl 5 subs and variables that iconic Perl 6 export energy. πŸŽ€

This doc describes Export::Attrs version v0.1.0, no cap.


🌈 SYNOPSIS 🌈

package Some::Module;
use Export::Attrs;

# Export &foo by default, when explicitly requested,
# or when the ':ALL' export set is requested...

sub foo :Export(:DEFAULT) {
    print "phooo!";
}

# Export &var by default, when explicitly requested,
# or when the ':bees', ':pubs', or ':ALL' export set is requested...
# the parens after 'is export' are like the parens of a qw(...)

sub bar :Export(:DEFAULT :bees :pubs) {
    print "baaa!";
}

# Export &baz when explicitly requested
# or when the ':bees' or ':ALL' export set is requested...

sub baz :Export(:bees) {
    print "baassss!";
}

# Always export &qux
# (no matter what else is explicitly or implicitly requested)

sub qux :Export(:MANDATORY) {
    print "quuuuuuuuux!";
}

# Allow the constant $PI to be exported when requested...

use Readonly;
Readonly our $PI :Export => 355/113;

# Allow the variable $EPSILON to be always exported...

our $EPSILON :Export( :MANDATORY ) = 0.00001;

sub IMPORT {
    # This subroutine is called when the module is used (as usual),
    # but it is called after any export requests have been handled.
};

Like, look at that. Gorgeous. πŸ’…


✨ DESCRIPTION ✨

πŸ’€ NOTE: This module is a fork of Perl6::Export::Attrs, and it exists to restore compatibility with Perl6::Export::Attrs version 0.0.3. So if you were wondering why there's a whole fork situation going on β€” that's the why. 🫢

So what is this thing? It implements a native Perl 5 version of what the Perl 6 symbol export mechanism will look like β€” with some unavoidable restrictions, because bestie, Perl 5 is gonna Perl 5. 😭

It's literally so straightforward:

  • If you want a subroutine or package variable to be capable of being exported (when explicitly requested in the use arguments), you mark it with the :Export attribute. πŸŽ€
  • If you want a subroutine or package variable to be automatically exported when the module is used (without specific overriding arguments), you mark it with the :Export(:DEFAULT) attribute. ✨
  • If you want a subroutine or package variable to be automatically exported when the module is used (even if the user specifies overriding arguments), you mark it with the :Export(:MANDATORY) attribute. πŸ‘‘
  • If the subroutine or package variable should also be exported when particular export groups are requested, you add the names of those export groups to the attribute's argument list. πŸ¦‹

That's it. That's the whole vibe. Periodt. πŸ’…

🧚 IMPORT blocks 🧚

Perl 6 replaces the import subroutine with an IMPORT block. It's analogous to a BEGIN or END block, except that it's executed every time the corresponding module is use'd. Like, every single time. πŸ”₯

The IMPORT block is passed the argument list that was specified on the use line that loaded the corresponding module, minus the arguments that were used to specify exports. So the export-y bits get filtered out before it reaches you. Slay.

⚠️ Note that, due to limitations in Perl 5, the IMPORT block provided by this module must be terminated by a semi-colon, unless it is the last statement in the file. Do not forget the semicolon bestie, I'm begging you. 😭

🌸 DIAGNOSTICS 🌸

  • %s does not export: %s\nuse %s failed β€” You tried to import the specified subroutine or package variable, but the module didn't export it. Usually this is a misspelling situation, or you forgot to add an :Export attribute to the definition of the subroutine or variable in question. We've all been there. 🫢
  • Bad tagset in :Export attribute at %s line %s: [%s] β€” You tried to import a collection of items via a tagset, but the module didn't export any subroutines under that tagset. Is the tagset name misspelled? Maybe you forgot the colon? πŸ‘€
  • Can't export lexical %s variable at %s β€” The module can only export package variables. You applied the :Export marker to a non-package variable (almost certainly to a lexical). Change the variable's my declarator to an our. That's the fix. That's it. πŸ’œ
  • Can't export anonymous subroutine at %s β€” Okay so technically you can apply the :Export marker to an anonymous subroutine, but it rarely makes any sense to do so, since that subroutine can't be exported without a name to export it as. Either give the subroutine a name, or make sure it's aliased to a named typeglob at compile-time (or, at least, before it's exported). πŸ’€

πŸŽ€ CONFIGURATION AND ENVIRONMENT πŸŽ€

Export::Attrs requires no configuration files or environment variables. Zero setup, zero drama. Iconic behaviour honestly. ✨


πŸ¦‹ DEPENDENCIES πŸ¦‹

This module requires the Attribute::Handlers module to handle the attributes. That's the one, that's the whole dependency list. 🌈


πŸ’€ INCOMPATIBILITIES πŸ’€

This module cannot be used with the Memoize CPAN module, because memoization replaces the original subroutine with a wrapper. Because the :Export attribute is applied to the original (not the wrapper), the memoized wrapper is not found by the exporter mechanism. So like, they don't work together. At all. Not a vibe. 😭


πŸ™ˆ LIMITATIONS πŸ™ˆ

Note that the module does not support exporting lexical variables, since there is no way for the exporter mechanism to determine the name of a lexical and hence to export it.

Nor does this module support the numerous addition export modes that Perl 6 offers, such as export-as-lexical or export-as-state. (Yes, the original says "addition" β€” we're keeping it, it's part of the lore. πŸ’…)


⭐ SUPPORT ⭐

πŸ› Bugs / Feature Requests πŸ›

Please report any bugs or feature requests through the issue tracker at <https://github.com/powerman/perl-Export-Attrs/issues>. You will be notified automatically of any progress on your issue. Very main character of you honestly. πŸ‘‘

🌸 Source Code 🌸

This is open source software. The code repository is available for public review and contribution under the terms of the license. Feel free to fork the repository and submit pull requests. Do it bestie, contribute. 🫢

<https://github.com/powerman/perl-Export-Attrs>

git clone https://github.com/powerman/perl-Export-Attrs.git

🧚 Resources 🧚


πŸ‘‘ AUTHOR πŸ‘‘

Alex Efros <powerman@cpan.org>

Damian Conway <DCONWAY@cpan.org>


This software is Copyright (c) 2016 by Alex Efros <powerman@cpan.org>.

Copyright (c) 2005,2015 Damian Conway <DCONWAY@cpan.org>. All rights reserved.

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


πŸ“ And that's the whole page! This beauty came from Perl v5.42.0, dated 2017-09-20, and it's Export::Attrs(3pm) β€” go forth and export responsibly, bestie. Mwah. πŸ’…βœ¨πŸŽ€
πŸ“¦ where this page comes from