如何获得Perl的ref()函数以返回REF,IO和LVALUE? [英] How can I get Perl's ref() function to return REF, IO, and LVALUE?

查看:91
本文介绍了如何获得Perl的ref()函数以返回REF,IO和LVALUE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ref()的文档中提到了几种可能的返回值.我了解它们中的大多数,但不理解REFIOLVALUE.如何编写Perl代码以使ref返回这些值?

The documentation for ref() mentions several possible return values. I understand most of them, but not REF, IO, and LVALUE. How would I write Perl code to cause ref to return those values?

在阅读 typeglob和文件句柄上的文档后,我与此代码相距很近的IO:

After reading the documentation on typeglobs and file handles, I came close for IO with this code:

open(INPUT, '<', 'foo.pl');
print ref(*INPUT{IO}), "\n";  # Prints IO::Handle

对于REFLVALUE,我尝试了几种奇怪的构造,但没有成功.

For REF and LVALUE I tried several bizarre constructions, but had no success.

推荐答案

下面是生产其中大多数的快捷方法:

Here's a quick and easy way to produce most of them:

use 5.010;
say 'SCALAR:  ', ref \undef;
say 'ARRAY:   ', ref [1..5];
say 'HASH:    ', ref { key => 'value' };
say 'CODE:    ', ref sub {};
say 'REF:     ', ref \\undef;
say 'GLOB:    ', ref \*_;
say 'LVALUE:  ', ref \substr "abc", 1, 2;
say 'LVALUE:  ', ref \vec 42, 1, 2;
say 'FORMAT:  ', ref *STDOUT{FORMAT}; # needs declaration below
say 'IO:      ', ref *STDIN{IO};   # actually prints IO::Handle
say 'VSTRING: ', ref \v5.10.0;
say 'Regexp:  ', ref qr/./;

format =
.

REF只是对另一个引用的引用. LVALUE是标量的特例,如果对其进行修改,它会具有外部影响.

REF is just a reference to another reference. LVALUE is a special case of a scalar that has an external influence if it is modified.

IO是处理的基本类型,您可以使用

IO is the base type behind the handles, you can make it appear explicitely using Acme::Damn from CPAN. As noted by Michael Carman in the comments, you really shouldn't be unblessing objects — don't use in real code.

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

ref函数的源代码也具有一些代码来显示"BIND"和"UNKNOWN",但是应该没有办法使它们不弄乱内部结构.布莱德(Blead)也有一个有趣的未受祝福的"REGEXP"(不同于上面的"Regexp");如果有人知道如何使ref产生...

The source for the ref function also has bits of code to display "BIND" and "UNKNOWN", but there shouldn't be a way to get those without messing with the internals. Blead also has an interesting unblessed "REGEXP" (different from the "Regexp" above); if someone knows how to make ref yield that...

这篇关于如何获得Perl的ref()函数以返回REF,IO和LVALUE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆