⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perlreftut.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 2 页
字号:
.\" 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 "PERLREFTUT 1".TH PERLREFTUT 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"perlreftut \- Mark's very short tutorial about references.SH "DESCRIPTION".IX Header "DESCRIPTION"One of the most important new features in Perl 5 was the capability tomanage complicated data structures like multidimensional arrays andnested hashes.  To enable these, Perl 5 introduced a feature called`references', and using references is the key to managing complicated,structured data in Perl.  Unfortunately, there's a lot of funny syntaxto learn, and the main manual page can be hard to follow.  The manualis quite complete, and sometimes people find that a problem, becauseit can be hard to tell what is important and what isn't..PPFortunately, you only need to know 10% of what's in the main page to get90% of the benefit.  This page will show you that 10%..SH "Who Needs Complicated Data Structures?".IX Header "Who Needs Complicated Data Structures?"One problem that came up all the time in Perl 4 was how to represent ahash whose values were lists.  Perl 4 had hashes, of course, but thevalues had to be scalars; they couldn't be lists..PPWhy would you want a hash of lists?  Let's take a simple example: Youhave a file of city and country names, like this:.PP.Vb 6\&        Chicago, USA\&        Frankfurt, Germany\&        Berlin, Germany\&        Washington, USA\&        Helsinki, Finland\&        New York, USA.Ve.PPand you want to produce an output like this, with each country mentionedonce, and then an alphabetical list of the cities in that country:.PP.Vb 3\&        Finland: Helsinki.\&        Germany: Berlin, Frankfurt.\&        USA:  Chicago, New York, Washington..Ve.PPThe natural way to do this is to have a hash whose keys are countrynames.  Associated with each country name key is a list of the cities inthat country.  Each time you read a line of input, split it into a countryand a city, look up the list of cities already known to be in thatcountry, and append the new city to the list.  When you're done readingthe input, iterate over the hash as usual, sorting each list of citiesbefore you print it out..PPIf hash values can't be lists, you lose.  In Perl 4, hash values can'tbe lists; they can only be strings.  You lose.  You'd probably have tocombine all the cities into a single string somehow, and then whentime came to write the output, you'd have to break the string into alist, sort the list, and turn it back into a string.  This is messyand error-prone.  And it's frustrating, because Perl already hasperfectly good lists that would solve the problem if only you coulduse them..SH "The Solution".IX Header "The Solution"By the time Perl 5 rolled around, we were already stuck with thisdesign: Hash values must be scalars.  The solution to this isreferences..PPA reference is a scalar value that \fIrefers to\fR an entire array or anentire hash (or to just about anything else).  Names are one kind ofreference that you're already familiar with.  Think of the Presidentof the United States: a messy, inconvenient bag of blood and bones.But to talk about him, or to represent him in a computer program, allyou need is the easy, convenient scalar string \*(L"George Bush\*(R"..PPReferences in Perl are like names for arrays and hashes.  They'rePerl's private, internal names, so you can be sure they'reunambiguous.  Unlike \*(L"George Bush\*(R", a reference only refers to onething, and you always know what it refers to.  If you have a referenceto an array, you can recover the entire array from it.  If you have areference to a hash, you can recover the entire hash.  But thereference is still an easy, compact scalar value..PPYou can't have a hash whose values are arrays; hash values can only bescalars.  We're stuck with that.  But a single reference can refer toan entire array, and references are scalars, so you can have a hash ofreferences to arrays, and it'll act a lot like a hash of arrays, andit'll be just as useful as a hash of arrays..PPWe'll come back to this city-country problem later, after we've seensome syntax for managing references..SH "Syntax".IX Header "Syntax"There are just two ways to make a reference, and just two ways to useit once you have it..Sh "Making References".IX Subsection "Making References"\fI\f(BIMake Rule 1\fI\fR.IX Subsection "Make Rule 1".PPIf you put a \f(CW\*(C`\e\*(C'\fR in front of a variable, you get areference to that variable..PP.Vb 3\&    $aref = \e@array;         # $aref now holds a reference to @array\&    $href = \e%hash;          # $href now holds a reference to %hash\&    $sref = \e$scalar;        # $sref now holds a reference to $scalar.Ve.PPOnce the reference is stored in a variable like \f(CW$aref\fR or \f(CW$href\fR, youcan copy it or store it just the same as any other scalar value:.PP.Vb 3\&    $xy = $aref;             # $xy now holds a reference to @array\&    $p[3] = $href;           # $p[3] now holds a reference to %hash\&    $z = $p[3];              # $z now holds a reference to %hash.Ve.PPThese examples show how to make references to variables with names.Sometimes you want to make an array or a hash that doesn't have aname.  This is analogous to the way you like to be able to use thestring \f(CW"\en"\fR or the number 80 without having to store it in a namedvariable first..PP\&\fBMake Rule 2\fR.PP\&\f(CW\*(C`[ ITEMS ]\*(C'\fR makes a new, anonymous array, and returns a reference tothat array.  \f(CW\*(C`{ ITEMS }\*(C'\fR makes a new, anonymous hash, and returns areference to that hash..PP.Vb 2\&    $aref = [ 1, "foo", undef, 13 ];\&    # $aref now holds a reference to an array\&\&    $href = { APR => 4, AUG => 8 };\&    # $href now holds a reference to a hash.Ve.PPThe references you get from rule 2 are the same kind ofreferences that you get from rule 1:.PP.Vb 2\&        # This:\&        $aref = [ 1, 2, 3 ];\&\&        # Does the same as this:\&        @array = (1, 2, 3);\&        $aref = \e@array;.Ve.PPThe first line is an abbreviation for the following two lines, exceptthat it doesn't create the superfluous array variable \f(CW@array\fR..PPIf you write just \f(CW\*(C`[]\*(C'\fR, you get a new, empty anonymous array.If you write just \f(CW\*(C`{}\*(C'\fR, you get a new, empty anonymous hash..Sh "Using References".IX Subsection "Using References"What can you do with a reference once you have it?  It's a scalarvalue, and we've seen that you can store it as a scalar and get it backagain just like any scalar.  There are just two more ways to use it:.PP\fI\f(BIUse Rule 1\fI\fR.IX Subsection "Use Rule 1".PPYou can always use an array reference, in curly braces, in place ofthe name of an array.  For example, \f(CW\*(C`@{$aref}\*(C'\fR instead of \f(CW@array\fR..PPHere are some examples of that:.PPArrays:.PP.Vb 4\&        @a              @{$aref}                An array\&        reverse @a      reverse @{$aref}        Reverse the array\&        $a[3]           ${$aref}[3]             An element of the array\&        $a[3] = 17;     ${$aref}[3] = 17        Assigning an element.Ve.PPOn each line are two expressions that do the same thing.  Theleft-hand versions operate on the array \f(CW@a\fR.  The right-handversions operate on the array that is referred to by \f(CW$aref\fR.  Oncethey find the array they're operating on, both versions do the samethings to the arrays..PPUsing a hash reference is \fIexactly\fR the same:.PP.Vb 4\&        %h              %{$href}              A hash\&        keys %h         keys %{$href}         Get the keys from the hash\&        $h{\*(Aqred\*(Aq}       ${$href}{\*(Aqred\*(Aq}       An element of the hash\&        $h{\*(Aqred\*(Aq} = 17  ${$href}{\*(Aqred\*(Aq} = 17  Assigning an element.Ve.PPWhatever you want to do with a reference, \fBUse Rule 1\fR tells you howto do it.  You just write the Perl code that you would have writtenfor doing the same thing to a regular array or hash, and then replacethe array or hash name with \f(CW\*(C`{$reference}\*(C'\fR.  \*(L"How do I loop over anarray when all I have is a reference?\*(R"  Well, to loop over an array, youwould write.PP.Vb 3\&        for my $element (@array) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -