关于优先级的问题+重复修饰符
请您向我解释一下这种明显不一致的行为:
use strict;
my @a;
print "a" x 2; # this prints: aa
@a = "a" x 2; print @a; # this prints: aa
print ("a") x 2; # this prints: a
@a = ("a") x 2; print @a; # this prints: aa
最后一个不应该打印一个“a”吗?
编辑:好的,现在这对我来说更有意义: “二进制“x”是重复运算符...在列表上下文中,如果左操作数括在括号中或者是由 qw/STRING/ 形成的列表,则会重复该列表。” perlop
这对我来说就像泥一样清晰(二进制 x - 为什么使用这个词是否存在二进制 X?) 但无论如何: @a = ("a") x 2 # 似乎在列表上下文中,因为我们在开头有一个数组 - 数组不是列表,但它包含一个列表,所以我认为我们可能有一个列表上下文,(不是数组上下文,尽管它们可能是同义词)。
我想“左操作数”是(“a”)。 (要么是那个,要么是@a)。 perlop 没有说明操作数实际上是什么,查询 perldoc.perl.org 给出“未找到匹配项”,而谷歌搜索给出“在计算机编程中,操作数是一个用于描述任何能够被操作的对象的术语。”例如像一个数组。
因此,左操作数可能会括在括号中,因此它可能应该“重复列表”。该列表是:("a") x 2
或者是: ("a")
如果我们重复 ("a") x 2
我们会得到 ("a") x 2 ("a" ) x 2
。这似乎不对。
如果我们输入: print $a[1]
我们将得到一个单一的 'a',所以“它重复列表”意味着 Perl 变成 ("a") x 2
进入 ("a", "a")
所以我们有效地得到 @a=("a", "a")
但是, print ("a" ) x 2
不会变成 ("a", "a")
。这是因为 print 是一个具有高优先级的“列表运算符”。因此,我们有效地得到: (print ("a")) x 2
数组是一个术语,因此它也具有很高的优先级,但是 @a=stuff 涉及赋值运算符 =,它具有相对较高的优先级。低优先级。所以它和印刷品有很大不同。
Please could you explain this apparently inconsistent behaviour to me:
use strict;
my @a;
print "a" x 2; # this prints: aa
@a = "a" x 2; print @a; # this prints: aa
print ("a") x 2; # this prints: a
@a = ("a") x 2; print @a; # this prints: aa
Shouldn't the last one print a single 'a'?
Edit: Ok so now this is kind of making more sense to me:
"Binary "x" is the repetition operator...In list context, if the left operand is enclosed in parentheses or is a list formed by qw/STRING/, it repeats the list." perlop
This is as clear as mud to me (binary x - why use the word binary? Is there a denary X?)
But anyway:
@a = ("a") x 2 # seems to be in list context, in that we have an array at the beginning - an array is not a list, but it contains a list, so I think we probably have a list context, (not an array context although they might be synonymous).
i suppose the "left operand" is ("a"). (It's either that or @a). perlop doesn't say what an operand actually is, querying perldoc.perl.org gives "No matches found", and googling gives "In computer programming, an operand is a term used to describe any object that is capable of being manipulated." Like an array for instance.
So the left operand might be enclosed in brackets so maybe it should "repeat the list". The list is either: ("a") x 2
or it is: ("a")
If we repeated ("a") x 2
we would get ("a") x 2 ("a") x 2
. This doesn't seem right.
If we type: print $a[1]
we will get a single 'a', so "it repeats the list" means Perl turns ("a") x 2
into ("a", "a")
so we effectively get @a=("a", "a")
However, print ("a") x 2
does not get turned into ("a", "a")
. That's because print is a "list operator" with a high precedence. So there we effectively get: (print ("a")) x 2
An array is a term so it also has a high precedence, but @a=stuff involves the assignation operator = which has a relatively low precedence. So it's quite different from print.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在你的脚本中添加
use warnings;
,然后你会得到警告,例如Do it like
print (("a") x 2);
#这打印:aa
正如您在评论中提到的,如何格式化代码,我会说参见
Perltidy 是一个 Perl 脚本,它可以缩进并重新格式化 Perl 脚本以使其更易于阅读。如果您编写 Perl 脚本,或者花大量时间阅读它们,您可能会发现它很有用。
有关 Perltidy 的更多信息:
您可以下载 perltidy 并运行它
使用默认值,或自定义它以使用您喜欢的支撑样式、缩进宽度等。
自定义很容易,并且有大量选项。默认值通常是一个很好的起点。
Perltidy 还可用于生成代码的彩色 HTML 输出。
Perltidy 带有一组合理的默认值;但它们可能不适合您。幸运的是,您可以使用 tidyview 作为图形用户界面来预览 perltidy 对代码的更改,并查看哪些选项最适合您。您可以从 CPAN 下载 tidyview。
注意:始终在脚本开头添加
use strict
和use warnings
。add
use warnings;
in your script, then you will get warnings likeDo it like
print (("a") x 2);
# this prints:aa
As you mentioned in comments, how to format your code, i would say see Perltidy.
Perltidy is a Perl script which indents and reformats Perl scripts to make them easier to read. If you write Perl scripts, or spend much time reading them, you will probably find it useful.
Some more information about Perltidy:
You can download perltidy and run it
with its defaults, or customise it to use your preferred bracing style, indentation width etc.
Customisation is easy and there are a huge number of options. The defaults are usually a fine starting place.
Perltidy can also be used to generate colourised HTML output of your code.
Perltidy comes with a sensible set of defaults; but they may not be right for you. Fortunately you can use tidyview as a graphical user interface to preview perltidy’s changes to your code and see which options suit you best. You can download tidyview from CPAN.
Note: Always add
use strict
anduse warnings
at the begining of your scripts.我认为您出现奇怪行为的原因是因为 perl 的 print 命令需要一个列表。引用相关文档:
还要注意 print 关键字后面不要跟有左括号,除非您希望相应的右括号终止打印的参数;在所有参数两边加上括号。
加上括号使其表现得像一个函数。考虑一下是否执行此测试用例:
当编写为函数调用时,对于正在对其参数求值的函数而不是在以下操作后求值的赋值,结果是一致的。
I think the reason that you are getting strange behavior is because the
print
command for perl is to expect a list. Quoting from the relevant documentation:Also be careful not to follow the print keyword with a left parenthesis unless you want the corresponding right parenthesis to terminate the arguments to the print; put parentheses around all the arguments.
Putting parentheses makes it act like a function. Consider if you perform this test case:
When written as a function call, your results are consistent for a function being evaluated to its arguments rather than an assignment being evaluated after the following operations.
您遇到了一个常见的 Perl 解析问题。您的第三条语句
print ("a") x 2
被解析为:您可以添加另一组括号来修复解析:
You are being bitten by a common Perl parsing gotcha. Your third statement
print ("a") x 2
is parsed as:You can add another set of parentheses to fix the parsing: