1Exporter::Tiny::Manual:U:sIemrpoCrotnitnrgi(b3u)ted PerlExDpoocrutmeern:t:aTtiinoyn::Manual::Importing(3)
2
3
4

NAME

6       Exporter::Tiny::Manual::Importing - importing from Exporter::Tiny-based
7       modules
8

DESCRIPTION

10       For the purposes of this discussion we'll assume we have a module
11       called "MyUtils" which exports functions called "frobnicate", "red",
12       "blue", and "green". It has a tag set up called ":colours" which
13       corresponds to "red", "blue", and "green".
14
15       Many of these tricks may seem familiar from Sub::Exporter. That is
16       intentional. Exporter::Tiny doesn't attempt to provide every feature of
17       Sub::Exporter, but where it does it usually uses a fairly similar API.
18
19   Basic importing
20       It's easy to import a single function from a module:
21
22          use MyUtils "frobnicate";
23
24       Or a list of functions:
25
26          use MyUtils "red", "green";
27
28       Perl's qw() shorthand for a list of words is pretty useful:
29
30          use MyUtils qw( red green );
31
32       If the module defines tags, you can import them like this:
33
34          use MyUtils qw( :colours );
35
36       Or with a hyphen instead of a colon:
37
38          use MyUtils qw( -colours );
39
40       Hyphens are good because Perl will autoquote a bareword that follows
41       them:
42
43          use MyUtils -colours;
44
45       And it's possible to mix function names and tags in the same list:
46
47          use MyUtils qw( frobnicate :colours );
48
49   Renaming imported functions
50       It's possible to rename a function you're importing:
51
52          use MyUtils "frobnicate" => { -as => "frob" };
53
54       Or you can apply a prefix and/or suffix. The following imports the
55       function and calls it "my_frobinate_thing".
56
57          use MyUtils "frobnicate" => { -prefix => "my_", -suffix => "_thing" };
58
59       You can apply a prefix/suffix to all functions you import by placing
60       the hashref first in the import list. (This first hashref is referred
61       to as the global options hash, and can do some special things.)
62
63          use MyUtils { prefix => "my_" }, "frobnicate";
64
65       Did you notice that we used "-prefix" and "-suffix" in the normal
66       options hash, but "prefix" and "suffix" (no hyphen) in the global
67       options hash? That's a common pattern with this module.
68
69       You can import the same function multiple times with different names:
70
71          use MyUtils
72             "frobnicate" => { -as => "frob" },
73             "frobnicate" => { -as => "frbnct" };
74
75       Tags can take the "-prefix" and "-suffix" options too. The following
76       imports "colour_red", "colour_green", and "colour_blue":
77
78          use MyUtils -colours => { -prefix => "colour_" };
79
80       You can also set "-as" to be a coderef to generate a function name.
81       This imports functions called "RED", "GREEN", and "BLUE":
82
83          use MyUtils -colours => { -as => sub { uc($_[0]) } };
84
85       Note that it doesn't make sense to use "-as" with a tag unless you're
86       doing this coderef thing. Coderef "as" also works in the global options
87       hash.
88
89   DO NOT WANT!
90       Sometimes you want to supply a list of functions you don't want to
91       import. To do that, prefix the function with a bang. This imports
92       everything except "frobnicate":
93
94          use MyUtils qw( -all !frobnicate );
95
96       You can add the bang prefix to tags too. This will import everything
97       except the colours.
98
99          use MyUtils qw( -all !:colours );
100
101       Negated imports always "win", so the following will not import
102       "frobnicate", no matter how many times you repeat it...
103
104          use MyUtils qw( !frobnicate frobnicate frobnicate frobnicate );
105
106   Importing by regexp
107       Here's how you could import all functions beginning with an "f":
108
109          use MyUtils qw( /^F/i );
110
111       Or import everything except functions beginning with a "z":
112
113          use MyUtils qw( -all !/^Z/i );
114
115       Note that regexps are always supplied as strings starting with "/", and
116       not as quoted regexp references ("qr/.../").
117
118   Import functions into another package
119       Occasionally you need to import functions not into your own package,
120       but into a different package. You can do that like this:
121
122          use MyUtils { into => "OtherPkg" }, "frobnicate";
123
124          OtherPkg::frobincate(...);
125
126       However, Import::Into will probably provide you with a better approach
127       which doesn't just work with Exporter::Tiny, but all exporters.
128
129   Lexical subs on Perl 5.37.2 and above
130       Often you want to make use of an exported function, but don't want it
131       to "pollute" your namespace.
132
133       On newer versions of Perl, Exporter::Tiny can use "export_lexically"
134       from builtin to give you lexical versions of exports.
135
136          {
137             use MyUtils -lexical, "frobnicate";
138
139             frobnicate(...);  # ok
140          }
141
142          frobnicate(...);  # not ok
143
144       This functionality should be considered EXPERIMENTAL until
145       "export_lexically" is included in a stable release of Perl.
146
147   Lexical subs on Perl older than 5.37.2
148       If you install Lexical::Var, then lexical imports should work on
149       versions of Perl as old as 5.12.
150
151   Unimporting
152       You can unimport the functions that MyUtils added to your namespace:
153
154          no MyUtils;
155
156       Or just specific ones:
157
158          no MyUtils qw(frobnicate);
159
160       If you renamed a function when you imported it, you should unimport by
161       the new name:
162
163          use MyUtils frobnicate => { -as => "frob" };
164          ...;
165          no MyUtils "frob";
166
167       Unimporting using tags and regexps should mostly do what you want.
168

DIAGNOSTICS

170       Overwriting existing sub '%s::%s' with sub '%s' exported by %s
171           A warning issued if Exporter::Tiny is asked to export a symbol
172           which will result in an existing sub being overwritten. This
173           warning can be suppressed using either of the following:
174
175              use MyUtils { replace => 1 }, "frobnicate";
176              use MyUtils "frobnicate" => { -replace => 1 };
177
178           Or can be upgraded to a fatal error:
179
180              use MyUtils { replace => "die" }, "frobnicate";
181              use MyUtils "frobnicate" => { -replace => "die" };
182
183       Refusing to overwrite existing sub '%s::%s' with sub '%s' exported by
184       %s
185           The fatal version of the above warning.
186
187       Could not find sub '%s' exported by %s
188           You requested to import a sub which the package does not provide.
189
190       Cannot provide an -as option for tags
191           Because a tag may provide more than one function, it does not make
192           sense to request a single name for it. Instead use "-prefix" or
193           "-suffix".
194
195       Passing options to unimport '%s' makes no sense
196           When you import a sub, it occasionally makes sense to pass some
197           options for it. However, when unimporting, options do nothing, so
198           this warning is issued.
199

SEE ALSO

201       <https://exportertiny.github.io/>.
202
203       Exporter::Shiny, Exporter::Tiny.
204

AUTHOR

206       Toby Inkster <tobyink@cpan.org>.
207
209       This software is copyright (c) 2013-2014, 2017, 2022-2023 by Toby
210       Inkster.
211
212       This is free software; you can redistribute it and/or modify it under
213       the same terms as the Perl 5 programming language system itself.
214

DISCLAIMER OF WARRANTIES

216       THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
217       WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
218       MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
219
220
221
222perl v5.36.0                      2023-04-0E2xporter::Tiny::Manual::Importing(3)
Impressum