📄 exporter.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "Exporter 3".TH Exporter 3 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"Exporter \- Implements default import method for modules.SH "SYNOPSIS".IX Header "SYNOPSIS"In module YourModule.pm:.PP.Vb 4\& package YourModule;\& require Exporter;\& @ISA = qw(Exporter);\& @EXPORT_OK = qw(munge frobnicate); # symbols to export on request.Ve.PPor.PP.Vb 3\& package YourModule;\& use Exporter \*(Aqimport\*(Aq; # gives you Exporter\*(Aqs import() method directly\& @EXPORT_OK = qw(munge frobnicate); # symbols to export on request.Ve.PPIn other files which wish to use YourModule:.PP.Vb 2\& use ModuleName qw(frobnicate); # import listed symbols\& frobnicate ($left, $right) # calls YourModule::frobnicate.Ve.PPTake a look at \*(L"Good Practices\*(R" for some variantsyou will like to use in modern Perl code..SH "DESCRIPTION".IX Header "DESCRIPTION"The Exporter module implements an \f(CW\*(C`import\*(C'\fR method which allows a moduleto export functions and variables to its users' namespaces. Many modulesuse Exporter rather than implementing their own \f(CW\*(C`import\*(C'\fR method becauseExporter provides a highly flexible interface, with an implementation optimisedfor the common case..PPPerl automatically calls the \f(CW\*(C`import\*(C'\fR method when processing a\&\f(CW\*(C`use\*(C'\fR statement for a module. Modules and \f(CW\*(C`use\*(C'\fR are documentedin perlfunc and perlmod. Understanding the concept ofmodules and how the \f(CW\*(C`use\*(C'\fR statement operates is important tounderstanding the Exporter..Sh "How to Export".IX Subsection "How to Export"The arrays \f(CW@EXPORT\fR and \f(CW@EXPORT_OK\fR in a module hold lists ofsymbols that are going to be exported into the users name space bydefault, or which they can request to be exported, respectively. Thesymbols can represent functions, scalars, arrays, hashes, or typeglobs.The symbols must be given by full name with the exception that theampersand in front of a function is optional, e.g..PP.Vb 2\& @EXPORT = qw(afunc $scalar @array); # afunc is a function\& @EXPORT_OK = qw(&bfunc %hash *typeglob); # explicit prefix on &bfunc.Ve.PPIf you are only exporting function names it is recommended to omit theampersand, as the implementation is faster this way..Sh "Selecting What To Export".IX Subsection "Selecting What To Export"Do \fBnot\fR export method names!.PPDo \fBnot\fR export anything else by default without a good reason!.PPExports pollute the namespace of the module user. If you must exporttry to use \f(CW@EXPORT_OK\fR in preference to \f(CW@EXPORT\fR and avoid short orcommon symbol names to reduce the risk of name clashes..PPGenerally anything not exported is still accessible from outside themodule using the ModuleName::item_name (or \f(CW$blessed_ref\fR\->method)syntax. By convention you can use a leading underscore on names toinformally indicate that they are 'internal' and not for public use..PP(It is actually possible to get private functions by saying:.PP.Vb 3\& my $subref = sub { ... };\& $subref\->(@args); # Call it as a function\& $obj\->$subref(@args); # Use it as a method.Ve.PPHowever if you use them for methods it is up to you to figure outhow to make inheritance work.).PPAs a general rule, if the module is trying to be object orientedthen export nothing. If it's just a collection of functions then\&\f(CW@EXPORT_OK\fR anything but use \f(CW@EXPORT\fR with caution. For function andmethod names use barewords in preference to names prefixed withampersands for the export lists..PPOther module design guidelines can be found in perlmod..Sh "How to Import".IX Subsection "How to Import"In other files which wish to use your module there are three basic ways forthem to load your module and import its symbols:.ie n .IP """use ModuleName;""" 4.el .IP "\f(CWuse ModuleName;\fR" 4.IX Item "use ModuleName;"This imports all the symbols from ModuleName's \f(CW@EXPORT\fR into the namespaceof the \f(CW\*(C`use\*(C'\fR statement..ie n .IP """use ModuleName ();""" 4.el .IP "\f(CWuse ModuleName ();\fR" 4.IX Item "use ModuleName ();"This causes perl to load your module but does not import any symbols..ie n .IP """use ModuleName qw(...);""" 4.el .IP "\f(CWuse ModuleName qw(...);\fR" 4.IX Item "use ModuleName qw(...);"This imports only the symbols listed by the caller into their namespace.All listed symbols must be in your \f(CW@EXPORT\fR or \f(CW@EXPORT_OK\fR, else an erroroccurs. The advanced export features of Exporter are accessed like this,but with list entries that are syntactically distinct from symbol names..PPUnless you want to use its advanced features, this is probably all youneed to know to use Exporter..SH "Advanced features".IX Header "Advanced features".Sh "Specialised Import Lists".IX Subsection "Specialised Import Lists"If any of the entries in an import list begins with !, : or / thenthe list is treated as a series of specifications which either add toor delete from the list of names to import. They are processed left toright. Specifications are in the form:.PP.Vb 4\& [!]name This name only\& [!]:DEFAULT All names in @EXPORT\& [!]:tag All names in $EXPORT_TAGS{tag} anonymous list\& [!]/pattern/ All names in @EXPORT and @EXPORT_OK which match.Ve.PPA leading ! indicates that matching names should be deleted from thelist of names to import. If the first specification is a deletion itis treated as though preceded by :DEFAULT. If you just want to importextra names in addition to the default set you will still need toinclude :DEFAULT explicitly..PPe.g., Module.pm defines:.PP.Vb 3\& @EXPORT = qw(A1 A2 A3 A4 A5);\& @EXPORT_OK = qw(B1 B2 B3 B4 B5);\& %EXPORT_TAGS = (T1 => [qw(A1 A2 B1 B2)], T2 => [qw(A1 A2 B3 B4)]);\&\& Note that you cannot use tags in @EXPORT or @EXPORT_OK.\& Names in EXPORT_TAGS must also appear in @EXPORT or @EXPORT_OK..Ve.PPAn application using Module can say something like:.PP.Vb 1\& use Module qw(:DEFAULT :T2 !B3 A3);.Ve.PPOther examples include:.PP.Vb 2\& use Socket qw(!/^[AP]F_/ !SOMAXCONN !SOL_SOCKET);\& use POSIX qw(:errno_h :termios_h !TCSADRAIN !/^EXIT/);.Ve.PPRemember that most patterns (using //) will need to be anchoredwith a leading ^, e.g., \f(CW\*(C`/^EXIT/\*(C'\fR rather than \f(CW\*(C`/EXIT/\*(C'\fR..PPYou can say \f(CW\*(C`BEGIN { $Exporter::Verbose=1 }\*(C'\fR to see how thespecifications are being processed and what is actually being importedinto modules..Sh "Exporting without using Exporter's import method".IX Subsection "Exporting without using Exporter's import method"Exporter has a special method, 'export_to_level' which is used in situationswhere you can't directly call Exporter's import method. The export_to_levelmethod looks like:.PP.Vb 1\& MyPackage\->export_to_level($where_to_export, $package, @what_to_export);.Ve.PPwhere \f(CW$where_to_export\fR is an integer telling how far up the calling stackto export your symbols, and \f(CW@what_to_export\fR is an array telling whatsymbols *to* export (usually this is \f(CW@_\fR). The \f(CW$package\fR argument iscurrently unused..PP
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -