📄 perlsub.1
字号:
.\" 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 "PERLSUB 1".TH PERLSUB 1 "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"perlsub \- Perl subroutines.IX Xref "subroutine function".SH "SYNOPSIS".IX Header "SYNOPSIS"To declare subroutines:.IX Xref "subroutine, declaration sub".PP.Vb 4\& sub NAME; # A "forward" declaration.\& sub NAME(PROTO); # ditto, but with prototypes\& sub NAME : ATTRS; # with attributes\& sub NAME(PROTO) : ATTRS; # with attributes and prototypes\&\& sub NAME BLOCK # A declaration and a definition.\& sub NAME(PROTO) BLOCK # ditto, but with prototypes\& sub NAME : ATTRS BLOCK # with attributes\& sub NAME(PROTO) : ATTRS BLOCK # with prototypes and attributes.Ve.PPTo define an anonymous subroutine at runtime:.IX Xref "subroutine, anonymous".PP.Vb 4\& $subref = sub BLOCK; # no proto\& $subref = sub (PROTO) BLOCK; # with proto\& $subref = sub : ATTRS BLOCK; # with attributes\& $subref = sub (PROTO) : ATTRS BLOCK; # with proto and attributes.Ve.PPTo import subroutines:.IX Xref "import".PP.Vb 1\& use MODULE qw(NAME1 NAME2 NAME3);.Ve.PPTo call subroutines:.IX Xref "subroutine, call call".PP.Vb 4\& NAME(LIST); # & is optional with parentheses.\& NAME LIST; # Parentheses optional if predeclared/imported.\& &NAME(LIST); # Circumvent prototypes.\& &NAME; # Makes current @_ visible to called subroutine..Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"Like many languages, Perl provides for user-defined subroutines.These may be located anywhere in the main program, loaded in fromother files via the \f(CW\*(C`do\*(C'\fR, \f(CW\*(C`require\*(C'\fR, or \f(CW\*(C`use\*(C'\fR keywords, orgenerated on the fly using \f(CW\*(C`eval\*(C'\fR or anonymous subroutines.You can even call a function indirectly using a variable containingits name or a \s-1CODE\s0 reference..PPThe Perl model for function call and return values is simple: allfunctions are passed as parameters one single flat list of scalars, andall functions likewise return to their caller one single flat list ofscalars. Any arrays or hashes in these call and return lists willcollapse, losing their identities\*(--but you may always usepass-by-reference instead to avoid this. Both call and return lists maycontain as many or as few scalar elements as you'd like. (Often afunction without an explicit return statement is called a subroutine, butthere's really no difference from Perl's perspective.).IX Xref "subroutine, parameter parameter".PPAny arguments passed in show up in the array \f(CW@_\fR. Therefore, ifyou called a function with two arguments, those would be stored in\&\f(CW$_[0]\fR and \f(CW$_[1]\fR. The array \f(CW@_\fR is a local array, but itselements are aliases for the actual scalar parameters. In particular,if an element \f(CW$_[0]\fR is updated, the corresponding argument isupdated (or an error occurs if it is not updatable). If an argumentis an array or hash element which did not exist when the functionwas called, that element is created only when (and if) it is modifiedor a reference to it is taken. (Some earlier versions of Perlcreated the element whether or not the element was assigned to.)Assigning to the whole array \f(CW@_\fR removes that aliasing, and doesnot update any arguments..IX Xref "subroutine, argument argument @_".PPA \f(CW\*(C`return\*(C'\fR statement may be used to exit a subroutine, optionallyspecifying the returned value, which will be evaluated in theappropriate context (list, scalar, or void) depending on the context ofthe subroutine call. If you specify no return value, the subroutinereturns an empty list in list context, the undefined value in scalarcontext, or nothing in void context. If you return one or moreaggregates (arrays and hashes), these will be flattened together intoone large indistinguishable list..PPIf no \f(CW\*(C`return\*(C'\fR is found and if the last statement is an expression, itsvalue is returned. If the last statement is a loop control structurelike a \f(CW\*(C`foreach\*(C'\fR or a \f(CW\*(C`while\*(C'\fR, the returned value is unspecified. Theempty sub returns the empty list..IX Xref "subroutine, return value return value return".PPPerl does not have named formal parameters. In practice all youdo is assign to a \f(CW\*(C`my()\*(C'\fR list of these. Variables that aren'tdeclared to be private are global variables. For gory detailson creating private variables, see \*(L"Private Variables via \fImy()\fR\*(R"and \*(L"Temporary Values via \fIlocal()\fR\*(R". To create protectedenvironments for a set of functions in a separate package (andprobably a separate file), see \*(L"Packages\*(R" in perlmod..IX Xref "formal parameter parameter, formal".PPExample:.PP.Vb 8\& sub max {\& my $max = shift(@_);\& foreach $foo (@_) {\& $max = $foo if $max < $foo;\& }\& return $max;\& }\& $bestday = max($mon,$tue,$wed,$thu,$fri);.Ve.PPExample:.PP.Vb 2\& # get a line, combining continuation lines\& # that start with whitespace\&\& sub get_line {\& $thisline = $lookahead; # global variables!\& LINE: while (defined($lookahead = <STDIN>)) {\& if ($lookahead =~ /^[ \et]/) {\& $thisline .= $lookahead;\& }\& else {\& last LINE;\& }\& }\& return $thisline;\& }\&\& $lookahead = <STDIN>; # get first line\& while (defined($line = get_line())) {\& ...\& }.Ve.PPAssigning to a list of private variables to name your arguments:.PP.Vb 4\& sub maybeset {\& my($key, $value) = @_;\& $Foo{$key} = $value unless $Foo{$key};\& }.Ve.PPBecause the assignment copies the values, this also has the effectof turning call-by-reference into call-by-value. Otherwise afunction is free to do in-place modifications of \f(CW@_\fR and changeits caller's values..IX Xref "call-by-reference call-by-value".PP.Vb 4\& upcase_in($v1, $v2); # this changes $v1 and $v2\& sub upcase_in {\& for (@_) { tr/a\-z/A\-Z/ }\& }.Ve.PPYou aren't allowed to modify constants in this way, of course. If anargument were actually literal and you tried to change it, you'd take a(presumably fatal) exception. For example, this won't work:.IX Xref "call-by-reference call-by-value".PP.Vb 1\& upcase_in("frederick");.Ve.PPIt would be much safer if the \f(CW\*(C`upcase_in()\*(C'\fR functionwere written to return a copy of its parameters insteadof changing them in place:.PP.Vb 7\& ($v3, $v4) = upcase($v1, $v2); # this doesn\*(Aqt change $v1 and $v2\& sub upcase {\& return unless defined wantarray; # void context, do nothing\& my @parms = @_;\& for (@parms) { tr/a\-z/A\-Z/ }\& return wantarray ? @parms : $parms[0];\& }.Ve.PPNotice how this (unprototyped) function doesn't care whether it waspassed real scalars or arrays. Perl sees all arguments as one big,long, flat parameter list in \f(CW@_\fR. This is one area wherePerl's simple argument-passing style shines. The \f(CW\*(C`upcase()\*(C'\fRfunction would work perfectly well without changing the \f(CW\*(C`upcase()\*(C'\fRdefinition even if we fed it things like this:.PP.Vb 2\& @newlist = upcase(@list1, @list2);\& @newlist = upcase( split /:/, $var );.Ve.PPDo not, however, be tempted to do this:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -