问题描述
浏览源代码时,我看到以下行:
While browsing the source code I saw the following lines:
The fear is that perhaps you are trying to create a hashreference using the other (expression) formulation of map like so.
@array_of_hashrefs = map { "\L$_" => 1 }, @array注意逗号。那么如果解析器猜测你在OP中给出了这个语句,那么会出现一个缺少逗号的语法错误!要看到区别,请引用$ _。无论什么原因,解析器都足以触发表达式行为。
Notice the comma. Then if the parser guesses that you are doing this given the statement in the OP there will a syntax error for missing the comma! To see the difference try quoting "$_". For whatever reason, the parser takes this as enough to trigger the expression behavior.
是的,它是一个奇怪的。因此,许多超级偏执的Perl程序员比需要多加多余的加号(我包括)。
Yes its an oddity. Therefore many extra-paranoid Perl programmers toss in the extra plus sign more often than needed (me included).
以下是文档。
Here are the examples from the map documentation.
%hash = map { "\L$_" => 1 } @array # perl guesses EXPR. wrong %hash = map { +"\L$_" => 1 } @array # perl guesses BLOCK. right %hash = map { ("\L$_" => 1) } @array # this also works %hash = map { lc($_) => 1 } @array # as does this. %hash = map +( lc($_) => 1 ), @array # this is EXPR and works! %hash = map ( lc($_), 1 ), @array # evaluates to (1, @array)为了有趣的阅读(风格)和解析器发现错误的情况,请阅读以下内容:
For a fun read (stylistically) and a case where the parser gets it wrong read this: http://blogs.perl.org/users/tom_wyant/2012/01/the-case-of-the-overloaded-curlys.html
这篇关于为什么这个地图块含有一个显然没用的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!