When does ref($variable) return 'IO'?

断了今生、忘了曾经 提交于 2019-12-01 19:04:06

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)

DVK

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!