Scalar::Util 与 ref 函数
内置的 ref($object)
和 Scalar::Util
blessed($object)
有什么区别?其中一个比另一个更受青睐吗?
use strict;
use warnings;
use Scalar::Util qw(blessed isvstring);
my $object = foo->new();
print "Object is a " . blessed($object) . "\n";
print "Object is a " . ref($object) . "\n";
my $version = 5.00.03;
print "Version is a " . ref(\$version) . "\n";
if (isvstring($version)) {
print "Version is a VSTRING\n";
}
package foo;
sub new {
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
What's the difference between the built in ref($object)
and Scalar::Util
blessed($object)
? Is one preferred over the other?
use strict;
use warnings;
use Scalar::Util qw(blessed isvstring);
my $object = foo->new();
print "Object is a " . blessed($object) . "\n";
print "Object is a " . ref($object) . "\n";
my $version = 5.00.03;
print "Version is a " . ref(\$version) . "\n";
if (isvstring($version)) {
print "Version is a VSTRING\n";
}
package foo;
sub new {
my $class = shift;
my $self = {};
bless($self, $class);
return $self;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 POD,
blessed()
仅适用于受祝福的引用(例如传递给bless()
调用的引用)。它对其他所有内容都返回
undef
,包括哈希/数组引用,其中ref()
返回HASH
/ARRAY
(并且perldoc ref 中描述的一堆其他类型。要获取引用类型,您当然可以调用Scalar::Util::reftype
。至于是否应该使用一个而不是另一个,我认为这很大程度上取决于逻辑是什么。
如果您仅想要将真正的受祝福的引用与其他内容区分开来,
blessed()
提供了一种比采用ref
更简洁的方法,然后验证该值不是由 unblessed 引用返回的标准值。如果您需要区分受祝福的引用和不同的受祝福的引用,那么单个
ref()
调用比blessed
和的组合更简洁reftype
。正如 Eric Strom 的评论中所指出的,两种方法之间存在实际功能差异的一个边缘情况是,当有人创建一个与
ref()
之一匹配的类时 硬编码值(例如bless [], 'HASH'
- 在这种情况下它们要么太笨,要么太聪明)。免责声明:根据文档,当参数是祝福到类中的引用时(例如,它返回类名),两者之间应该没有区别。但我还没有检查“Scalar::Util”源来确认。
According to POD,
blessed()
only works on blessed references (e.g. a references passed to abless()
call).It returns
undef
on everything else, including hash/array refs whereref()
returnsHASH
/ARRAY
(and a bunch of other types as delineated in perldoc ref). To get reference type you can, of course callScalar::Util::reftype
.As for whether one should be used over another, I think it depends largely on what the logic is.
If you only want to distinguish real blessed references from everything else,
blessed()
provides a more concise way than taking aref
and then verifying that the value is not on of standard ones returned by unblessed reference.If you need fine distinctions between both blessed references and different ublessed ones, then a single
ref()
call is more concise than a combination ofblessed
andreftype
.One edge case where there's an actual functional difference between the two approaches, as noted in the comments by Eric Strom, is when someone creates a class which matches one of
ref()
hardcoded values (e.g.bless [], 'HASH'
- in which case they are either Way Dumb or Way Too Clever By Half).DISCLAIMER: Based on documentation, there should be no difference between the two when the argument is a reference blessed into a class (e.g. it returns class name). But I haven't checked "Scalar::Util" source to confirm.