| 1 |
NAME |
| 2 |
Exporter::Renaming - Allow renaming of symbols on import |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
# Enable renaming in Exporter |
| 6 |
use Exporter::Renaming; |
| 7 |
|
| 8 |
# Import File::Find::find as main::search |
| 9 |
use File::Find Renaming => [ find => search]; |
| 10 |
|
| 11 |
# Disable renaming |
| 12 |
no Exporter::Renaming |
| 13 |
|
| 14 |
ABSTRACT |
| 15 |
Allow Renaming of symbols on Import |
| 16 |
|
| 17 |
DESCRIPTION |
| 18 |
Overview |
| 19 |
This module adds the ability to rename symbols to the standard Exporter |
| 20 |
module. After "use Exporter::Renaming", you can import symbols from |
| 21 |
exporting modules not only under their original names, but also under |
| 22 |
names of your choosing. |
| 23 |
|
| 24 |
Here, *symbol* is used to mean anything that could be exported by a |
| 25 |
Module, that is, a Perl function or variable. Thus a symbol begins with |
| 26 |
an optional *type character* (one of "$", "@", "%", "&", and "*"), |
| 27 |
followed by a name (a Perl identifier, made up of alphanumerics and "_", |
| 28 |
starting with a non-digit). |
| 29 |
|
| 30 |
To trigger renaming behavior, the import list of a subsequent "use |
| 31 |
<module>" statement must begin with the keyword 'Renaming', followed by |
| 32 |
a list reference, the <renaming list|/Renaming List>, which describes |
| 33 |
the renaming imports (see below). After that, a normal import list may |
| 34 |
follow, which Exporter processes as usual. |
| 35 |
|
| 36 |
Renaming List |
| 37 |
The renaming list contains *renaming pairs*, which are pairs of symbols. |
| 38 |
The first part of a pair is the original symbol (as known to the |
| 39 |
exporting module) and the second one is the renamed symbol (as you want |
| 40 |
to use it after import). It is an error (fatal, as all "Renaming" or |
| 41 |
"Exporter" errors) if the renaming list has an odd number of elements, |
| 42 |
or if one of its symbols is invalid. |
| 43 |
|
| 44 |
If none of the symbols in a *renaming pair* contains a *type character*, |
| 45 |
an "&" is assumed. If only one has a *type character*, this type is |
| 46 |
assumed for the other one too. If both have type characters, it is an |
| 47 |
error if they don't agree. |
| 48 |
|
| 49 |
If the renamed symbol (the second part) of a *renaming pair* is |
| 50 |
undefined, the original symbol is imported unchanged, so you can include |
| 51 |
normal imports in a renaming list without retyping the name. |
| 52 |
|
| 53 |
It is an error for a symbol to appear more than once as the second part |
| 54 |
of a *renaming pair*, that is, to specify the same thing twice as the |
| 55 |
target of a renaming operation. It is allowed to import the same symbol |
| 56 |
multiple times with different targets. Maybe it even makes sense in some |
| 57 |
situations. |
| 58 |
|
| 59 |
Operation |
| 60 |
Exporter continues to behave normally for normal imports while renaming |
| 61 |
behavior is switched on. Only the presence of the keyword "Renaming", |
| 62 |
followed by an array reference in the first and second positions after a |
| 63 |
"use" statement triggers renaming. |
| 64 |
|
| 65 |
The renaming behavior of Exporter is thus compatible with its standard |
| 66 |
behavior. If renaming must be switched off for some reason, this can be |
| 67 |
done via "no Export::Renaming". |
| 68 |
|
| 69 |
If an *import list* contains both a renaming list and a sequence of |
| 70 |
normal import statements, the renaming is done first, as indicated by |
| 71 |
its position. No cross-check is done between the results of renaming and |
| 72 |
the normal imports, as if these resulted from two separate "use" |
| 73 |
statements. |
| 74 |
|
| 75 |
EXAMPLES |
| 76 |
All examples assume that |
| 77 |
|
| 78 |
use Exporter::Renaming; |
| 79 |
|
| 80 |
has been called (and that "no Exporter::Renaming" hasn't). |
| 81 |
|
| 82 |
The most obvious application of "Exporter::Renaming" is to solve a name |
| 83 |
conflict. Suppose our module already defines a function "find", and we |
| 84 |
want to use the standard "File::Find" module. We could then rename |
| 85 |
"find" from "File::Find" to "search" in our own module: |
| 86 |
|
| 87 |
use File::Find Renaming => [ find => 'search' ]; |
| 88 |
|
| 89 |
Let's assume the "finddepth" function from File::Find doesn't cause a |
| 90 |
name conflict, and we want to import it under its original name as well. |
| 91 |
|
| 92 |
This does it in the renaming list: |
| 93 |
|
| 94 |
use File::Find Renaming => [ |
| 95 |
find => 'search', |
| 96 |
finddepth => undef, |
| 97 |
]; |
| 98 |
|
| 99 |
...as does this, but explicitly: |
| 100 |
|
| 101 |
use File::Find Renaming => [ |
| 102 |
find => 'search', |
| 103 |
finddepth => 'finddepth', |
| 104 |
]; |
| 105 |
|
| 106 |
...while this uses a regular import: |
| 107 |
|
| 108 |
use File::Find Renaming => [ find => 'search' ], 'finddepth'; |
| 109 |
|
| 110 |
Should you find it annoying that a pedantic module author has chosen to |
| 111 |
adorn all of the module's exports with a redundant prefix (these things |
| 112 |
happen), you could do this: |
| 113 |
|
| 114 |
use Mythical::Graphics::Module Renaming => [ |
| 115 |
gfxColor => '%color', # this imports a hash |
| 116 |
gfxPen => 'pen', |
| 117 |
gfxLine => 'line', |
| 118 |
# .... |
| 119 |
# etc |
| 120 |
]; |
| 121 |
|
| 122 |
...lower-casing the names as well. |
| 123 |
|
| 124 |
If you need to add clarifying prefixes that a sloppy module author has |
| 125 |
neglected to provide in the exports (these things happen), you go the |
| 126 |
other way around: |
| 127 |
|
| 128 |
use Legendary::Graphics::Module Renaming [ |
| 129 |
Color => '%gfxColor', |
| 130 |
Pen => 'gfxPen', |
| 131 |
Line => 'gfxLine', |
| 132 |
# ... |
| 133 |
# etc |
| 134 |
]; |
| 135 |
|
| 136 |
...also lower-casing the initial letters. |
| 137 |
|
| 138 |
If you are confronted with a standard module that uses a slightly |
| 139 |
non-standard naming convention (it happens), you can rectify the |
| 140 |
situation: |
| 141 |
|
| 142 |
use Data::Dumper Renaming => [ Dumper => 'dump' ]; |
| 143 |
|
| 144 |
Now you can say "print dump \ %some_hash" instead of "print Dumper ..."; |
| 145 |
|
| 146 |
CAVEATS |
| 147 |
* As has been mentioned in section Operation, no cross-check is done |
| 148 |
between renaming exports and normal exports that go on in the same |
| 149 |
"use" statement. This means that a renaming import may later be |
| 150 |
overwritten by a normal import without a clear indication. This |
| 151 |
happens when one of the new names given in renaming coincides with |
| 152 |
one of the original ones imported through normal import. |
| 153 |
|
| 154 |
* "Exporter::Renaming" only affects modules that do standard |
| 155 |
exporting, that is, modules that inherit their "import" method from |
| 156 |
Exporter. Modules that use a different "import" method are |
| 157 |
unaffected and don't understand renaming lists. |
| 158 |
|
| 159 |
* Renaming doesn't affect the name c<caller> sees for a function. This |
| 160 |
should come as no surprise, since normal export doesn't affect this |
| 161 |
name either. It is always the (package-qualified) name the function |
| 162 |
was originally compiled with. |
| 163 |
|
| 164 |
BUGS |
| 165 |
* The lack of a cross-check between renaming and normal imports is |
| 166 |
regrettable, but unlikely to be fixed unless Renaming is made part |
| 167 |
of Exporter. Except for the simplest cases, only Exporter can parse |
| 168 |
an export list. |
| 169 |
|
| 170 |
* Calls of "use Exporter::Renaming" and "no Exporter::Renaming" don't |
| 171 |
nest. Instead of switching unconditionally, "no Renaming" should |
| 172 |
only switch off the behavior if it was off in the corresponding call |
| 173 |
to "use Exporter::Renaming". A future release may address this. |
| 174 |
|
| 175 |
SEE ALSO |
| 176 |
Exporter, Perl |
| 177 |
|
| 178 |
AUTHOR |
| 179 |
Anno Siegel, <[email protected]> |
| 180 |
|
| 181 |
ACKNOWLEDGEMENTS |
| 182 |
Thanks to Avi Finkel ([email protected]) and Simon Cozens |
| 183 |
([email protected]) for a discussion of this project on IRC. While |
| 184 |
brief, their remarks helped me think about things the right way. |
| 185 |
|
| 186 |
COPYRIGHT AND LICENSE |
| 187 |
Copyright 2003 by Anno Siegel |
| 188 |
|
| 189 |
This library is free software; you can redistribute it and/or modify it |
| 190 |
under the same terms as Perl itself. |
| 191 |
|