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

📄 perldsc.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 3 页
字号:
.PP.Vb 4\&    for $i (1..10) {\&        @array = 0 .. $i;\&        @{$AoA[$i]} = @array;\&    }.Ve.PPIs it the same?  Well, maybe so\*(--and maybe not.  The subtle differenceis that when you assign something in square brackets, you know for sureit's always a brand new reference with a new \fIcopy\fR of the data.Something else could be going on in this new case with the \f(CW\*(C`@{$AoA[$i]}\*(C'\fRdereference on the left-hand-side of the assignment.  It all depends onwhether \f(CW$AoA[$i]\fR had been undefined to start with, or whether italready contained a reference.  If you had already populated \f(CW@AoA\fR withreferences, as in.PP.Vb 1\&    $AoA[3] = \e@another_array;.Ve.PPThen the assignment with the indirection on the left-hand-side woulduse the existing reference that was already there:.PP.Vb 1\&    @{$AoA[3]} = @array;.Ve.PPOf course, this \fIwould\fR have the \*(L"interesting\*(R" effect of clobbering\&\f(CW@another_array\fR.  (Have you ever noticed how when a programmer sayssomething is \*(L"interesting\*(R", that rather than meaning \*(L"intriguing\*(R",they're disturbingly more apt to mean that it's \*(L"annoying\*(R",\&\*(L"difficult\*(R", or both?  :\-).PPSo just remember always to use the array or hash constructors with \f(CW\*(C`[]\*(C'\fRor \f(CW\*(C`{}\*(C'\fR, and you'll be fine, although it's not always optimallyefficient..PPSurprisingly, the following dangerous-looking construct willactually work out fine:.PP.Vb 4\&    for $i (1..10) {\&        my @array = somefunc($i);\&        $AoA[$i] = \e@array;\&    }.Ve.PPThat's because \fImy()\fR is more of a run-time statement than it is acompile-time declaration \fIper se\fR.  This means that the \fImy()\fR variable isremade afresh each time through the loop.  So even though it \fIlooks\fR asthough you stored the same variable reference each time, you actually didnot!  This is a subtle distinction that can produce more efficient code atthe risk of misleading all but the most experienced of programmers.  So Iusually advise against teaching it to beginners.  In fact, except forpassing arguments to functions, I seldom like to see the gimme-a-referenceoperator (backslash) used much at all in code.  Instead, I advisebeginners that they (and most of the rest of us) should try to use themuch more easily understood constructors \f(CW\*(C`[]\*(C'\fR and \f(CW\*(C`{}\*(C'\fR instead ofrelying upon lexical (or dynamic) scoping and hidden reference-counting todo the right thing behind the scenes..PPIn summary:.PP.Vb 3\&    $AoA[$i] = [ @array ];      # usually best\&    $AoA[$i] = \e@array;         # perilous; just how my() was that array?\&    @{ $AoA[$i] } = @array;     # way too tricky for most programmers.Ve.SH "CAVEAT ON PRECEDENCE".IX Xref "dereference, precedence dereferencing, precedence".IX Header "CAVEAT ON PRECEDENCE"Speaking of things like \f(CW\*(C`@{$AoA[$i]}\*(C'\fR, the following are actually thesame thing:.IX Xref "->".PP.Vb 2\&    $aref\->[2][2]       # clear\&    $$aref[2][2]        # confusing.Ve.PPThat's because Perl's precedence rules on its five prefix dereferencers(which look like someone swearing: \f(CW\*(C`$ @ * % &\*(C'\fR) make them bind moretightly than the postfix subscripting brackets or braces!  This will nodoubt come as a great shock to the C or \*(C+ programmer, who is quiteaccustomed to using \f(CW*a[i]\fR to mean what's pointed to by the \fIi'th\fRelement of \f(CW\*(C`a\*(C'\fR.  That is, they first take the subscript, and only thendereference the thing at that subscript.  That's fine in C, but this isn't C..PPThe seemingly equivalent construct in Perl, \f(CW$$aref[$i]\fR first doesthe deref of \f(CW$aref\fR, making it take \f(CW$aref\fR as a reference to anarray, and then dereference that, and finally tell you the \fIi'th\fR valueof the array pointed to by \f(CW$AoA\fR. If you wanted the C notion, you'd have towrite \f(CW\*(C`${$AoA[$i]}\*(C'\fR to force the \f(CW$AoA[$i]\fR to get evaluated firstbefore the leading \f(CW\*(C`$\*(C'\fR dereferencer..ie n .SH "WHY YOU SHOULD ALWAYS ""use strict""".el .SH "WHY YOU SHOULD ALWAYS \f(CWuse strict\fP".IX Header "WHY YOU SHOULD ALWAYS use strict"If this is starting to sound scarier than it's worth, relax.  Perl hassome features to help you avoid its most common pitfalls.  The bestway to avoid getting confused is to start every program like this:.PP.Vb 2\&    #!/usr/bin/perl \-w\&    use strict;.Ve.PPThis way, you'll be forced to declare all your variables with \fImy()\fR andalso disallow accidental \*(L"symbolic dereferencing\*(R".  Therefore if you'd donethis:.PP.Vb 5\&    my $aref = [\&        [ "fred", "barney", "pebbles", "bambam", "dino", ],\&        [ "homer", "bart", "marge", "maggie", ],\&        [ "george", "jane", "elroy", "judy", ],\&    ];\&\&    print $aref[2][2];.Ve.PPThe compiler would immediately flag that as an error \fIat compile time\fR,because you were accidentally accessing \f(CW@aref\fR, an undeclaredvariable, and it would thereby remind you to write instead:.PP.Vb 1\&    print $aref\->[2][2].Ve.SH "DEBUGGING".IX Xref "data structure, debugging complex data structure, debugging AoA, debugging HoA, debugging AoH, debugging HoH, debugging array of arrays, debugging hash of arrays, debugging array of hashes, debugging hash of hashes, debugging".IX Header "DEBUGGING"Before version 5.002, the standard Perl debugger didn't do a very nice job ofprinting out complex data structures.  With 5.002 or above, thedebugger includes several new features, including command line editing aswell as the \f(CW\*(C`x\*(C'\fR command to dump out complex data structures.  Forexample, given the assignment to \f(CW$AoA\fR above, here's the debugger output:.PP.Vb 10\&    DB<1> x $AoA\&    $AoA = ARRAY(0x13b5a0)\&       0  ARRAY(0x1f0a24)\&          0  \*(Aqfred\*(Aq\&          1  \*(Aqbarney\*(Aq\&          2  \*(Aqpebbles\*(Aq\&          3  \*(Aqbambam\*(Aq\&          4  \*(Aqdino\*(Aq\&       1  ARRAY(0x13b558)\&          0  \*(Aqhomer\*(Aq\&          1  \*(Aqbart\*(Aq\&          2  \*(Aqmarge\*(Aq\&          3  \*(Aqmaggie\*(Aq\&       2  ARRAY(0x13b540)\&          0  \*(Aqgeorge\*(Aq\&          1  \*(Aqjane\*(Aq\&          2  \*(Aqelroy\*(Aq\&          3  \*(Aqjudy\*(Aq.Ve.SH "CODE EXAMPLES".IX Header "CODE EXAMPLES"Presented with little comment (these will get their own manpages someday)here are short code examples illustrating access of varioustypes of data structures..SH "ARRAYS OF ARRAYS".IX Xref "array of arrays AoA".IX Header "ARRAYS OF ARRAYS".Sh "Declaration of an \s-1ARRAY\s0 \s-1OF\s0 \s-1ARRAYS\s0".IX Subsection "Declaration of an ARRAY OF ARRAYS".Vb 5\& @AoA = (\&        [ "fred", "barney" ],\&        [ "george", "jane", "elroy" ],\&        [ "homer", "marge", "bart" ],\&      );.Ve.Sh "Generation of an \s-1ARRAY\s0 \s-1OF\s0 \s-1ARRAYS\s0".IX Subsection "Generation of an ARRAY OF ARRAYS".Vb 4\& # reading from file\& while ( <> ) {\&     push @AoA, [ split ];\& }\&\& # calling a function\& for $i ( 1 .. 10 ) {\&     $AoA[$i] = [ somefunc($i) ];\& }\&\& # using temp vars\& for $i ( 1 .. 10 ) {\&     @tmp = somefunc($i);\&     $AoA[$i] = [ @tmp ];\& }\&\& # add to an existing row\& push @{ $AoA[0] }, "wilma", "betty";.Ve.Sh "Access and Printing of an \s-1ARRAY\s0 \s-1OF\s0 \s-1ARRAYS\s0".IX Subsection "Access and Printing of an ARRAY OF ARRAYS".Vb 2\& # one element\& $AoA[0][0] = "Fred";\&\& # another element\& $AoA[1][1] =~ s/(\ew)/\eu$1/;\&\& # print the whole thing with refs\& for $aref ( @AoA ) {\&     print "\et [ @$aref ],\en";\& }\&\& # print the whole thing with indices\& for $i ( 0 .. $#AoA ) {\&     print "\et [ @{$AoA[$i]} ],\en";\& }\&\& # print the whole thing one at a time\& for $i ( 0 .. $#AoA ) {\&     for $j ( 0 .. $#{ $AoA[$i] } ) {\&         print "elt $i $j is $AoA[$i][$j]\en";\&     }\& }.Ve.SH "HASHES OF ARRAYS".IX Xref "hash of arrays HoA".IX Header "HASHES OF ARRAYS".Sh "Declaration of a \s-1HASH\s0 \s-1OF\s0 \s-1ARRAYS\s0".IX Subsection "Declaration of a HASH OF ARRAYS".Vb 5\& %HoA = (\&        flintstones        => [ "fred", "barney" ],\&        jetsons            => [ "george", "jane", "elroy" ],\&        simpsons           => [ "homer", "marge", "bart" ],\&      );.Ve.Sh "Generation of a \s-1HASH\s0 \s-1OF\s0 \s-1ARRAYS\s0".IX Subsection "Generation of a HASH OF ARRAYS".Vb 6\& # reading from file\& # flintstones: fred barney wilma dino\& while ( <> ) {\&     next unless s/^(.*?):\es*//;\&     $HoA{$1} = [ split ];\& }\&\& # reading from file; more temps\& # flintstones: fred barney wilma dino\& while ( $line = <> ) {\&     ($who, $rest) = split /:\es*/, $line, 2;\&     @fields = split \*(Aq \*(Aq, $rest;\&     $HoA{$who} = [ @fields ];\& }\&\& # calling a function that returns a list\& for $group ( "simpsons", "jetsons", "flintstones" ) {\&     $HoA{$group} = [ get_family($group) ];\& }\&\& # likewise, but using temps\& for $group ( "simpsons", "jetsons", "flintstones" ) {\&     @members = get_family($group);\&     $HoA{$group} = [ @members ];\& }\&\& # append new members to an existing family\& push @{ $HoA{"flintstones"} }, "wilma", "betty";.Ve.Sh "Access and Printing of a \s-1HASH\s0 \s-1OF\s0 \s-1ARRAYS\s0".IX Subsection "Access and Printing of a HASH OF ARRAYS".Vb 2\& # one element\& $HoA{flintstones}[0] = "Fred";\&\& # another element\& $HoA{simpsons}[1] =~ s/(\ew)/\eu$1/;\&\& # print the whole thing\& foreach $family ( keys %HoA ) {\&     print "$family: @{ $HoA{$family} }\en"\& }\&\& # print the whole thing with indices\& foreach $family ( keys %HoA ) {\&     print "family: ";\&     foreach $i ( 0 .. $#{ $HoA{$family} } ) {\&         print " $i = $HoA{$family}[$i]";\&     }\&     print "\en";\& }\&\& # print the whole thing sorted by number of members\& foreach $family ( sort { @{$HoA{$b}} <=> @{$HoA{$a}} } keys %HoA ) {\&     print "$family: @{ $HoA{$family} }\en"\& }\&\& # print the whole thing sorted by number of members and name\& foreach $family ( sort {\&                            @{$HoA{$b}} <=> @{$HoA{$a}}\&                                        ||\&                                    $a cmp $b\&            } keys %HoA )\& {\&     print "$family: ", join(", ", sort @{ $HoA{$family} }), "\en";\& }.Ve.SH "ARRAYS OF HASHES".IX Xref "array of hashes AoH".IX Header "ARRAYS OF HASHES".Sh "Declaration of an \s-1ARRAY\s0 \s-1OF\s0 \s-1HASHES\s0".IX Subsection "Declaration of an ARRAY OF HASHES".Vb 10\& @AoH = (\&        {\&            Lead     => "fred",\&            Friend   => "barney",\&        },\&        {\&            Lead     => "george",\&            Wife     => "jane",\&            Son      => "elroy",\&        },\&        {\&            Lead     => "homer",\&            Wife     => "marge",\&            Son      => "bart",\&        }\&  );.Ve.Sh "Generation of an \s-1ARRAY\s0 \s-1OF\s0 \s-1HASHES\s0".IX Subsection "Generation of an ARRAY OF HASHES".Vb 10\& # reading from file\& # format: LEAD=fred FRIEND=barney\& while ( <> ) {\&     $rec = {};\&     for $field ( split ) {\&         ($key, $value) = split /=/, $field;\&         $rec\->{$key} = $value;\&     }\&     push @AoH, $rec;\& }

⌨️ 快捷键说明

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