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

📄 perlsyn.1

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 1
📖 第 1 页 / 共 3 页
字号:
.Vb 11\&    if (EXPR) BLOCK\&    if (EXPR) BLOCK else BLOCK\&    if (EXPR) BLOCK elsif (EXPR) BLOCK ... else BLOCK\&    LABEL while (EXPR) BLOCK\&    LABEL while (EXPR) BLOCK continue BLOCK\&    LABEL until (EXPR) BLOCK\&    LABEL until (EXPR) BLOCK continue BLOCK\&    LABEL for (EXPR; EXPR; EXPR) BLOCK\&    LABEL foreach VAR (LIST) BLOCK\&    LABEL foreach VAR (LIST) BLOCK continue BLOCK\&    LABEL BLOCK continue BLOCK.Ve.PPNote that, unlike C and Pascal, these are defined in terms of BLOCKs,not statements.  This means that the curly brackets are \fIrequired\fR\-\-nodangling statements allowed.  If you want to write conditionals withoutcurly brackets there are several other ways to do it.  The followingall do the same thing:.PP.Vb 5\&    if (!open(FOO)) { die "Can\*(Aqt open $FOO: $!"; }\&    die "Can\*(Aqt open $FOO: $!" unless open(FOO);\&    open(FOO) or die "Can\*(Aqt open $FOO: $!";     # FOO or bust!\&    open(FOO) ? \*(Aqhi mom\*(Aq : die "Can\*(Aqt open $FOO: $!";\&                        # a bit exotic, that last one.Ve.PPThe \f(CW\*(C`if\*(C'\fR statement is straightforward.  Because BLOCKs are alwaysbounded by curly brackets, there is never any ambiguity about which\&\f(CW\*(C`if\*(C'\fR an \f(CW\*(C`else\*(C'\fR goes with.  If you use \f(CW\*(C`unless\*(C'\fR in place of \f(CW\*(C`if\*(C'\fR,the sense of the test is reversed..PPThe \f(CW\*(C`while\*(C'\fR statement executes the block as long as the expression istrue.The \f(CW\*(C`until\*(C'\fR statement executes the block as long as the expression isfalse.The \s-1LABEL\s0 is optional, and if present, consists of an identifier followedby a colon.  The \s-1LABEL\s0 identifies the loop for the loop controlstatements \f(CW\*(C`next\*(C'\fR, \f(CW\*(C`last\*(C'\fR, and \f(CW\*(C`redo\*(C'\fR.If the \s-1LABEL\s0 is omitted, the loop control statementrefers to the innermost enclosing loop.  This may include dynamicallylooking back your call-stack at run time to find the \s-1LABEL\s0.  Suchdesperate behavior triggers a warning if you use the \f(CW\*(C`use warnings\*(C'\fRpragma or the \fB\-w\fR flag..PPIf there is a \f(CW\*(C`continue\*(C'\fR \s-1BLOCK\s0, it is always executed just before theconditional is about to be evaluated again.  Thus it can be used toincrement a loop variable, even when the loop has been continued viathe \f(CW\*(C`next\*(C'\fR statement..Sh "Loop Control".IX Xref "loop control loop, control next last redo continue".IX Subsection "Loop Control"The \f(CW\*(C`next\*(C'\fR command starts the next iteration of the loop:.PP.Vb 4\&    LINE: while (<STDIN>) {\&        next LINE if /^#/;      # discard comments\&        ...\&    }.Ve.PPThe \f(CW\*(C`last\*(C'\fR command immediately exits the loop in question.  The\&\f(CW\*(C`continue\*(C'\fR block, if any, is not executed:.PP.Vb 4\&    LINE: while (<STDIN>) {\&        last LINE if /^$/;      # exit when done with header\&        ...\&    }.Ve.PPThe \f(CW\*(C`redo\*(C'\fR command restarts the loop block without evaluating theconditional again.  The \f(CW\*(C`continue\*(C'\fR block, if any, is \fInot\fR executed.This command is normally used by programs that want to lie to themselvesabout what was just input..PPFor example, when processing a file like \fI/etc/termcap\fR.If your input lines might end in backslashes to indicate continuation, youwant to skip ahead and get the next record..PP.Vb 8\&    while (<>) {\&        chomp;\&        if (s/\e\e$//) {\&            $_ .= <>;\&            redo unless eof();\&        }\&        # now process $_\&    }.Ve.PPwhich is Perl short-hand for the more explicitly written version:.PP.Vb 8\&    LINE: while (defined($line = <ARGV>)) {\&        chomp($line);\&        if ($line =~ s/\e\e$//) {\&            $line .= <ARGV>;\&            redo LINE unless eof(); # not eof(ARGV)!\&        }\&        # now process $line\&    }.Ve.PPNote that if there were a \f(CW\*(C`continue\*(C'\fR block on the above code, it wouldget executed only on lines discarded by the regex (since redo skips thecontinue block). A continue block is often used to reset line countersor \f(CW\*(C`?pat?\*(C'\fR one-time matches:.PP.Vb 10\&    # inspired by :1,$g/fred/s//WILMA/\&    while (<>) {\&        ?(fred)?    && s//WILMA $1 WILMA/;\&        ?(barney)?  && s//BETTY $1 BETTY/;\&        ?(homer)?   && s//MARGE $1 MARGE/;\&    } continue {\&        print "$ARGV $.: $_";\&        close ARGV  if eof();           # reset $.\&        reset       if eof();           # reset ?pat?\&    }.Ve.PPIf the word \f(CW\*(C`while\*(C'\fR is replaced by the word \f(CW\*(C`until\*(C'\fR, the sense of thetest is reversed, but the conditional is still tested before the firstiteration..PPThe loop control statements don't work in an \f(CW\*(C`if\*(C'\fR or \f(CW\*(C`unless\*(C'\fR, sincethey aren't loops.  You can double the braces to make them such, though..PP.Vb 5\&    if (/pattern/) {{\&        last if /fred/;\&        next if /barney/; # same effect as "last", but doesn\*(Aqt document as well\&        # do something here\&    }}.Ve.PPThis is caused by the fact that a block by itself acts as a loop thatexecutes once, see \*(L"Basic BLOCKs\*(R"..PPThe form \f(CW\*(C`while/if BLOCK BLOCK\*(C'\fR, available in Perl 4, is no longeravailable.   Replace any occurrence of \f(CW\*(C`if BLOCK\*(C'\fR by \f(CW\*(C`if (do BLOCK)\*(C'\fR..Sh "For Loops".IX Xref "for foreach".IX Subsection "For Loops"Perl's C\-style \f(CW\*(C`for\*(C'\fR loop works like the corresponding \f(CW\*(C`while\*(C'\fR loop;that means that this:.PP.Vb 3\&    for ($i = 1; $i < 10; $i++) {\&        ...\&    }.Ve.PPis the same as this:.PP.Vb 6\&    $i = 1;\&    while ($i < 10) {\&        ...\&    } continue {\&        $i++;\&    }.Ve.PPThere is one minor difference: if variables are declared with \f(CW\*(C`my\*(C'\fRin the initialization section of the \f(CW\*(C`for\*(C'\fR, the lexical scope ofthose variables is exactly the \f(CW\*(C`for\*(C'\fR loop (the body of the loopand the control sections)..IX Xref "my".PPBesides the normal array index looping, \f(CW\*(C`for\*(C'\fR can lend itselfto many other interesting applications.  Here's one that avoids theproblem you get into if you explicitly test for end-of-file onan interactive file descriptor causing your program to appear tohang..IX Xref "eof end-of-file end of file".PP.Vb 5\&    $on_a_tty = \-t STDIN && \-t STDOUT;\&    sub prompt { print "yes? " if $on_a_tty }\&    for ( prompt(); <STDIN>; prompt() ) {\&        # do something\&    }.Ve.PPUsing \f(CW\*(C`readline\*(C'\fR (or the operator form, \f(CW\*(C`<EXPR>\*(C'\fR) as theconditional of a \f(CW\*(C`for\*(C'\fR loop is shorthand for the following.  Thisbehaviour is the same as a \f(CW\*(C`while\*(C'\fR loop conditional..IX Xref "readline <>".PP.Vb 3\&    for ( prompt(); defined( $_ = <STDIN> ); prompt() ) {\&        # do something\&    }.Ve.Sh "Foreach Loops".IX Xref "for foreach".IX Subsection "Foreach Loops"The \f(CW\*(C`foreach\*(C'\fR loop iterates over a normal list value and sets thevariable \s-1VAR\s0 to be each element of the list in turn.  If the variableis preceded with the keyword \f(CW\*(C`my\*(C'\fR, then it is lexically scoped, andis therefore visible only within the loop.  Otherwise, the variable isimplicitly local to the loop and regains its former value upon exitingthe loop.  If the variable was previously declared with \f(CW\*(C`my\*(C'\fR, it usesthat variable instead of the global one, but it's still localized tothe loop.  This implicit localisation occurs \fIonly\fR in a \f(CW\*(C`foreach\*(C'\fRloop..IX Xref "my local".PPThe \f(CW\*(C`foreach\*(C'\fR keyword is actually a synonym for the \f(CW\*(C`for\*(C'\fR keyword, soyou can use \f(CW\*(C`foreach\*(C'\fR for readability or \f(CW\*(C`for\*(C'\fR for brevity.  (Or becausethe Bourne shell is more familiar to you than \fIcsh\fR, so writing \f(CW\*(C`for\*(C'\fRcomes more naturally.)  If \s-1VAR\s0 is omitted, \f(CW$_\fR is set to each value..IX Xref "$_".PPIf any element of \s-1LIST\s0 is an lvalue, you can modify it by modifying\&\s-1VAR\s0 inside the loop.  Conversely, if any element of \s-1LIST\s0 is \s-1NOT\s0 anlvalue, any attempt to modify that element will fail.  In other words,the \f(CW\*(C`foreach\*(C'\fR loop index variable is an implicit alias for each itemin the list that you're looping over..IX Xref "alias".PPIf any part of \s-1LIST\s0 is an array, \f(CW\*(C`foreach\*(C'\fR will get very confused ifyou add or remove elements within the loop body, for example with\&\f(CW\*(C`splice\*(C'\fR.   So don't do that..IX Xref "splice".PP\&\f(CW\*(C`foreach\*(C'\fR probably won't do what you expect if \s-1VAR\s0 is a tied or otherspecial variable.   Don't do that either..PPExamples:.PP.Vb 1\&    for (@ary) { s/foo/bar/ }\&\&    for my $elem (@elements) {\&        $elem *= 2;\&    }\&\&    for $count (10,9,8,7,6,5,4,3,2,1,\*(AqBOOM\*(Aq) {\&        print $count, "\en"; sleep(1);\&    }\&\&    for (1..15) { print "Merry Christmas\en"; }\&\&    foreach $item (split(/:[\e\e\en:]*/, $ENV{TERMCAP})) {\&        print "Item: $item\en";\&    }.Ve.PPHere's how a C programmer might code up a particular algorithm in Perl:.PP.Vb 9\&    for (my $i = 0; $i < @ary1; $i++) {\&        for (my $j = 0; $j < @ary2; $j++) {\&            if ($ary1[$i] > $ary2[$j]) {\&                last; # can\*(Aqt go to outer :\-(\&            }\&            $ary1[$i] += $ary2[$j];\&        }\&        # this is where that last takes me\&    }.Ve.PPWhereas here's how a Perl programmer more comfortable with the idiom mightdo it:.PP.Vb 6\&    OUTER: for my $wid (@ary1) {\&    INNER:   for my $jet (@ary2) {\&                next OUTER if $wid > $jet;\&                $wid += $jet;\&             }\&          }.Ve.PPSee how much easier this is?  It's cleaner, safer, and faster.  It'scleaner because it's less noisy.  It's safer because if code gets addedbetween the inner and outer loops later on, the new code won't beaccidentally executed.  The \f(CW\*(C`next\*(C'\fR explicitly iterates the other looprather than merely terminating the inner one.  And it's faster becausePerl executes a \f(CW\*(C`foreach\*(C'\fR statement more rapidly than it would theequivalent \f(CW\*(C`for\*(C'\fR loop..Sh "Basic BLOCKs".IX Xref "block".IX Subsection "Basic BLOCKs"A \s-1BLOCK\s0 by itself (labeled or not) is semantically equivalent to aloop that executes once.  Thus you can use any of the loop controlstatements in it to leave or restart the block.  (Note that this is\&\fI\s-1NOT\s0\fR true in \f(CW\*(C`eval{}\*(C'\fR, \f(CW\*(C`sub{}\*(C'\fR, or contrary to popular belief\&\f(CW\*(C`do{}\*(C'\fR blocks, which do \fI\s-1NOT\s0\fR count as loops.)  The \f(CW\*(C`continue\*(C'\fRblock is optional..PPThe \s-1BLOCK\s0 construct can be used to emulate case structures..PP.Vb 6\&    SWITCH: {\&        if (/^abc/) { $abc = 1; last SWITCH; }\&        if (/^def/) { $def = 1; last SWITCH; }\&        if (/^xyz/) { $xyz = 1; last SWITCH; }\&        $nothing = 1;\&    }.Ve.PPSuch constructs are quite frequently used, because older versionsof Perl had no official \f(CW\*(C`switch\*(C'\fR statement..Sh "Switch statements".IX Xref "switch case given when default".IX Subsection "Switch statements"Starting from Perl 5.10, you can say.PP.Vb 1\&    use feature "switch";.Ve.PPwhich enables a switch feature that is closely based on thePerl 6 proposal..PPThe keywords \f(CW\*(C`given\*(C'\fR and \f(CW\*(C`when\*(C'\fR are analogousto \f(CW\*(C`switch\*(C'\fR and \f(CW\*(C`case\*(C'\fR in other languages, so the codeabove could be written as.PP.Vb 6\&    given($_) {\&        when (/^abc/) { $abc = 1; }\&        when (/^def/) { $def = 1; }\&        when (/^xyz/) { $xyz = 1; }\&        default { $nothing = 1; }\&    }.Ve.PPThis construct is very flexible and powerful. For example:.PP.Vb 5\&    use feature ":5.10";\&    given($foo) {\&        when (undef) {\&            say \*(Aq$foo is undefined\*(Aq;\&        }\&        \&        when ("foo") {\&            say \*(Aq$foo is the string "foo"\*(Aq;\&        }\&        \&        when ([1,3,5,7,9]) {\&            say \*(Aq$foo is an odd digit\*(Aq;\&            continue; # Fall through\&        }\&        \&        when ($_ < 100) {\&            say \*(Aq$foo is numerically less than 100\*(Aq;\&        }\&        \&        when (\e&complicated_check) {\&            say \*(Aqcomplicated_check($foo) is true\*(Aq;\&        }

⌨️ 快捷键说明

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