"print" 语句开头的点?
Dot at the beginning of a "print" statement?
我在使用 Perl 脚本时遇到了一些奇怪的事情。这是关于使用一个点给出不同的结果。
perlop
didn't turn anything up, or perhaps I just blew past it. I was looking at Operator Precedence and Associativity
print "I'd expect to see 11x twice, but I only see it once.\n";
print (1 . 1) . "3";
print "\n";
print "" . (1 . 1) . "3\n";
print "Pluses: I expect to see 14 in both cases, and not 113, because plus works on numbers.\n";
print (1 . 1) + "3";
print "\n";
print "" + (1 . 1) + "3\n";
在开头加上引号是获得我想要的东西的可接受的解决方法,但是这里发生了什么我缺少的操作顺序?有什么规则要学?
当您将 print
的第一个参数放在括号中时,Perl 将其视为函数调用语法。
所以这个:
print (1 . 1) . "3";
解析为:
print(1 . 1) . "3";
或者,等价地:
(print 1 . 1) . "3";
因此,Perl 打印“11”,然后获取 print
调用的 return 值(如果成功则为 1
),将 3
连接到它,并且 - 因为整个表达式在 void 上下文中 - 对结果 13
.
完全没有任何作用
如果您 运行 您的代码启用了警告(通过命令行上的 -w
或 use warnings;
pragma),您将收到这些警告以识别您的错误:
$ perl -w foo.pl
print (...) interpreted as function at foo.pl line 2.
print (...) interpreted as function at foo.pl line 6.
Useless use of concatenation (.) or string in void context at foo.pl line 2.
Useless use of addition (+) in void context at foo.pl line 6.
正如 Borodin 在下面的评论中指出的那样,您不应该依赖 -w
(或代码中的等价物 $^W
);生产代码应始终使用 warnings
编译指示,最好使用 use warnings qw(all);
。虽然在这个特定情况下这无关紧要,但您也应该 use strict;
,尽管通过 use
version;
请求 Perl 版本的现代功能5.11 或更高版本也会自动打开 strict
。
如果命名运算符(或子调用)后跟括号,则这些括号分隔操作数(或参数)。
print (1 . 1) . "3"; ≡ ( print(1 . 1) ) . "3";
print "" . (1 . 1) . "3"; ≡ print("" . (1 . 1) . "3");
请注意,如果您一直在使用(use strict;
和)use warnings qw( all );
,Perl 会提醒您您的问题。
print (...) interpreted as function at a.pl line 2.
print (...) interpreted as function at a.pl line 6.
Useless use of concatenation (.) or string in void context at a.pl line 2.
Useless use of addition (+) in void context at a.pl line 6.
I'd expect to see 11x twice, but I only see it once.
11
113
Pluses: I expect to see 14 in both cases, and not 113, because plus works on numbers.
11
Argument "" isn't numeric in addition (+) at a.pl line 8.
14
我在使用 Perl 脚本时遇到了一些奇怪的事情。这是关于使用一个点给出不同的结果。
perlop
didn't turn anything up, or perhaps I just blew past it. I was looking at Operator Precedence and Associativity
print "I'd expect to see 11x twice, but I only see it once.\n";
print (1 . 1) . "3";
print "\n";
print "" . (1 . 1) . "3\n";
print "Pluses: I expect to see 14 in both cases, and not 113, because plus works on numbers.\n";
print (1 . 1) + "3";
print "\n";
print "" + (1 . 1) + "3\n";
在开头加上引号是获得我想要的东西的可接受的解决方法,但是这里发生了什么我缺少的操作顺序?有什么规则要学?
当您将 print
的第一个参数放在括号中时,Perl 将其视为函数调用语法。
所以这个:
print (1 . 1) . "3";
解析为:
print(1 . 1) . "3";
或者,等价地:
(print 1 . 1) . "3";
因此,Perl 打印“11”,然后获取 print
调用的 return 值(如果成功则为 1
),将 3
连接到它,并且 - 因为整个表达式在 void 上下文中 - 对结果 13
.
如果您 运行 您的代码启用了警告(通过命令行上的 -w
或 use warnings;
pragma),您将收到这些警告以识别您的错误:
$ perl -w foo.pl
print (...) interpreted as function at foo.pl line 2.
print (...) interpreted as function at foo.pl line 6.
Useless use of concatenation (.) or string in void context at foo.pl line 2.
Useless use of addition (+) in void context at foo.pl line 6.
正如 Borodin 在下面的评论中指出的那样,您不应该依赖 -w
(或代码中的等价物 $^W
);生产代码应始终使用 warnings
编译指示,最好使用 use warnings qw(all);
。虽然在这个特定情况下这无关紧要,但您也应该 use strict;
,尽管通过 use
version;
请求 Perl 版本的现代功能5.11 或更高版本也会自动打开 strict
。
如果命名运算符(或子调用)后跟括号,则这些括号分隔操作数(或参数)。
print (1 . 1) . "3"; ≡ ( print(1 . 1) ) . "3";
print "" . (1 . 1) . "3"; ≡ print("" . (1 . 1) . "3");
请注意,如果您一直在使用(use strict;
和)use warnings qw( all );
,Perl 会提醒您您的问题。
print (...) interpreted as function at a.pl line 2.
print (...) interpreted as function at a.pl line 6.
Useless use of concatenation (.) or string in void context at a.pl line 2.
Useless use of addition (+) in void context at a.pl line 6.
I'd expect to see 11x twice, but I only see it once.
11
113
Pluses: I expect to see 14 in both cases, and not 113, because plus works on numbers.
11
Argument "" isn't numeric in addition (+) at a.pl line 8.
14