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

📄 switch.3

📁 视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.
💻 3
📖 第 1 页 / 共 2 页
字号:
.PPTogether these two new statements provide a fully generalized casemechanism:.PP.Vb 1\&        use Switch;\&\&        # AND LATER...\&\&        %special = ( woohoo => 1,  d\*(Aqoh => 1 );\&\&        while (<>) {\&            chomp;\&            switch ($_) {\&                case (%special) { print "homer\en"; }      # if $special{$_}\&                case /[a\-z]/i   { print "alpha\en"; }      # if $_ =~ /a\-z/i\&                case [1..9]     { print "small num\en"; }  # if $_ in [1..9]\&                case { $_[0] >= 10 } { print "big num\en"; } # if $_ >= 10\&                print "must be punctuation\en" case /\eW/;  # if $_ ~= /\eW/\&            }\&        }.Ve.PPNote that \f(CW\*(C`switch\*(C'\fRes can be nested within \f(CW\*(C`case\*(C'\fR (or any other) blocks,and a series of \f(CW\*(C`case\*(C'\fR statements can try different types of matches\&\*(-- hash membership, pattern match, array intersection, simple equality,etc. \*(-- against the same switch value..PPThe use of intersection tests against an array reference is particularlyuseful for aggregating integral cases:.PP.Vb 8\&        sub classify_digit\&        {\&                switch ($_[0]) { case 0            { return \*(Aqzero\*(Aq }\&                                 case [2,4,6,8]    { return \*(Aqeven\*(Aq }\&                                 case [1,3,5,7,9]  { return \*(Aqodd\*(Aq }\&                                 case /[A\-F]/i     { return \*(Aqhex\*(Aq }\&                               }\&        }.Ve.Sh "Allowing fall-through".IX Subsection "Allowing fall-through"Fall-though (trying another case after one has already succeeded)is usually a Bad Idea in a switch statement. However, thisis Perl, not a police state, so there \fIis\fR a way to do it, if you must..PPIf a \f(CW\*(C`case\*(C'\fR block executes an untargeted \f(CW\*(C`next\*(C'\fR, control isimmediately transferred to the statement \fIafter\fR the \f(CW\*(C`case\*(C'\fR statement(i.e. usually another case), rather than out of the surrounding\&\f(CW\*(C`switch\*(C'\fR block..PPFor example:.PP.Vb 7\&        switch ($val) {\&                case 1      { handle_num_1(); next }    # and try next case...\&                case "1"    { handle_str_1(); next }    # and try next case...\&                case [0..9] { handle_num_any(); }       # and we\*(Aqre done\&                case /\ed/   { handle_dig_any(); next }  # and try next case...\&                case /.*/   { handle_str_any(); next }  # and try next case...\&        }.Ve.PPIf \f(CW$val\fR held the number \f(CW1\fR, the above \f(CW\*(C`switch\*(C'\fR block would call thefirst three \f(CW\*(C`handle_...\*(C'\fR subroutines, jumping to the next case testeach time it encountered a \f(CW\*(C`next\*(C'\fR. After the third \f(CW\*(C`case\*(C'\fR blockwas executed, control would jump to the end of the enclosing\&\f(CW\*(C`switch\*(C'\fR block..PPOn the other hand, if \f(CW$val\fR held \f(CW10\fR, then only the last two \f(CW\*(C`handle_...\*(C'\fRsubroutines would be called..PPNote that this mechanism allows the notion of \fIconditional fall-through\fR.For example:.PP.Vb 4\&        switch ($val) {\&                case [0..9] { handle_num_any(); next if $val < 7; }\&                case /\ed/   { handle_dig_any(); }\&        }.Ve.PPIf an untargeted \f(CW\*(C`last\*(C'\fR statement is executed in a case block, thisimmediately transfers control out of the enclosing \f(CW\*(C`switch\*(C'\fR block(in other words, there is an implicit \f(CW\*(C`last\*(C'\fR at the end of eachnormal \f(CW\*(C`case\*(C'\fR block). Thus the previous example could also have beenwritten:.PP.Vb 4\&        switch ($val) {\&                case [0..9] { handle_num_any(); last if $val >= 7; next; }\&                case /\ed/   { handle_dig_any(); }\&        }.Ve.Sh "Automating fall-through".IX Subsection "Automating fall-through"In situations where case fall-through should be the norm, rather than anexception, an endless succession of terminal \f(CW\*(C`next\*(C'\fRs is tedious and ugly.Hence, it is possible to reverse the default behaviour by specifyingthe string \*(L"fallthrough\*(R" when importing the module. For example, the following code is equivalent to the first example in \*(L"Allowing fall-through\*(R":.PP.Vb 1\&        use Switch \*(Aqfallthrough\*(Aq;\&\&        switch ($val) {\&                case 1      { handle_num_1(); }\&                case "1"    { handle_str_1(); }\&                case [0..9] { handle_num_any(); last }\&                case /\ed/   { handle_dig_any(); }\&                case /.*/   { handle_str_any(); }\&        }.Ve.PPNote the explicit use of a \f(CW\*(C`last\*(C'\fR to preserve the non-fall-throughbehaviour of the third case..Sh "Alternative syntax".IX Subsection "Alternative syntax"Perl 6 will provide a built-in switch statement with essentially thesame semantics as those offered by Switch.pm, but with a differentpair of keywords. In Perl 6 \f(CW\*(C`switch\*(C'\fR will be spelled \f(CW\*(C`given\*(C'\fR, and\&\f(CW\*(C`case\*(C'\fR will be pronounced \f(CW\*(C`when\*(C'\fR. In addition, the \f(CW\*(C`when\*(C'\fR statementwill not require switch or case values to be parenthesized..PPThis future syntax is also (largely) available via the Switch.pm module, byimporting it with the argument \f(CW"Perl6"\fR.  For example:.PP.Vb 1\&        use Switch \*(AqPerl6\*(Aq;\&\&        given ($val) {\&                when 1       { handle_num_1(); }\&                when ($str1) { handle_str_1(); }\&                when [0..9]  { handle_num_any(); last }\&                when /\ed/    { handle_dig_any(); }\&                when /.*/    { handle_str_any(); }\&                default      { handle anything else; }\&        }.Ve.PPNote that scalars still need to be parenthesized, since they would beambiguous in Perl 5..PPNote too that you can mix and match both syntaxes by importing the modulewith:.PP.Vb 1\&        use Switch \*(AqPerl5\*(Aq, \*(AqPerl6\*(Aq;.Ve.Sh "Higher-order Operations".IX Subsection "Higher-order Operations"One situation in which \f(CW\*(C`switch\*(C'\fR and \f(CW\*(C`case\*(C'\fR do not provide a goodsubstitute for a cascaded \f(CW\*(C`if\*(C'\fR, is where a switch value needs tobe tested against a series of conditions. For example:.PP.Vb 11\&        sub beverage {\&            switch (shift) {\&                case { $_[0] < 10 } { return \*(Aqmilk\*(Aq }\&                case { $_[0] < 20 } { return \*(Aqcoke\*(Aq }\&                case { $_[0] < 30 } { return \*(Aqbeer\*(Aq }\&                case { $_[0] < 40 } { return \*(Aqwine\*(Aq }\&                case { $_[0] < 50 } { return \*(Aqmalt\*(Aq }\&                case { $_[0] < 60 } { return \*(AqMoet\*(Aq }\&                else                { return \*(Aqmilk\*(Aq }\&            }\&        }.Ve.PP(This is equivalent to writing \f(CW\*(C`case (sub { $_[0] < 10 })\*(C'\fR, etc.; \f(CW$_[0]\fRis the argument to the anonymous subroutine.).PPThe need to specify each condition as a subroutine block is tiresome. Toovercome this, when importing Switch.pm, a special \*(L"placeholder\*(R"subroutine named \f(CW\*(C`_\|_\*(C'\fR [sic] may also be imported. This subroutineconverts (almost) any expression in which it appears to a reference to ahigher-order function. That is, the expression:.PP.Vb 1\&        use Switch \*(Aq_\|_\*(Aq;\&\&        _\|_ < 2.Ve.PPis equivalent to:.PP.Vb 1\&        sub { $_[0] < 2 }.Ve.PPWith \f(CW\*(C`_\|_\*(C'\fR, the previous ugly case statements can be rewritten:.PP.Vb 7\&        case  _\|_ < 10  { return \*(Aqmilk\*(Aq }\&        case  _\|_ < 20  { return \*(Aqcoke\*(Aq }\&        case  _\|_ < 30  { return \*(Aqbeer\*(Aq }\&        case  _\|_ < 40  { return \*(Aqwine\*(Aq }\&        case  _\|_ < 50  { return \*(Aqmalt\*(Aq }\&        case  _\|_ < 60  { return \*(AqMoet\*(Aq }\&        else           { return \*(Aqmilk\*(Aq }.Ve.PPThe \f(CW\*(C`_\|_\*(C'\fR subroutine makes extensive use of operator overloading toperform its magic. All operations involving _\|_ are overloaded toproduce an anonymous subroutine that implements a lazy versionof the original operation..PPThe only problem is that operator overloading does not allow theboolean operators \f(CW\*(C`&&\*(C'\fR and \f(CW\*(C`||\*(C'\fR to be overloaded. So a case statementlike this:.PP.Vb 1\&        case  0 <= _\|_ && _\|_ < 10  { return \*(Aqdigit\*(Aq }.Ve.PPdoesn't act as expected, because when it isexecuted, it constructs two higher order subroutinesand then treats the two resulting references as arguments to \f(CW\*(C`&&\*(C'\fR:.PP.Vb 1\&        sub { 0 <= $_[0] } && sub { $_[0] < 10 }.Ve.PPThis boolean expression is inevitably true, since both references arenon-false. Fortunately, the overloaded \f(CW\*(Aqbool\*(Aq\fR operator catches thissituation and flags it as a error..SH "DEPENDENCIES".IX Header "DEPENDENCIES"The module is implemented using Filter::Util::Call and Text::Balancedand requires both these modules to be installed..SH "AUTHOR".IX Header "AUTHOR"Damian Conway (damian@conway.org). The maintainer of this module is now RafaelGarcia-Suarez (rgarciasuarez@gmail.com)..SH "BUGS".IX Header "BUGS"There are undoubtedly serious bugs lurking somewhere in code this funky :\-)Bug reports and other feedback are most welcome..SH "LIMITATIONS".IX Header "LIMITATIONS"Due to the heuristic nature of Switch.pm's source parsing, the presence ofregexes with embedded newlines that are specified with raw \f(CW\*(C`/.../\*(C'\fRdelimiters and don't have a modifier \f(CW\*(C`//x\*(C'\fR are indistinguishable fromcode chunks beginning with the division operator \f(CW\*(C`/\*(C'\fR. As a workaroundyou must use \f(CW\*(C`m/.../\*(C'\fR or \f(CW\*(C`m?...?\*(C'\fR for such patterns. Also, the presenceof regexes specified with raw \f(CW\*(C`?...?\*(C'\fR delimiters may cause mysteriouserrors. The workaround is to use \f(CW\*(C`m?...?\*(C'\fR instead..PPDue to the way source filters work in Perl, you can't use Switch insidean string \f(CW\*(C`eval\*(C'\fR..PPIf your source file is longer then 1 million characters and you have aswitch statement that crosses the 1 million (or 2 million, etc.)character boundary you will get mysterious errors. The workaround is touse smaller source files..SH "COPYRIGHT".IX Header "COPYRIGHT".Vb 3\&    Copyright (c) 1997\-2006, Damian Conway. All Rights Reserved.\&    This module is free software. It may be used, redistributed\&        and/or modified under the same terms as Perl itself..Ve

⌨️ 快捷键说明

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