Export...naming(3pm)
πΈ NAME πΈ
Exporter::Renaming β allow renaming of symbols on import π
okay so quick vibe check: this is the Export...naming(3pm) page from the User Contributed Perl Documentation (yeah the title up there is all truncated and mysterious, that's just how the header renders it, don't panic bestie π«Ά). It's a Perl module that lets you import stuff under a whole new name. iconic behavior, honestly.
β¨ SYNOPSIS β¨
# Enable renaming in Exporter
use Exporter::Renaming;
# Import File::Find::find as main::search
use File::Find Renaming => [ find => search];
# Disable renaming
no Exporter::Renaming
π¦ ABSTRACT π¦
Allow Renaming of symbols on Import β that's the whole pitch, no cap.
π DESCRIPTION π
π Overview
So here's the tea: this module bolts the ability to rename symbols onto the standard Exporter module. Once you've done use Exporter::Renaming, you can import symbols from exporting modules not just under their original names, but under whatever names you feel like. main character energy, truly.
And when we say symbol? We mean literally anything a Module could export β a Perl function or a variable. So a symbol starts with an optional type character (one of $, @, %, &, and *), and then a name (a Perl identifier, made up of alphanumerics and _, starting with a non-digit). That's it, that's the anatomy π§.
To trigger the renaming behavior, the import list of a subsequent use <module> statement has to begin with the keyword 'Renaming', followed by a list reference β the <renaming list|/Renaming List> β which describes the renaming imports (more on that below π). After that, a normal import list may follow, which Exporter processes as usual. Slay.
π Renaming List
The renaming list contains renaming pairs, which are pairs of symbols. First part of a pair = the original symbol (as known to the exporting module). Second part = the renamed symbol (as you want to use it after import).
Heads up bestie: it is an error (fatal, as allRenamingorExportererrors are) if the renaming list has an odd number of elements, or if one of its symbols is invalid. π
More rules, because of course there are:
- If none of the symbols in a renaming pair contains a type character, an
&is assumed. - If only one has a type character, that type is assumed for the other one too.
- If both have type characters, it is an error if they don't agree.
If the renamed symbol (the second part) of a renaming pair is undefined, the original symbol is imported unchanged β so cute, you can include normal imports in a renaming list without retyping the name π«Ά.
And it is an error for a symbol to appear more than once as the <em>second</em> part of a renaming pair, i.e. specifying the same thing twice as the target of a renaming operation. That's a no-no.
But importing the same symbol multiple times with different targets? Totally allowed β¨. Maybe it even makes sense in some situations. Live your truth.
π Operation
Exporter keeps behaving normally for normal imports while renaming behavior is switched on. The only thing that triggers renaming is the presence of the keyword Renaming, followed by an array reference, in the first and second positions after a use statement.
So the renaming behavior of Exporter is compatible with its standard behavior. And if renaming must be switched off for some reason, you do it via no Export::Renaming.
If an import list contains both a renaming list <em>and</em> a sequence of normal import statements, the renaming is done first, as indicated by its position. And nope β no cross-check is done between the results of renaming and the normal imports, as if these resulted from two separate use statements. Keep that in mind, it matters later π.
π₯ EXAMPLES π₯
All examples assume that
use Exporter::Renaming;
has been called (and that no Exporter::Renaming hasn't).
The most obvious application of Exporter::Renaming is to solve a name conflict. Suppose our module already defines a function find, and we want to use the standard File::Find module. We could then rename find from File::Find to search in our own module:
use File::Find Renaming => [ find => 'search' ];
Let's assume the finddepth function from File::Find doesn't cause a name conflict, and we want to import it under its original name as well.
This does it in the renaming list:
use File::Find Renaming => [
find => 'search',
finddepth => undef,
];
...as does this, but explicitly:
use File::Find Renaming => [
find => 'search',
finddepth => 'finddepth',
];
...while this uses a regular import:
use File::Find Renaming => [ find => 'search' ], 'finddepth';
Should you find it annoying that a pedantic module author has chosen to adorn all of the module's exports with a redundant prefix (these things happen), you could do this:
use Mythical::Graphics::Module Renaming => [
gfxColor => '%color', # this imports a hash
gfxPen => 'pen',
gfxLine => 'line',
# ....
# etc
];
...lower-casing the names as well. β¨
If you need to add clarifying prefixes that a sloppy module author has neglected to provide in the exports (these things happen), you go the other way around:
use Legendary::Graphics::Module Renaming [
Color => '%gfxColor',
Pen => 'gfxPen',
Line => 'gfxLine',
# ...
# etc
]
...also lower-casing the initial letters.
If you are confronted with a standard module that uses a slightly non-standard naming convention (it happens), you can rectify the situation:
use Data::Dumper Renaming => [ Dumper => 'dump' ];
Now you can say print dump \ %some_hash instead of print Dumper ... β glow up complete π
β¨
π«Ά CAVEATS π«Ά
- As has been mentioned in section Operation, no cross-check is done between renaming exports and normal exports that go on in the same
usestatement. This means that a renaming import may later be overwritten by a normal import without a clear indication. This happens when one of the new names given in renaming coincides with one of the original ones imported through normal import. π Exporter::Renamingonly affects modules that do standard exporting, that is, modules that inherit theirimportmethod from Exporter. Modules that use a differentimportmethod are unaffected and don't understand renaming lists. Not their fault, not yours.- Renaming doesn't affect the name
callersees for a function. This should come as no surprise, since normal export doesn't affect this name either. It is always the (package-qualified) name the function was originally compiled with. Facts are facts π
π BUGS π
- The lack of a cross-check between renaming and normal imports is regrettable, but unlikely to be fixed unless Renaming is made part of Exporter. Except for the simplest cases, only Exporter can parse an export list.
- Calls of
use Exporter::Renamingandno Exporter::Renamingdon't nest. Instead of switching unconditionally,no Renamingshould only switch off the behavior if it was off in the corresponding call touse Exporter::Renaming. A future release may address this.
π SEE ALSO π
Exporter, Perl
π AUTHOR π
Anno Siegel, <siegel@zrz.tu-berlin.de>
πΈ ACKNOWLEDGEMENTS πΈ
Thanks to Avi Finkel (avi@finkel.org) and Simon Cozens (simon@simon-cozens.org) for a discussion of this project on IRC. While brief, their remarks helped me think about things the right way. π«Ά
π COPYRIGHT AND LICENSE π
Copyright 2003 by Anno Siegel
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
okay bestie that's the whole page, and before you go β this lil manual page is perl v5.36.0, dated 2022-11-21 π go rename something and slay, periodt β¨
π¦ where this page comes from
- debian bookworm: libexporter-renaming-perl 1.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libexporter-renaming-perl 1.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libexporter-renaming-perl 1.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libexporter-renaming-perl 1.19-3 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libexporter-renaming-perl 1.19-3 Β· 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