使用 Dumper 不会触发故障

发布于 2024-10-04 11:26:00 字数 241 浏览 1 评论 0原文

运行这样的代码时:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦忆晨望 2024-10-11 11:26:00

如果您从标准样板开始,那么您就会知道:

#!/usr/bin/env perl
#
# name_of_program - what the program does as brief one-liner
#
# Your Name <your_email@your_host.TLA>
# Date program written/released
#################################################################

use 5.10.0;

use utf8;
use strict;
use autodie;
use warnings FATAL => "all";

#  ⚠ change to agree with your input: ↓
use open ":std" => IN    => ":encoding(ISO-8859-1)",
                   OUT   => ":utf8";
#  ⚠ change for your output: ↑ — *maybe*, but leaving as UTF-8 is sometimes better

END {close STDOUT}

our $VERSION = 1.0;

$| = 1;

答案是您的程序在语法上是正确的,但在语义上不正确。您正在将 "something" 打印到未打开的 Dumper 文件句柄对象,因为 Dumper 位于 print方法调用。这使得 Dumper print 成为调用者。但是您从未打开过该名称的句柄,因此您正在打印到未初始化的文件句柄。

使用我的样板。 请!

If you had begun with the standard boilerplate, then you would know:

#!/usr/bin/env perl
#
# name_of_program - what the program does as brief one-liner
#
# Your Name <your_email@your_host.TLA>
# Date program written/released
#################################################################

use 5.10.0;

use utf8;
use strict;
use autodie;
use warnings FATAL => "all";

#  ⚠ change to agree with your input: ↓
use open ":std" => IN    => ":encoding(ISO-8859-1)",
                   OUT   => ":utf8";
#  ⚠ change for your output: ↑ — *maybe*, but leaving as UTF-8 is sometimes better

END {close STDOUT}

our $VERSION = 1.0;

$| = 1;

The answer is that your program is syntactically but not semantically correct. You are printing "something" to the unopened Dumper filehandle-object, because Dumper is in the dative slot for the print method call. That makes Dumper print’s invocant. But you never opened a handle by that name, so you are printing to an uninitialized filehandle.

Use my boilerplate. PLEASE!

[旋木] 2024-10-11 11:26:00

print 的有效语法之一是

print FILEHANDLE LIST

在您的程序中 Perl 将 Dumper 视为文件句柄 glob。

在启用警告的情况下运行此代码将告诉您:

print() on unopened filehandle Dumper at ...

One of the valid syntaxes for print is

print FILEHANDLE LIST

In 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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文