perlop.1

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 1 代码 · 共 1,599 行 · 第 1/5 页

1
1,599
字号
.Vb 2\&    $a += 2;\&    $a *= 3;.Ve.PPSimilarly, a list assignment in list context produces the list oflvalues assigned to, and a list assignment in scalar context returnsthe number of elements produced by the expression on the right handside of the assignment..Sh "Comma Operator".IX Xref "comma operator, comma ,".IX Subsection "Comma Operator"Binary \*(L",\*(R" is the comma operator.  In scalar context it evaluatesits left argument, throws that value away, then evaluates its rightargument and returns that value.  This is just like C's comma operator..PPIn list context, it's just the list argument separator, and insertsboth its arguments into the list.  These arguments are also evaluatedfrom left to right..PPThe \f(CW\*(C`=>\*(C'\fR operator is a synonym for the comma, but forces any word(consisting entirely of word characters) to its left to be interpretedas a string (as of 5.001).  This includes words that might otherwise beconsidered a constant or function call..PP.Vb 1\&    use constant FOO => "something";\&\&    my %h = ( FOO => 23 );.Ve.PPis equivalent to:.PP.Vb 1\&    my %h = ("FOO", 23);.Ve.PPIt is \fI\s-1NOT\s0\fR:.PP.Vb 1\&    my %h = ("something", 23);.Ve.PPIf the argument on the left is not a word, it is first interpreted asan expression, and then the string value of that is used..PPThe \f(CW\*(C`=>\*(C'\fR operator is helpful in documenting the correspondencebetween keys and values in hashes, and other paired elements in lists..PP.Vb 2\&        %hash = ( $key => $value );\&        login( $username => $password );.Ve.Sh "List Operators (Rightward)".IX Xref "operator, list, rightward list operator".IX Subsection "List Operators (Rightward)"On the right side of a list operator, it has very low precedence,such that it controls all comma-separated expressions found there.The only operators with lower precedence are the logical operators\&\*(L"and\*(R", \*(L"or\*(R", and \*(L"not\*(R", which may be used to evaluate calls to listoperators without the need for extra parentheses:.PP.Vb 2\&    open HANDLE, "filename"\&        or die "Can\*(Aqt open: $!\en";.Ve.PPSee also discussion of list operators in \*(L"Terms and List Operators (Leftward)\*(R"..Sh "Logical Not".IX Xref "operator, logical, not not".IX Subsection "Logical Not"Unary \*(L"not\*(R" returns the logical negation of the expression to its right.It's the equivalent of \*(L"!\*(R" except for the very low precedence..Sh "Logical And".IX Xref "operator, logical, and and".IX Subsection "Logical And"Binary \*(L"and\*(R" returns the logical conjunction of the two surroundingexpressions.  It's equivalent to && except for the very lowprecedence.  This means that it short-circuits: i.e., the rightexpression is evaluated only if the left expression is true..Sh "Logical or, Defined or, and Exclusive Or".IX Xref "operator, logical, or operator, logical, xor operator, logical, defined or operator, logical, exclusive or or xor".IX Subsection "Logical or, Defined or, and Exclusive Or"Binary \*(L"or\*(R" returns the logical disjunction of the two surroundingexpressions.  It's equivalent to || except for the very low precedence.This makes it useful for control flow.PP.Vb 1\&    print FH $data              or die "Can\*(Aqt write to FH: $!";.Ve.PPThis means that it short-circuits: i.e., the right expression is evaluatedonly if the left expression is false.  Due to its precedence, you shouldprobably avoid using this for assignment, only for control flow..PP.Vb 3\&    $a = $b or $c;              # bug: this is wrong\&    ($a = $b) or $c;            # really means this\&    $a = $b || $c;              # better written this way.Ve.PPHowever, when it's a list-context assignment and you're trying to use\&\*(L"||\*(R" for control flow, you probably need \*(L"or\*(R" so that the assignmenttakes higher precedence..PP.Vb 2\&    @info = stat($file) || die;     # oops, scalar sense of stat!\&    @info = stat($file) or die;     # better, now @info gets its due.Ve.PPThen again, you could always use parentheses..PPBinary \*(L"xor\*(R" returns the exclusive-OR of the two surrounding expressions.It cannot short circuit, of course..Sh "C Operators Missing From Perl".IX Xref "operator, missing from perl & * typecasting (TYPE)".IX Subsection "C Operators Missing From Perl"Here is what C has that Perl doesn't:.IP "unary &" 8.IX Item "unary &"Address-of operator.  (But see the \*(L"\e\*(R" operator for taking a reference.).IP "unary *" 8.IX Item "unary *"Dereference-address operator. (Perl's prefix dereferencingoperators are typed: $, @, %, and &.).IP "(\s-1TYPE\s0)" 8.IX Item "(TYPE)"Type-casting operator..Sh "Quote and Quote-like Operators".IX Xref "operator, quote operator, quote-like q qq qx qw m qr s tr ' '' "" """" ` `` << escape sequence escape".IX Subsection "Quote and Quote-like Operators"While we usually think of quotes as literal values, in Perl theyfunction as operators, providing various kinds of interpolating andpattern matching capabilities.  Perl provides customary quote charactersfor these behaviors, but also provides a way for you to choose yourquote character for any of them.  In the following table, a \f(CW\*(C`{}\*(C'\fR representsany pair of delimiters you choose..PP.Vb 10\&    Customary  Generic        Meaning        Interpolates\&        \*(Aq\*(Aq       q{}          Literal             no\&        ""      qq{}          Literal             yes\&        \`\`      qx{}          Command             yes*\&                qw{}         Word list            no\&        //       m{}       Pattern match          yes*\&                qr{}          Pattern             yes*\&                 s{}{}      Substitution          yes*\&                tr{}{}    Transliteration         no (but see below)\&        <<EOF                 here\-doc            yes*\&\&        * unless the delimiter is \*(Aq\*(Aq..Ve.PPNon-bracketing delimiters use the same character fore and aft, but the foursorts of brackets (round, angle, square, curly) will all nest, which meansthat.PP.Vb 1\&        q{foo{bar}baz}.Ve.PPis the same as.PP.Vb 1\&        \*(Aqfoo{bar}baz\*(Aq.Ve.PPNote, however, that this does not always work for quoting Perl code:.PP.Vb 1\&        $s = q{ if($a eq "}") ... }; # WRONG.Ve.PPis a syntax error. The \f(CW\*(C`Text::Balanced\*(C'\fR module (from \s-1CPAN\s0, andstarting from Perl 5.8 part of the standard distribution) is ableto do this properly..PPThere can be whitespace between the operator and the quotingcharacters, except when \f(CW\*(C`#\*(C'\fR is being used as the quoting character.\&\f(CW\*(C`q#foo#\*(C'\fR is parsed as the string \f(CW\*(C`foo\*(C'\fR, while \f(CW\*(C`q #foo#\*(C'\fR is theoperator \f(CW\*(C`q\*(C'\fR followed by a comment.  Its argument will be takenfrom the next line.  This allows you to write:.PP.Vb 2\&    s {foo}  # Replace foo\&      {bar}  # with bar..Ve.PPThe following escape sequences are available in constructs that interpolateand in transliterations..IX Xref "\et \en \er \ef \eb \ea \ee \ex \e0 \ec \eN".PP.Vb 12\&    \et          tab             (HT, TAB)\&    \en          newline         (NL)\&    \er          return          (CR)\&    \ef          form feed       (FF)\&    \eb          backspace       (BS)\&    \ea          alarm (bell)    (BEL)\&    \ee          escape          (ESC)\&    \e033        octal char      (example: ESC)\&    \ex1b        hex char        (example: ESC)\&    \ex{263a}    wide hex char   (example: SMILEY)\&    \ec[         control char    (example: ESC)\&    \eN{name}    named Unicode character.Ve.PPThe character following \f(CW\*(C`\ec\*(C'\fR is mapped to some other character byconverting letters to upper case and then (on \s-1ASCII\s0 systems) by invertingthe 7th bit (0x40). The most interesting range is from '@' to '_'(0x40 through 0x5F), resulting in a control character from 0x00through 0x1F. A '?' maps to the \s-1DEL\s0 character. On \s-1EBCDIC\s0 systems only\&'@', the letters, '[', '\e', ']', '^', '_' and '?' will work, resultingin 0x00 through 0x1F and 0x7F..PP\&\fB\s-1NOTE\s0\fR: Unlike C and other languages, Perl has no \ev escape sequence forthe vertical tab (\s-1VT\s0 \- \s-1ASCII\s0 11), but you may use \f(CW\*(C`\eck\*(C'\fR or \f(CW\*(C`\ex0b\*(C'\fR..PPThe following escape sequences are available in constructs that interpolatebut not in transliterations..IX Xref "\el \eu \eL \eU \eE \eQ".PP.Vb 6\&    \el          lowercase next char\&    \eu          uppercase next char\&    \eL          lowercase till \eE\&    \eU          uppercase till \eE\&    \eE          end case modification\&    \eQ          quote non\-word characters till \eE.Ve.PPIf \f(CW\*(C`use locale\*(C'\fR is in effect, the case map used by \f(CW\*(C`\el\*(C'\fR, \f(CW\*(C`\eL\*(C'\fR,\&\f(CW\*(C`\eu\*(C'\fR and \f(CW\*(C`\eU\*(C'\fR is taken from the current locale.  See perllocale.If Unicode (for example, \f(CW\*(C`\eN{}\*(C'\fR or wide hex characters of 0x100 orbeyond) is being used, the case map used by \f(CW\*(C`\el\*(C'\fR, \f(CW\*(C`\eL\*(C'\fR, \f(CW\*(C`\eu\*(C'\fR and\&\f(CW\*(C`\eU\*(C'\fR is as defined by Unicode.  For documentation of \f(CW\*(C`\eN{name}\*(C'\fR,see charnames..PPAll systems use the virtual \f(CW"\en"\fR to represent a line terminator,called a \*(L"newline\*(R".  There is no such thing as an unvarying, physicalnewline character.  It is only an illusion that the operating system,device drivers, C libraries, and Perl all conspire to preserve.  Not allsystems read \f(CW"\er"\fR as \s-1ASCII\s0 \s-1CR\s0 and \f(CW"\en"\fR as \s-1ASCII\s0 \s-1LF\s0.  For example,on a Mac, these are reversed, and on systems without line terminator,printing \f(CW"\en"\fR may emit no actual data.  In general, use \f(CW"\en"\fR whenyou mean a \*(L"newline\*(R" for your system, but use the literal \s-1ASCII\s0 when youneed an exact character.  For example, most networking protocols expectand prefer a \s-1CR+LF\s0 (\f(CW"\e015\e012"\fR or \f(CW"\ecM\ecJ"\fR) for line terminators,and although they often accept just \f(CW"\e012"\fR, they seldom tolerate just\&\f(CW"\e015"\fR.  If you get in the habit of using \f(CW"\en"\fR for networking,you may be burned some day..IX Xref "newline line terminator eol end of line \en \er \er\en".PPFor constructs that do interpolate, variables beginning with "\f(CW\*(C`$\*(C'\fR\*(L"or \*(R"\f(CW\*(C`@\*(C'\fR" are interpolated.  Subscripted variables such as \f(CW$a[3]\fR or\&\f(CW\*(C`$href\->{key}[0]\*(C'\fR are also interpolated, as are array and hash slices.But method calls such as \f(CW\*(C`$obj\->meth\*(C'\fR are not..PPInterpolating an array or slice interpolates the elements in order,separated by the value of \f(CW$"\fR, so is equivalent to interpolating\&\f(CW\*(C`join $", @array\*(C'\fR.    \*(L"Punctuation\*(R" arrays such as \f(CW\*(C`@*\*(C'\fR are onlyinterpolated if the name is enclosed in braces \f(CW\*(C`@{*}\*(C'\fR, but specialarrays \f(CW@_\fR, \f(CW\*(C`@+\*(C'\fR, and \f(CW\*(C`@\-\*(C'\fR are interpolated, even without braces..PPYou cannot include a literal \f(CW\*(C`$\*(C'\fR or \f(CW\*(C`@\*(C'\fR within a \f(CW\*(C`\eQ\*(C'\fR sequence.An unescaped \f(CW\*(C`$\*(C'\fR or \f(CW\*(C`@\*(C'\fR interpolates the corresponding variable,while escaping will cause the literal string \f(CW\*(C`\e$\*(C'\fR to be inserted.You'll need to write something like \f(CW\*(C`m/\eQuser\eE\e@\eQhost/\*(C'\fR..PPPatterns are subject to an additional level of interpretation as aregular expression.  This is done as a second pass, after variables areinterpolated, so that regular expressions may be incorporated into thepattern from the variables.  If this is not what you want, use \f(CW\*(C`\eQ\*(C'\fR tointerpolate a variable literally..PPApart from the behavior described above, Perl does not expandmultiple levels of interpolation.  In particular, contrary to theexpectations of shell programmers, back-quotes do \fI\s-1NOT\s0\fR interpolatewithin double quotes, nor do single quotes impede evaluation ofvariables when used within double quotes..Sh "Regexp Quote-Like Operators".IX Xref "operator, regexp".IX Subsection "Regexp Quote-Like Operators"Here are the quote-like operators that apply to patternmatching and related activities..IP "qr/STRING/msixpo" 8.IX Xref "qr  i  m  o  s  x  p".IX Item "qr/STRING/msixpo"This operator quotes (and possibly compiles) its \fI\s-1STRING\s0\fR as a regularexpression.  \fI\s-1STRING\s0\fR is interpolated the same way as \fI\s-1PATTERN\s0\fRin \f(CW\*(C`m/PATTERN/\*(C'\fR.  If \*(L"'\*(R" is used as the delimiter, no interpolationis done.  Returns a Perl value which may be used instead of thecorresponding \f(CW\*(C`/STRING/msixpo\*(C'\fR expression. The returned value is anormalized version of the original pattern. It magically differs froma string containing the same characters: \f(CW\*(C`ref(qr/x/)\*(C'\fR returns \*(L"Regexp\*(R",even though dereferencing the result returns undef..SpFor example,.Sp.Vb 3\&    $rex = qr/my.STRING/is;\&    print $rex;                 # prints (?si\-xm:my.STRING)\&    s/$rex/foo/;.Ve.Spis equivalent to.Sp.Vb 1\&    s/my.STRING/foo/is;.Ve.SpThe result may be used as a subpattern in a match:.Sp.Vb 4\&    $re = qr/$pattern/;\&    $string =~ /foo${re}bar/;   # can be interpolated in other patterns\&    $string =~ $re;             # or used standalone\&    $string =~ /$re/;           # or this way.Ve.Sp

⌨️ 快捷键说明

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