次のサンプル プログラムを見てください。
#! /usr/bin/env perl
#
use 5.10.0;
use strict;
use warnings;
my $new_foo = Foo->new();
my $new_foo_bar = Foo::Bar->new();
say '$new_foo is an object type ' . ref $new_foo;
say '$new_foo_bar is an object type ' . ref $new_foo_bar;
package Foo;
sub new {
my $class = shift;
my $self = {};
bless $self, $class;
return $self;
}
package Foo::Bar;
use base qw(Foo);
これは以下を返します:
$new_foo is an object type Foo
$new_foo_bar is an object type Foo::Bar
$new_foo_bar
のオブジェクトタイプでもあるかどうかをテストする方法はありFoo
ますか? 私はそれを知っています。命名規則を利用して、`/^Foo(::)/ に一致する参照型を持つものはすべてそのオブジェクト型であると単純に仮定することができますが、それはその規則に従った場合にのみ当てはまります。オブジェクトがそのオブジェクト タイプであるかどうかを判断する公式の Perl の方法はありますか?
このようなもの:
if ( is_a_memeber_of( $my_object_ref, "Foo" ) {
say qq(\$my_object_ref is a member of "Foo" even though it might also be a sub-class of "Foo");
}
else {
say qq(\$my_object_ref isn't a member of "Foo");
}