📄 perlref.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 "PERLREF 1".TH PERLREF 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"perlref \- Perl references and nested data structures.IX Xref "reference pointer data structure structure struct".SH "NOTE".IX Header "NOTE"This is complete documentation about all aspects of references.For a shorter, tutorial introduction to just the essential features,see perlreftut..SH "DESCRIPTION".IX Header "DESCRIPTION"Before release 5 of Perl it was difficult to represent complex datastructures, because all references had to be symbolic\*(--and even thenit was difficult to refer to a variable instead of a symbol table entry.Perl now not only makes it easier to use symbolic references to variables,but also lets you have \*(L"hard\*(R" references to any piece of data or code.Any scalar may hold a hard reference. Because arrays and hashes containscalars, you can now easily build arrays of arrays, arrays of hashes,hashes of arrays, arrays of hashes of functions, and so on..PPHard references are smart\*(--they keep track of reference counts for you,automatically freeing the thing referred to when its reference count goesto zero. (Reference counts for values in self-referential orcyclic data structures may not go to zero without a little help; see\&\*(L"Two-Phased Garbage Collection\*(R" in perlobj for a detailed explanation.)If that thing happens to be an object, the object is destructed. Seeperlobj for more about objects. (In a sense, everything in Perl is anobject, but we usually reserve the word for references to objects thathave been officially \*(L"blessed\*(R" into a class package.).PPSymbolic references are names of variables or other objects, just as asymbolic link in a Unix filesystem contains merely the name of a file.The \f(CW*glob\fR notation is something of a symbolic reference. (Symbolicreferences are sometimes called \*(L"soft references\*(R", but please don't callthem that; references are confusing enough without useless synonyms.).IX Xref "reference, symbolic reference, soft symbolic reference soft reference".PPIn contrast, hard references are more like hard links in a Unix filesystem: They are used to access an underlying object without concern forwhat its (other) name is. When the word \*(L"reference\*(R" is used without anadjective, as in the following paragraph, it is usually talking about ahard reference..IX Xref "reference, hard hard reference".PPReferences are easy to use in Perl. There is just one overridingprinciple: Perl does no implicit referencing or dereferencing. When ascalar is holding a reference, it always behaves as a simple scalar. Itdoesn't magically start being an array or hash or subroutine; you have totell it explicitly to do so, by dereferencing it..Sh "Making References".IX Xref "reference, creation referencing".IX Subsection "Making References"References can be created in several ways..IP "1." 4.IX Xref "\e backslash".IX Item "1."By using the backslash operator on a variable, subroutine, or value.(This works much like the & (address-of) operator in C.) This typically creates \fIanother\fR reference to a variable, becausethere's already a reference to the variable in the symbol table. Butthe symbol table reference might go away, and you'll still have thereference that the backslash returned. Here are some examples:.Sp.Vb 5\& $scalarref = \e$foo;\& $arrayref = \e@ARGV;\& $hashref = \e%ENV;\& $coderef = \e&handler;\& $globref = \e*foo;.Ve.SpIt isn't possible to create a true reference to an \s-1IO\s0 handle (filehandleor dirhandle) using the backslash operator. The most you can get is areference to a typeglob, which is actually a complete symbol table entry.But see the explanation of the \f(CW*foo{THING}\fR syntax below. However,you can still use type globs and globrefs as though they were \s-1IO\s0 handles..IP "2." 4.IX Xref "array, anonymous [ [] square bracket bracket, square arrayref array reference reference, array".IX Item "2."A reference to an anonymous array can be created using squarebrackets:.Sp.Vb 1\& $arrayref = [1, 2, [\*(Aqa\*(Aq, \*(Aqb\*(Aq, \*(Aqc\*(Aq]];.Ve.SpHere we've created a reference to an anonymous array of three elementswhose final element is itself a reference to another anonymous array of threeelements. (The multidimensional syntax described later can be used toaccess this. For example, after the above, \f(CW\*(C`$arrayref\->[2][1]\*(C'\fR would havethe value \*(L"b\*(R".).SpTaking a reference to an enumerated list is not the sameas using square brackets\*(--instead it's the same as creatinga list of references!.Sp.Vb 2\& @list = (\e$a, \e@b, \e%c);\& @list = \e($a, @b, %c); # same thing!.Ve.SpAs a special case, \f(CW\*(C`\e(@foo)\*(C'\fR returns a list of references to the contentsof \f(CW@foo\fR, not a reference to \f(CW@foo\fR itself. Likewise for \f(CW%foo\fR,except that the key references are to copies (since the keys are juststrings rather than full-fledged scalars)..IP "3." 4.IX Xref "hash, anonymous { {} curly bracket bracket, curly brace hashref hash reference reference, hash".IX Item "3."A reference to an anonymous hash can be created using curlybrackets:.Sp.Vb 4\& $hashref = {\& \*(AqAdam\*(Aq => \*(AqEve\*(Aq,\& \*(AqClyde\*(Aq => \*(AqBonnie\*(Aq,\& };.Ve.SpAnonymous hash and array composers like these can be intermixed freely toproduce as complicated a structure as you want. The multidimensionalsyntax described below works for these too. The values above areliterals, but variables and expressions would work just as well, becauseassignment operators in Perl (even within \fIlocal()\fR or \fImy()\fR) are executablestatements, not compile-time declarations..SpBecause curly brackets (braces) are used for several other thingsincluding BLOCKs, you may occasionally have to disambiguate braces at thebeginning of a statement by putting a \f(CW\*(C`+\*(C'\fR or a \f(CW\*(C`return\*(C'\fR in front sothat Perl realizes the opening brace isn't starting a \s-1BLOCK\s0. The economy andmnemonic value of using curlies is deemed worth this occasional extrahassle..SpFor example, if you wanted a function to make a new hash and return areference to it, you have these options:.Sp.Vb 3\& sub hashem { { @_ } } # silently wrong\& sub hashem { +{ @_ } } # ok\& sub hashem { return { @_ } } # ok.Ve.SpOn the other hand, if you want the other meaning, you can do this:.Sp.Vb 3\& sub showem { { @_ } } # ambiguous (currently ok, but may change)\& sub showem { {; @_ } } # ok\& sub showem { { return @_ } } # ok.Ve.SpThe leading \f(CW\*(C`+{\*(C'\fR and \f(CW\*(C`{;\*(C'\fR always serve to disambiguatethe expression to mean either the \s-1HASH\s0 reference, or the \s-1BLOCK\s0..IP "4." 4.IX Xref "subroutine, anonymous subroutine, reference reference, subroutine scope, lexical closure lexical lexical scope".IX Item "4."
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -