简体   繁体   English

这个带有一元加号的 perl 片段有什么作用? 加入('/',拼接@t,0,+@d)

[英]What does this perl snippet with unary plus do? join( '/', splice @t, 0, +@d )

What does this perl snippet with unary plus do?这个带有一元加号的 perl 片段有什么作用?

join( '/', splice @t, 0, +@d )

This is from File::Path.pm line 267, and I'm trying to understand it.这是来自 File::Path.pm 第 267 行,我正在尝试理解它。 According to the documentation, unary plus does nothing.根据文档,unary plus 什么都不做。 Also the 3rd argument to splice is an integer, not an array. splice 的第三个参数也是 integer,而不是数组。 Does this actually do a scalar(@d) ?这实际上是一个scalar(@d)吗? Thanks for your help.谢谢你的帮助。 I'm trying to translate this code with my Pythonizer to python.我试图用我的Pythonizer将这段代码翻译成 python。

I tried using -MO=Deparse and all it does is eat the + and move the parens around:我尝试使用 -MO=Deparse,它所做的就是吃掉+并移动括号:

join '/', splice(@t, 0, @d);

The + is completely useless here. +在这里完全没用。


The author probably thought +@d was a short version of scalar(@d) , but they were mistaken if so.作者可能认为+@dscalar(@d)的简短版本,但如果是这样他们就错了。

Unary- + has no effect on context.一元- +对上下文没有影响。 [1] Unary- + has no effect at all. [1]一元- +根本没有作用。 Quote perlop ,引用perlop ,

Unary "+" has no effect whatsoever, even on strings.一元"+"没有任何效果,即使对字符串也是如此。 It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments. (See examples above under "Terms and List Operators (Leftward)".)它在句法上用于将 function 名称与括号表达式分开,否则该表达式将被解释为 function arguments 的完整列表。(参见上面“术语和列表运算符(左)”下的示例。)

It's can be used to disambiguate between ambiguous syntax, [2] but it doesn't even do that in the above snippet.它可以用来消除不明确的语法之间的歧义, [2]但在上面的代码片段中它甚至没有做到这一点。 It is completely superfluous.这完全是多余的。 [3] [3]

That said, @d is evaluated in scalar context here, but that's because the splice operator imposes scalar context on its third operand.也就是说, @d在这里是在标量上下文中求值的,但那是因为splice运算符将标量上下文强加给它的第三个操作数。 [4] It has nothing to do with the unary- + . [4]它与一元 - +无关。

As such, another possibility is that the author wanted to signal that there's something unusual about @d here.因此,另一种可能性是作者想表明@d这里有一些不寻常的地方。 One would probably expect it to be evaluated in list context, and the + is designed to make the reader stop to think.人们可能希望它在列表上下文中进行评估,而+旨在让读者停下来思考。

However, that's not something I would recommend doing.但是,我不建议这样做。 Instead of using the misleading +@d , I would use 0+@d to achieve this goal.我不会使用误导性的+@d ,而是使用0+@d来实现这个目标。 0+@d actually imposes scalar context, so it signals that @d is being evaluated in scalar context, and it does so without being misleading. 0+@d实际上强加了标量上下文,因此它表示@d正在标量上下文中进行评估,并且这样做不会产生误导。


  1. The following demonstrates that unary- + doesn't affect context:下面演示了 unary- +不影响上下文:

     $ perl -Mv5.10 -e' my @a = qw( ab c ); my $x_without = @a; say $x_without; my $x_with = +@a; say $x_with; my @y_without = @a; say @y_without; my @y_with = +@a; say @y_with; ' 3 3 abc abc
  2. See using unary- + to disambiguate .请参阅使用 unary- +来消除歧义

  3. The following demonstrates the exact same code is produced with and without the unary- + .下面演示了使用和不使用一元+生成的完全相同的代码。

     $ diff -u \ <( perl -MO=Concise,-exec -e'join( '/', splice @t, 0, +@d )' 2>&1 ) \ <( perl -MO=Concise,-exec -e'join( '/', splice @t, 0, @d )' 2>&1 ) \ && echo same same
  4. The following demonstrates that the third operand is evaluated in scalar context:下面演示了第三个操作数是在标量上下文中计算的:

     $ perl -Mv5.10 -e'say prototype( "CORE::splice" ) // "none/special"' \@;$$@

The unary + is sometimes useful to tell the parser how to interpret the following expression.一元+有时可用于告诉解析器如何解释以下表达式。 See perlop :perlop

Unary + has no effect whatsoever, even on strings.一元+没有任何效果,即使对字符串也是如此。 It is useful syntactically for separating a function name from a parenthesized expression that would otherwise be interpreted as the complete list of function arguments.它在句法上很有用,可以将 function 名称与带括号的表达式分开,否则该表达式将被解释为 function arguments 的完整列表。

In this particular case, though, it does nothing, as the @d is parsed as the third argument to splice .但是,在这种特殊情况下,它什么都不做,因为@d被解析为splice的第三个参数。 Maybe the author wanted it to be evaluated in scalar context, but it works that way regardless of the presence of the + .也许作者希望它在标量上下文中进行评估,但无论+是否存在,它都会以这种方式工作。

The prototype of splice is \@;$$@ , so the optional third argument is going to be interpreted in scalar context, with or without a unary plus. splice的原型是\@;$$@ ,所以可选的第三个参数将在标量上下文中解释,有或没有一元加号。

The interesting challenge for porting to Python is handling the side-effects of splice .移植到 Python 的有趣挑战是处理splice的副作用。

The return value of splice (which is what will get passed to the join function) are the elements of @t that are removed by the operation -- namely the first scalar @d elements of @t . splice的返回值(将传递给join函数的值)是操作删除的@t的元素——即@t的第一个scalar @d元素。 But the @t array will be modified by the call, and after this statement it will be missing those first few elements.但是@t数组将被调用修改,并且在这条语句之后它将丢失前几个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM