{"id":6783,"date":"2022-12-20T19:33:38","date_gmt":"2022-12-20T22:33:38","guid":{"rendered":"http:\/\/lode.uno\/linux-man\/index.php\/2022\/12\/20\/exportertinymanualimporting-man3\/"},"modified":"2022-12-20T19:33:38","modified_gmt":"2022-12-20T22:33:38","slug":"exportertinymanualimporting-man3","status":"publish","type":"post","link":"https:\/\/lode.uno\/linux-man\/2022\/12\/20\/exportertinymanualimporting-man3\/","title":{"rendered":"Exporter::Tiny::Manual::Importing (man3)"},"content":{"rendered":"<h1 align=\"center\">Exporter::Tiny::Manual::Importing<\/h1>\n<p> <a href=\"#NAME\">NAME<\/a><br \/> <a href=\"#DESCRIPTION\">DESCRIPTION<\/a><br \/> <a href=\"#DIAGNOSTICS\">DIAGNOSTICS<\/a><br \/> <a href=\"#SEE ALSO\">SEE ALSO<\/a><br \/> <a href=\"#AUTHOR\">AUTHOR<\/a><br \/> <a href=\"#COPYRIGHT AND LICENCE\">COPYRIGHT AND LICENCE<\/a><br \/> <a href=\"#DISCLAIMER OF WARRANTIES\">DISCLAIMER OF WARRANTIES<\/a> <\/p>\n<hr>\n<h2>NAME <a name=\"NAME\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Exporter::Tiny::Manual::Importing \u2212 importing from Exporter::Tiny\u2212based modules<\/p>\n<h2>DESCRIPTION <a name=\"DESCRIPTION\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">For the purposes of this discussion we\u2019ll assume we have a module called &#8220;MyUtils&#8221; which exports functions called &#8220;frobnicate&#8221;, &#8220;red&#8221;, &#8220;blue&#8221;, and &#8220;green&#8221;. It has a tag set up called &#8220;:colours&#8221; which corresponds to &#8220;red&#8221;, &#8220;blue&#8221;, and &#8220;green&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Many of these tricks may seem familiar from Sub::Exporter. That is intentional. Exporter::Tiny doesn\u2019t attempt to provide every feature of Sub::Exporter, but where it does it usually uses a fairly similar <small>API.<\/small><\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Basic importing<\/b> <br \/> It\u2019s easy to import a single function from a module:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils &#8220;frobnicate&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or a list of functions:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils &#8220;red&#8221;, &#8220;green&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Perl\u2019s &#8220;qw()&#8221; shorthand for a list of words is pretty useful:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( red green );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If the module defines tags, you can import them like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( :colours );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or with a hyphen instead of a colon:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( \u2212colours );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Hyphens are good because Perl will autoquote a bareword that follows them:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils \u2212colours;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">And it\u2019s possible to mix function names and tags in the same list:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( frobnicate :colours );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Renaming imported functions<\/b> <br \/> It\u2019s possible to rename a function you\u2019re importing:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils &#8220;frobnicate&#8221; => { \u2212as => &#8220;frob&#8221; };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or you can apply a prefix and\/or suffix. The following imports the function and calls it &#8220;my_frobinate_thing&#8221;.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils &#8220;frobnicate&#8221; => { \u2212prefix => &#8220;my_&#8221;, \u2212suffix => &#8220;_thing&#8221; };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can apply a prefix\/suffix to <b>all<\/b> functions you import by placing the hashref <b>first<\/b> in the import list. (This first hashref is referred to as the global options hash, and can do some special things.)<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils { prefix => &#8220;my_&#8221; }, &#8220;frobnicate&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Did you notice that we used &#8220;\u2212prefix&#8221; and &#8220;\u2212suffix&#8221; in the normal options hash, but &#8220;prefix&#8221; and &#8220;suffix&#8221; (no hyphen) in the global options hash? That\u2019s a common pattern with this module.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can import the same function multiple times with different names:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils <br \/> &#8220;frobnicate&#8221; => { \u2212as => &#8220;frob&#8221; }, <br \/> &#8220;frobnicate&#8221; => { \u2212as => &#8220;frbnct&#8221; };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Tags can take the &#8220;\u2212prefix&#8221; and &#8220;\u2212suffix&#8221; options too. The following imports &#8220;colour_red&#8221;, &#8220;colour_green&#8221;, and &#8220;colour_blue&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils \u2212colours => { \u2212prefix => &#8220;colour_&#8221; };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can also set &#8220;\u2212as&#8221; to be a coderef to generate a function name. This imports functions called &#8220;RED&#8221;, &#8220;GREEN&#8221;, and &#8220;BLUE&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils \u2212colours => { \u2212as => sub { uc($_[0]) } };<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that it doesn\u2019t make sense to use &#8220;\u2212as&#8221; with a tag unless you\u2019re doing this coderef thing. Coderef &#8220;as&#8221; also works in the global options hash.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b><small>DO NOT WANT<\/small> !<\/b> <br \/> Sometimes you want to supply a list of functions you <b>don\u2019t<\/b> want to import. To do that, prefix the function with a bang. This imports everything except &#8220;frobnicate&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( \u2212all !frobnicate );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can add the bang prefix to tags too. This will import everything except the colours.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( \u2212all !:colours );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Negated imports always &#8220;win&#8221;, so the following will not import &#8220;frobnicate&#8221;, no matter how many times you repeat it&#8230;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( !frobnicate frobnicate frobnicate frobnicate );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Importing by regexp<\/b> <br \/> Here\u2019s how you could import all functions beginning with an &#8220;f&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( \/^F\/i );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or import everything except functions beginning with a &#8220;z&#8221;:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils qw( \u2212all !\/^Z\/i );<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Note that regexps are always supplied as <i>strings<\/i> starting with &#8220;\/&#8221;, and not as quoted regexp references (&#8220;qr\/&#8230;\/&#8221;).<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Import functions into another package<\/b> <br \/> Occasionally you need to import functions not into your own package, but into a different package. You can do that like this:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils { into => &#8220;OtherPkg&#8221; }, &#8220;frobnicate&#8221;; <br \/> OtherPkg::frobincate(&#8230;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">However, Import::Into will probably provide you with a better approach which doesn\u2019t just work with Exporter::Tiny, but <b>all<\/b> exporters.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Lexical subs<\/b> <br \/> Often you want to make use of an exported function, but don\u2019t want it to &#8220;pollute&#8221; your namespace.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">There is this Sub::Exporter::Lexical thing that was designed as a plugin for Sub::Exporter, but Exporter::Tiny\u2019s <small>API<\/small> is close enough that it will work. Do you remember that global options hash? Just use that to tell Exporter::Tiny to use an alternative sub installer.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">{ <br \/> use Sub::Exporter::Lexical lexical_installer => { \u2212as => &#8220;lex&#8221; }; <br \/> use MyUtils { installer => lex }, &#8220;frobnicate&#8221;; <br \/> frobnicate(&#8230;); # ok <br \/> } <br \/> frobnicate(&#8230;); # not ok<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Another way to do lexical functions is to import a function into a scalar variable:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my $func; <br \/> use MyUtils &#8220;frobnicate&#8221; => { \u2212as => $func }; <br \/> $func\u2212>(&#8230;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">You can even provide a hashref to put all imported functions into as part of that global options hash I mentioned earlier.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">my %funcs; <br \/> use MyUtils { into => %funcs }, &#8220;frobnicate&#8221;; <br \/> $funcs{frobnicate}\u2212>(&#8230;);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Unimporting<\/b> <br \/> You can unimport the functions that MyUtils added to your namespace:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">no MyUtils;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Or just specific ones:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">no MyUtils qw(frobnicate);<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">If you renamed a function when you imported it, you should unimport by the new name:<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">use MyUtils frobnicate => { \u2212as => &#8220;frob&#8221; }; <br \/> &#8230;; <br \/> no MyUtils &#8220;frob&#8221;;<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">Unimporting using tags and regexps should mostly do what you want.<\/p>\n<h2>DIAGNOSTICS <a name=\"DIAGNOSTICS\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><b>Overwriting existing sub \u2019%s::%s\u2019 with sub \u2019%s\u2019 exported by %s<\/b><\/p>\n<p style=\"margin-left:17%;\">A warning issued if Exporter::Tiny is asked to export a symbol which will result in an existing sub being overwritten. This warning can be suppressed using either of the following:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use MyUtils { replace => 1 }, &#8220;frobnicate&#8221;; <br \/> use MyUtils &#8220;frobnicate&#8221; => { \u2212replace => 1 };<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">Or can be upgraded to a fatal error:<\/p>\n<p style=\"margin-left:17%; margin-top: 1em\">use MyUtils { replace => &#8220;die&#8221; }, &#8220;frobnicate&#8221;; <br \/> use MyUtils &#8220;frobnicate&#8221; => { \u2212replace => &#8220;die&#8221; };<\/p>\n<p style=\"margin-left:11%;\"><b>Refusing to overwrite existing sub \u2019%s::%s\u2019 with sub \u2019%s\u2019 exported by <br \/> %s<\/b><\/p>\n<p style=\"margin-left:17%;\">The fatal version of the above warning.<\/p>\n<p style=\"margin-left:11%;\"><b>Could not find sub \u2019%s\u2019 exported by %s<\/b><\/p>\n<p style=\"margin-left:17%;\">You requested to import a sub which the package does not provide.<\/p>\n<p style=\"margin-left:11%;\"><b>Cannot provide an \u2212as option for tags<\/b><\/p>\n<p style=\"margin-left:17%;\">Because a tag may provide more than one function, it does not make sense to request a single name for it. Instead use &#8220;\u2212prefix&#8221; or &#8220;\u2212suffix&#8221;.<\/p>\n<p style=\"margin-left:11%;\"><b>Passing options to unimport \u2019%s\u2019 makes no sense<\/b><\/p>\n<p style=\"margin-left:17%;\">When you import a sub, it occasionally makes sense to pass some options for it. However, when unimporting, options do nothing, so this warning is issued.<\/p>\n<h2>SEE ALSO <a name=\"SEE ALSO\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Exporter::Shiny, Exporter::Tiny.<\/p>\n<h2>AUTHOR <a name=\"AUTHOR\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">Toby Inkster <tobyink@cpan.org>.<\/p>\n<h2>COPYRIGHT AND LICENCE <a name=\"COPYRIGHT AND LICENCE\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\">This software is copyright (c) 2013\u22122014, 2017 by Toby Inkster.<\/p>\n<p style=\"margin-left:11%; margin-top: 1em\">This is free software; you can redistribute it and\/or modify it under the same terms as the Perl 5 programming language system itself.<\/p>\n<h2>DISCLAIMER OF WARRANTIES <a name=\"DISCLAIMER OF WARRANTIES\"><\/a> <\/h2>\n<p style=\"margin-left:11%; margin-top: 1em\"><small>THIS PACKAGE IS PROVIDED &#8220;AS IS&#8221; AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.<\/small><\/p>\n<hr>\n","protected":false},"excerpt":{"rendered":"<p>  Exporter::Tiny::Manual::Importing \u2212 importing from Exporter::Tiny\u2212based modules <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3154,3007],"class_list":["post-6783","post","type-post","status-publish","format-standard","hentry","category-sin-categoria","tag-exportertinymanualimporting","tag-man3"],"gutentor_comment":0,"_links":{"self":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6783","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/comments?post=6783"}],"version-history":[{"count":0,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/posts\/6783\/revisions"}],"wp:attachment":[{"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/media?parent=6783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/categories?post=6783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lode.uno\/linux-man\/wp-json\/wp\/v2\/tags?post=6783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}