ref($variable) 什么时候返回“IO”?

发布于 2024-09-04 08:36:00 字数 892 浏览 4 评论 0原文

以下是 ref 函数文档的相关摘录:

返回的值取决于引用所引用的事物的类型。内置类型包括:

<前><代码>标量 大批 哈希 代码 参考文献 全局 左值 格式 IO 字符串 正则表达式

基于此,我想象在文件句柄上调用 ref 将返回 'IO'。令人惊讶的是,它没有:

use strict;
use warnings;

open my $fileHandle, '<', 'aValidFile';
close $fileHandle;

print ref $fileHandle;     # prints 'GLOB', not 'IO'

perlref 尝试解释一下原因:

不可能创建一个真正的 对 IO 句柄的引用(文件句柄 或 dirhandle)使用反斜杠 操作员。您最多可以获得的是 对 typeglob 的引用,即 实际上是一个完整的符号表 条目 [...]但是,您仍然可以 使用类型 glob 和 globrefs 就像它们是 IO 句柄一样。


那么在什么情况下ref会返回'IO'呢?

Here's the relevant excerpt from the documentation of the ref function:

The value returned depends on the type of thing the reference is a reference to. Builtin types include:

SCALAR
ARRAY
HASH
CODE
REF
GLOB
LVALUE
FORMAT
IO
VSTRING
Regexp

Based on this, I imagined that calling ref on a filehandle would return 'IO'. Surprisingly, it doesn't:

use strict;
use warnings;

open my $fileHandle, '<', 'aValidFile';
close $fileHandle;

print ref $fileHandle;     # prints 'GLOB', not 'IO'

perlref tries to explain why:

It isn't possible to create a true
reference to an IO handle (filehandle
or dirhandle) using the backslash
operator. The most you can get is a
reference to a typeglob, which is
actually a complete symbol table
entry [...] However, you can still
use type globs and globrefs
as though they were IO handles.


In what circumstances would ref return 'IO' then?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

世俗缘 2024-09-11 08:36:00

获取 IO 引用的唯一方法是使用 *FOO{THING} 语法:

$ioref = *glob{IO};

其中 glob 是像 STDIN 这样的命名 glob 或像 $fh 这样的引用。但一旦你有了
这样一个引用,它可以像任何其他标量一样传递或存储在任意数据结构中,因此像编组模块这样的事情需要精通它。

由于 glob 或 globref 可以用作文件句柄并隐式获取包含的 IO 事物,因此不需要太多 IO 引用。主要的例外是:

use Symbol;
# replace *FOO{IO} handle but not $FOO, %FOO, etc.
*FOO = geniosym;

(geniosym 返回一个新的半匿名 IO 引用,任何非 glob 引用到 glob 的分配仅分配给该特定引用的 glob 部分)

The only way to get an IO reference is to use the *FOO{THING} syntax:

$ioref = *glob{IO};

where glob is a named glob like STDIN or a reference like $fh. But once you have
such a reference, it can be passed around or stored in arbitrary data structures just like any other scalar, so things like marshalling modules need to be savvy of it.

Since a glob or globref can be used as a filehandle and implicitly get the contained IO thingy, there isn't a lot of need for IO refs. The main exception is this:

use Symbol;
# replace *FOO{IO} handle but not $FOO, %FOO, etc.
*FOO = geniosym;

(geniosym returns a new semi-anonymous IO reference, and any non-glob reference-to-glob assignment only assigns to that particular reference's part of the glob)

锦爱 2024-09-11 08:36:00

正如 Konerak 所说,答案就在这个问题中:

如何让 Perl 的 ref() 函数返回 REF、IO 和 LVALUE?

相关片段是:

use Acme::Damn;
say 'IO:      ', ref damn *STDIN{IO}; # really prints IO

这不应该真正在生产代码中使用,因为该死的从引用中删除了祝福...

As Konerak says, the answer is in this question:

How can I get Perl's ref() function to return REF, IO, and LVALUE?

The relevant snippet is:

use Acme::Damn;
say 'IO:      ', ref damn *STDIN{IO}; # really prints IO

and this should not really be used in production code since damn removes the bless from the reference...

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