perlop.1
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· 1 代码 · 共 1,599 行 · 第 1/5 页
1
1,599 行
.IX Subsection "C-style Logical Or"Binary \*(L"||\*(R" performs a short-circuit logical \s-1OR\s0 operation. That is,if the left operand is true, the right operand is not even evaluated.Scalar or list context propagates down to the right operand if itis evaluated..Sh "C\-style Logical Defined-Or".IX Xref "operator, logical, defined-or".IX Subsection "C-style Logical Defined-Or"Although it has no direct equivalent in C, Perl's \f(CW\*(C`//\*(C'\fR operator is relatedto its C\-style or. In fact, it's exactly the same as \f(CW\*(C`||\*(C'\fR, except that ittests the left hand side's definedness instead of its truth. Thus, \f(CW\*(C`$a // $b\*(C'\fRis similar to \f(CW\*(C`defined($a) || $b\*(C'\fR (except that it returns the value of \f(CW$a\fRrather than the value of \f(CW\*(C`defined($a)\*(C'\fR) and is exactly equivalent to\&\f(CW\*(C`defined($a) ? $a : $b\*(C'\fR. This is very useful for providing default valuesfor variables. If you actually want to test if at least one of \f(CW$a\fR and\&\f(CW$b\fR is defined, use \f(CW\*(C`defined($a // $b)\*(C'\fR..PPThe \f(CW\*(C`||\*(C'\fR, \f(CW\*(C`//\*(C'\fR and \f(CW\*(C`&&\*(C'\fR operators return the last value evaluated(unlike C's \f(CW\*(C`||\*(C'\fR and \f(CW\*(C`&&\*(C'\fR, which return 0 or 1). Thus, a reasonablyportable way to find out the home directory might be:.PP.Vb 2\& $home = $ENV{\*(AqHOME\*(Aq} // $ENV{\*(AqLOGDIR\*(Aq} //\& (getpwuid($<))[7] // die "You\*(Aqre homeless!\en";.Ve.PPIn particular, this means that you shouldn't use thisfor selecting between two aggregates for assignment:.PP.Vb 3\& @a = @b || @c; # this is wrong\& @a = scalar(@b) || @c; # really meant this\& @a = @b ? @b : @c; # this works fine, though.Ve.PPAs more readable alternatives to \f(CW\*(C`&&\*(C'\fR and \f(CW\*(C`||\*(C'\fR when used forcontrol flow, Perl provides the \f(CW\*(C`and\*(C'\fR and \f(CW\*(C`or\*(C'\fR operators (see below).The short-circuit behavior is identical. The precedence of \*(L"and\*(R"and \*(L"or\*(R" is much lower, however, so that you can safely use them after alist operator without the need for parentheses:.PP.Vb 2\& unlink "alpha", "beta", "gamma"\& or gripe(), next LINE;.Ve.PPWith the C\-style operators that would have been written like this:.PP.Vb 2\& unlink("alpha", "beta", "gamma")\& || (gripe(), next LINE);.Ve.PPUsing \*(L"or\*(R" for assignment is unlikely to do what you want; see below..Sh "Range Operators".IX Xref "operator, range range .. ...".IX Subsection "Range Operators"Binary \*(L"..\*(R" is the range operator, which is really two differentoperators depending on the context. In list context, it returns alist of values counting (up by ones) from the left value to the rightvalue. If the left value is greater than the right value then itreturns the empty list. The range operator is useful for writing\&\f(CW\*(C`foreach (1..10)\*(C'\fR loops and for doing slice operations on arrays. Inthe current implementation, no temporary array is created when therange operator is used as the expression in \f(CW\*(C`foreach\*(C'\fR loops, but olderversions of Perl might burn a lot of memory when you write somethinglike this:.PP.Vb 3\& for (1 .. 1_000_000) {\& # code\& }.Ve.PPThe range operator also works on strings, using the magical auto-increment,see below..PPIn scalar context, \*(L"..\*(R" returns a boolean value. The operator isbistable, like a flip-flop, and emulates the line-range (comma) operatorof \fBsed\fR, \fBawk\fR, and various editors. Each \*(L"..\*(R" operator maintains itsown boolean state. It is false as long as its left operand is false.Once the left operand is true, the range operator stays true until theright operand is true, \fI\s-1AFTER\s0\fR which the range operator becomes falseagain. It doesn't become false till the next time the range operator isevaluated. It can test the right operand and become false on the sameevaluation it became true (as in \fBawk\fR), but it still returns true once.If you don't want it to test the right operand till the nextevaluation, as in \fBsed\fR, just use three dots (\*(L"...\*(R") instead oftwo. In all other regards, \*(L"...\*(R" behaves just like \*(L"..\*(R" does..PPThe right operand is not evaluated while the operator is in the\&\*(L"false\*(R" state, and the left operand is not evaluated while theoperator is in the \*(L"true\*(R" state. The precedence is a little lowerthan || and &&. The value returned is either the empty string forfalse, or a sequence number (beginning with 1) for true. Thesequence number is reset for each range encountered. The finalsequence number in a range has the string \*(L"E0\*(R" appended to it, whichdoesn't affect its numeric value, but gives you something to searchfor if you want to exclude the endpoint. You can exclude thebeginning point by waiting for the sequence number to be greaterthan 1..PPIf either operand of scalar \*(L"..\*(R" is a constant expression,that operand is considered true if it is equal (\f(CW\*(C`==\*(C'\fR) to the currentinput line number (the \f(CW$.\fR variable)..PPTo be pedantic, the comparison is actually \f(CW\*(C`int(EXPR) == int(EXPR)\*(C'\fR,but that is only an issue if you use a floating point expression; whenimplicitly using \f(CW$.\fR as described in the previous paragraph, thecomparison is \f(CW\*(C`int(EXPR) == int($.)\*(C'\fR which is only an issue when \f(CW$.\fRis set to a floating point value and you are not reading from a file.Furthermore, \f(CW"span" .. "spat"\fR or \f(CW\*(C`2.18 .. 3.14\*(C'\fR will not do whatyou want in scalar context because each of the operands are evaluatedusing their integer representation..PPExamples:.PPAs a scalar operator:.PP.Vb 2\& if (101 .. 200) { print; } # print 2nd hundred lines, short for\& # if ($. == 101 .. $. == 200) ...\&\& next LINE if (1 .. /^$/); # skip header lines, short for\& # ... if ($. == 1 .. /^$/);\& # (typically in a loop labeled LINE)\&\& s/^/> / if (/^$/ .. eof()); # quote body\&\& # parse mail messages\& while (<>) {\& $in_header = 1 .. /^$/;\& $in_body = /^$/ .. eof;\& if ($in_header) {\& # ...\& } else { # in body\& # ...\& }\& } continue {\& close ARGV if eof; # reset $. each file\& }.Ve.PPHere's a simple example to illustrate the difference betweenthe two range operators:.PP.Vb 4\& @lines = (" \- Foo",\& "01 \- Bar",\& "1 \- Baz",\& " \- Quux");\&\& foreach (@lines) {\& if (/0/ .. /1/) {\& print "$_\en";\& }\& }.Ve.PPThis program will print only the line containing \*(L"Bar\*(R". Ifthe range operator is changed to \f(CW\*(C`...\*(C'\fR, it will also print the\&\*(L"Baz\*(R" line..PPAnd now some examples as a list operator:.PP.Vb 3\& for (101 .. 200) { print; } # print $_ 100 times\& @foo = @foo[0 .. $#foo]; # an expensive no\-op\& @foo = @foo[$#foo\-4 .. $#foo]; # slice last 5 items.Ve.PPThe range operator (in list context) makes use of the magicalauto-increment algorithm if the operands are strings. Youcan say.PP.Vb 1\& @alphabet = (\*(AqA\*(Aq .. \*(AqZ\*(Aq);.Ve.PPto get all normal letters of the English alphabet, or.PP.Vb 1\& $hexdigit = (0 .. 9, \*(Aqa\*(Aq .. \*(Aqf\*(Aq)[$num & 15];.Ve.PPto get a hexadecimal digit, or.PP.Vb 1\& @z2 = (\*(Aq01\*(Aq .. \*(Aq31\*(Aq); print $z2[$mday];.Ve.PPto get dates with leading zeros..PPIf the final value specified is not in the sequence that the magicalincrement would produce, the sequence goes until the next value wouldbe longer than the final value specified..PPIf the initial value specified isn't part of a magical incrementsequence (that is, a non-empty string matching \*(L"/^[a\-zA\-Z]*[0\-9]*\ez/\*(R"),only the initial value will be returned. So the following will onlyreturn an alpha:.PP.Vb 2\& use charnames \*(Aqgreek\*(Aq;\& my @greek_small = ("\eN{alpha}" .. "\eN{omega}");.Ve.PPTo get lower-case greek letters, use this instead:.PP.Vb 1\& my @greek_small = map { chr } ( ord("\eN{alpha}") .. ord("\eN{omega}") );.Ve.PPBecause each operand is evaluated in integer form, \f(CW\*(C`2.18 .. 3.14\*(C'\fR willreturn two elements in list context..PP.Vb 1\& @list = (2.18 .. 3.14); # same as @list = (2 .. 3);.Ve.Sh "Conditional Operator".IX Xref "operator, conditional operator, ternary ternary ?:".IX Subsection "Conditional Operator"Ternary \*(L"?:\*(R" is the conditional operator, just as in C. It works muchlike an if-then-else. If the argument before the ? is true, theargument before the : is returned, otherwise the argument after the :is returned. For example:.PP.Vb 2\& printf "I have %d dog%s.\en", $n,\& ($n == 1) ? \*(Aq\*(Aq : "s";.Ve.PPScalar or list context propagates downward into the 2ndor 3rd argument, whichever is selected..PP.Vb 3\& $a = $ok ? $b : $c; # get a scalar\& @a = $ok ? @b : @c; # get an array\& $a = $ok ? @b : @c; # oops, that\*(Aqs just a count!.Ve.PPThe operator may be assigned to if both the 2nd and 3rd arguments arelegal lvalues (meaning that you can assign to them):.PP.Vb 1\& ($a_or_b ? $a : $b) = $c;.Ve.PPBecause this operator produces an assignable result, using assignmentswithout parentheses will get you in trouble. For example, this:.PP.Vb 1\& $a % 2 ? $a += 10 : $a += 2.Ve.PPReally means this:.PP.Vb 1\& (($a % 2) ? ($a += 10) : $a) += 2.Ve.PPRather than this:.PP.Vb 1\& ($a % 2) ? ($a += 10) : ($a += 2).Ve.PPThat should probably be written more simply as:.PP.Vb 1\& $a += ($a % 2) ? 10 : 2;.Ve.Sh "Assignment Operators".IX Xref "assignment operator, assignment = **= += *= &= <<= &&= -= = |= >>= ||= = .= %= ^= x=".IX Subsection "Assignment Operators"\&\*(L"=\*(R" is the ordinary assignment operator..PPAssignment operators work as in C. That is,.PP.Vb 1\& $a += 2;.Ve.PPis equivalent to.PP.Vb 1\& $a = $a + 2;.Ve.PPalthough without duplicating any side effects that dereferencing the lvaluemight trigger, such as from \fItie()\fR. Other assignment operators work similarly.The following are recognized:.PP.Vb 4\& **= += *= &= <<= &&=\& \-= /= |= >>= ||=\& .= %= ^= //=\& x=.Ve.PPAlthough these are grouped by family, they all have the precedenceof assignment..PPUnlike in C, the scalar assignment operator produces a valid lvalue.Modifying an assignment is equivalent to doing the assignment andthen modifying the variable that was assigned to. This is usefulfor modifying a copy of something, like this:.PP.Vb 1\& ($tmp = $global) =~ tr [A\-Z] [a\-z];.Ve.PPLikewise,.PP.Vb 1\& ($a += 2) *= 3;.Ve.PPis equivalent to.PP
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?