質問
の違いについて教えてください内蔵 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;
}
解決
によるポッド、 blessed()
のみ動作に恵まれた参考文献(例えばa参照で渡される bless()
ます。
を返します undef
にも、ハッシュ/配列refsが ref()
を返します HASH
/ARRAY
(々ソフトバンクグループはその他の種類としての縁を perldoc ref).への参照型までの電話 Scalar::Util::reftype
.
しかるべき他のかによって大きく異なってその論理です。
の場合 のみ したいとの区別実に恵まれた参考文献からであ
blessed()
に関するより簡潔な道をref
そのことを検証することに価値がないの標準的な返されるunblessedます。my $ref_type = ref($my_ref); print "USING REF: "; if ( $ref_type && $ref_type ne ref({}) && $ref_type ne ref([]) && $ref_type ne "SCALAR" # Could also use a hash with all allowed values of ref() instead && $ref_type !~ /^(CODE|REF|GLOB|...)$) { print "I am an object of class $ref_type\n"; } else { print "I'm a reference of type $ref_type\n"; } # vs... print "USING SCALAR_UTIL: "; my $ref_type = blessed($my_ref); print $ref_type ? "I am an object of class $ref_type\n" : "I am a reference of type " . reftype($my_ref) . "\n";
が必要な場合は罰との差別化の両方に恵まれた参考文献と異なるublessed、その後シングル
ref()
通話より簡潔以上の組み合わせblessed
やreftype
.の一端がある場合、実際に機能的な違いは二つのアプローチとしてのコメントエリック-マーティノー(Eric Strom、誰かが作るクラスで一致す
ref()
ハードコード値 (例:bless [], 'HASH'
る場合にはそのどちらダムにとことんこだわり、これまでにも味によるお問い合わせくださいmy $sssft = bless [], 'HASH'; # sssft = someone_should_suffer_for_this ref_description_using_ref($sssft); ref_description_using_scalar_util($sssft); # OUTPUT: USING REF: I'm a reference of type HASH USING SCALAR_UTIL: I am an object of class HASH
免責事項:書類がないと認められることの二つの違いが引数で参照に恵まれた授業などを返すクラス名)となります。がんチェック"のスカラー::Util"源を確認することをお勧めします。
所属していません StackOverflow