Perl 5中存在什么伪运算符?

前端之家收集整理的这篇文章主要介绍了Perl 5中存在什么伪运算符?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我目前正在记录所有Perl 5的操作符(参见 perlopref GitHub项目),我决定也包括Perl 5的伪运算符。对我来说,Perl中的伪运算符是任何看起来像运算符的东西,但实际上是多个运算符或一些其他语法。我已经记录了我熟悉的四个:

>()= countof运算符
> =()= goatse / countof运算符
> ~~标量上下文运算符
>} {爱斯基摩吻操作符

这些伪运算符还有什么其他名字,你知道我错过了什么伪运算符吗?

  1. =head1 Pseudo-operators
  2.  
  3. There are idioms in Perl 5 that appear to be operators,but are really a
  4. combination of several operators or pieces of Syntax. These pseudo-operators
  5. have the precedence of the constituent parts.
  6.  
  7. =head2 ()= X
  8.  
  9. =head3 Description
  10.  
  11. This pseudo-operator is the list assignment operator (aka the countof
  12. operator). It is made up of two items C<()>,and C<=>. In scalar context
  13. it returns the number of items in the list X. In list context it returns an
  14. empty list. It is useful when you have something that returns a list and
  15. you want to know the number of items in that list and don't care about the
  16. list's contents. It is needed because the comma operator returns the last
  17. item in the sequence rather than the number of items in the sequence when it
  18. is placed in scalar context.
  19.  
  20. It works because the assignment operator returns the number of items
  21. available to be assigned when its left hand side has list context. In the
  22. following example there are five values in the list being assigned to the
  23. list C<($x,$y,$z)>,so C<$count> is assigned C<5>.
  24.  
  25. my $count = my ($x,$z) = qw/a b c d e/;
  26.  
  27. The empty list (the C<()> part of the pseudo-operator) triggers this
  28. behavior.
  29.  
  30. =head3 Example
  31.  
  32. sub f { return qw/a b c d e/ }
  33.  
  34. my $count = ()= f(); #$count is now 5
  35.  
  36. my $string = "cat cat dog cat";
  37.  
  38. my $cats = ()= $string =~ /cat/g; #$cats is now 3
  39.  
  40. print scalar( ()= f() ),"\n"; #prints "5\n"
  41.  
  42. =head3 See also
  43.  
  44. L</X = Y> and L</X =()= Y>
  45.  
  46. =head2 X =()= Y
  47.  
  48. This pseudo-operator is often called the goatse operator for reasons better
  49. left unexamined; it is also called the list assignment or countof operator.
  50. It is made up of three items C<=>,C<()>,and C<=>. When X is a scalar
  51. variable,the number of items in the list Y is returned. If X is an array
  52. or a hash it it returns an empty list. It is useful when you have something
  53. that returns a list and you want to know the number of items in that list
  54. and don't care about the list's contents. It is needed because the comma
  55. operator returns the last item in the sequence rather than the number of
  56. items in the sequence when it is placed in scalar context.
  57.  
  58. It works because the assignment operator returns the number of items
  59. available to be assigned when its left hand side has list context. In the
  60. following example there are five values in the list being assigned to the
  61. list C<($x,$z) = qw/a b c d e/;
  62.  
  63. The empty list (the C<()> part of the pseudo-operator) triggers this
  64. behavior.
  65.  
  66. =head3 Example
  67.  
  68. sub f { return qw/a b c d e/ }
  69.  
  70. my $count =()= f(); #$count is now 5
  71.  
  72. my $string = "cat cat dog cat";
  73.  
  74. my $cats =()= $string =~ /cat/g; #$cats is now 3
  75.  
  76. =head3 See also
  77.  
  78. L</=> and L</()=>
  79.  
  80. =head2 ~~X
  81.  
  82. =head3 Description
  83.  
  84. This pseudo-operator is named the scalar context operator. It is made up of
  85. two bitwise negation operators. It provides scalar context to the
  86. expression X. It works because the first bitwise negation operator provides
  87. scalar context to X and performs a bitwise negation of the result; since the
  88. result of two bitwise negations is the original item,the value of the
  89. original expression is preserved.
  90.  
  91. With the addition of the Smart match operator,this pseudo-operator is even
  92. more confusing. The C<scalar> function is much easier to understand and you
  93. are encouraged to use it instead.
  94.  
  95. =head3 Example
  96.  
  97. my @a = qw/a b c d/;
  98.  
  99. print ~~@a,"\n"; #prints 4
  100.  
  101. =head3 See also
  102.  
  103. L</~X>,L</X ~~ Y>,and L<perlfunc/scalar>
  104.  
  105. =head2 X }{ Y
  106.  
  107. =head3 Description
  108.  
  109. This pseudo-operator is called the Eskimo-kiss operator because it looks
  110. like two faces touching noses. It is made up of an closing brace and an
  111. opening brace. It is used when using C<perl> as a command-line program with
  112. the C<-n> or C<-p> options. It has the effect of running X inside of the
  113. loop created by C<-n> or C<-p> and running Y at the end of the program. It
  114. works because the closing brace closes the loop created by C<-n> or C<-p>
  115. and the opening brace creates a new bare block that is closed by the loop's
  116. original ending. You can see this behavior by using the L<B::Deparse>
  117. module. Here is the command C<perl -ne 'print $_;'> deparsed:
  118.  
  119. LINE: while (defined($_ = <ARGV>)) {
  120. print $_;
  121. }
  122.  
  123. Notice how the original code was wrapped with the C<while> loop. Here is
  124. the deparsing of C<perl -ne '$count++ if /foo/; }{ print "$count\n"'>:
  125.  
  126. LINE: while (defined($_ = <ARGV>)) {
  127. ++$count if /foo/;
  128. }
  129. {
  130. print "$count\n";
  131. }
  132.  
  133. Notice how the C<while> loop is closed by the closing brace we added and the
  134. opening brace starts a new bare block that is closed by the closing brace
  135. that was originally intended to close the C<while> loop.
  136.  
  137. =head3 Example
  138.  
  139. # count unique lines in the file FOO
  140. perl -nle '$seen{$_}++ }{ print "$_ => $seen{$_}" for keys %seen' FOO
  141.  
  142. # sum all of the lines until the user types control-d
  143. perl -nle '$sum += $_ }{ print $sum'
  144.  
  145. =head3 See also
  146.  
  147. L<perlrun> and L<perlsyn>
  148.  
  149. =cut

解决方法

尼斯项目,这里有几个:
  1. scalar x!! $value # conditional scalar include operator
  2. (list) x!! $value # conditional list include operator
  3. 'string' x/pattern/ # conditional include if pattern
  4. "@{[ list ]}" # interpolate list expression operator
  5. "${\scalar}" # interpolate scalar expression operator
  6. !! $scalar # scalar -> boolean operator
  7. +0 # cast to numeric operator
  8. .'' # cast to string operator
  9.  
  10. { ($value or next)->depends_on_value() } # early bail out operator
  11. # aka using next/last/redo with bare blocks to avoid duplicate variable lookups
  12. # might be a stretch to call this an operator though...
  13.  
  14. sub{\@_}->( list ) # list capture "operator",like [ list ] but with aliases

猜你在找的Perl相关文章