使用 Dumper 不会触发故障
运行这样的代码时:
use strict;
print Dumper "something";
不会打印任何内容,并且在编译和运行时不会发生错误。为什么会出现这种情况?为什么 strict
不阻止此代码运行?尽管 Dumper 未知,但为什么运行时没有错误?
我知道当显式启用这些功能时它会产生警告,但我很感兴趣为什么这段代码在任何方面都被认为是“正确的”。
when running code like this:
use strict;
print Dumper "something";
nothing is printed out and no error occurs during compile and runtime. Why does this happen? Why doesn't strict
prevent this code from running? Why is there no error at runtime, even though Dumper is unknown?
I know it produces a warning when those are explicitly enabled, but I'm interested why is this code considered "correct" in any way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您从标准样板开始,那么您就会知道:
答案是您的程序在语法上是正确的,但在语义上不正确。您正在将
"something"
打印到未打开的Dumper
文件句柄对象,因为Dumper
位于print方法调用。这使得
Dumper
print
成为调用者。但是您从未打开过该名称的句柄,因此您正在打印到未初始化的文件句柄。使用我的样板。 请!
If you had begun with the standard boilerplate, then you would know:
The answer is that your program is syntactically but not semantically correct. You are printing
"something"
to the unopenedDumper
filehandle-object, becauseDumper
is in the dative slot for theprint
method call. That makesDumper
print
’s invocant. But you never opened a handle by that name, so you are printing to an uninitialized filehandle.Use my boilerplate. PLEASE!
print
的有效语法之一是在您的程序中 Perl 将
Dumper
视为文件句柄 glob。在启用警告的情况下运行此代码将告诉您:
print() on unopened filehandle Dumper at ...
One of the valid syntaxes for
print
isIn your program Perl is treating
Dumper
as a filehandle glob.Running this code with warnings enabled will tell you:
print() on unopened filehandle Dumper at ...