在 Moose 中,我如何判断一个对象的类是否是另一个对象类的子类?

发布于 2024-09-27 21:33:12 字数 311 浏览 3 评论 0 原文

假设我有两个对象 $obj1$obj2,它们都是 Moose 类的实例。我想找出以下哪一项适用:

  • $obj1 的类与 $obj2 的类相同;
  • $obj1 的类是 $obj2 的子类;
  • $obj1 的类是 $obj2 的超类;
  • 两个对象的类都不是另一个对象的子类。

我该怎么做?

Suppose I have two objects $obj1 and $obj2 that are both instances of Moose classes. I want to find out which of the following applies:

  • $obj1's class is the same as $obj2's;
  • $obj1's class is a subclass of $obj2's;
  • $obj1's class is a superclass of $obj2's;
  • Neither object's class is a subclass of the other's.

How can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白昼 2024-10-04 21:33:13
  1. $obj1 的类与 $obj2 的类相同吗?

    ref $obj1 eq ref $obj2;
    
  2. $obj1 的类是 $obj2 的子类吗?

    $obj1->isa(ref $obj2);
    
  3. $obj1 的类是 $obj2 的超类吗?

    $obj2->isa(ref $obj1);
    
  4. 两个对象的类都不是另一个对象的子类。

    参见上文。

更新:

响应有关运行时应用的角色的评论:

package My::X;

use Moose; use namespace::autoclean;

sub boo { }

__PACKAGE__->meta->make_immutable;

package My::Y;

use Moose; use namespace::autoclean;

extends 'My::X';

__PACKAGE__->meta->make_immutable;

package My::Z;

use Moose::Role; use namespace::autoclean;

requires 'boo';

package main;

use Test::More tests => 2;

use Moose::Util qw( apply_all_roles );

my $x = My::X->new;
my $y = My::Y->new;

ok($y->isa(ref $x), 'Before role was applied at runtime');

apply_all_roles($x, 'My::Z');

ok($y->isa(ref $x), 'After role was applied at runtime');

输出:

1..2
ok 1 - Before role was applied at runtime
not ok 2 - After role was applied at runtime
#   Failed test 'After role was applied at runtime' at C:\Temp\t.pl line 36.
# Looks like you failed 1 test of 2.
  1. Is $obj1's class the same as $obj2's?

    ref $obj1 eq ref $obj2;
    
  2. Is $obj1's class a subclass of $obj2's?

    $obj1->isa(ref $obj2);
    
  3. Is $obj1's class a superclass of $obj2's?

    $obj2->isa(ref $obj1);
    
  4. Neither object's class is a subclass of the other's.

    See above.

Update:

In response to comments regarding roles applied at run time:

package My::X;

use Moose; use namespace::autoclean;

sub boo { }

__PACKAGE__->meta->make_immutable;

package My::Y;

use Moose; use namespace::autoclean;

extends 'My::X';

__PACKAGE__->meta->make_immutable;

package My::Z;

use Moose::Role; use namespace::autoclean;

requires 'boo';

package main;

use Test::More tests => 2;

use Moose::Util qw( apply_all_roles );

my $x = My::X->new;
my $y = My::Y->new;

ok($y->isa(ref $x), 'Before role was applied at runtime');

apply_all_roles($x, 'My::Z');

ok($y->isa(ref $x), 'After role was applied at runtime');

Output:

1..2
ok 1 - Before role was applied at runtime
not ok 2 - After role was applied at runtime
#   Failed test 'After role was applied at runtime' at C:\Temp\t.pl line 36.
# Looks like you failed 1 test of 2.
末骤雨初歇 2024-10-04 21:33:13

使用 Class::MOP 基础 Moose 您可以内省所有这些信息。

例如:

{
    package Daddy;
    use Moose;
}

{
    package Kid;
    use Moose;
    extends 'Daddy';
}

my $son      = Kid->new;
my $daughter = Kid->new;

my $sons_class                  = ($son->meta->class_precedence_list)[0];
my $daughters_class             = ($daughter->meta->class_precedence_list)[0];

my @sons_subclasses             = $son->meta->subclasses;     # or better...
my @daughters_subclasses        = $daughter->meta->direct_subclasses;

my @sons_superclasses           = $son->meta->superclasses;

my @Daddies_children            = Daddy->meta->direct_subclasses;

另请参阅此问题/答案 如何在 Perl 中找到继承自某个包的所有包?

/I3az/

Using the Class::MOP underpinnings in Moose you can introspect all this information.

For eg:

{
    package Daddy;
    use Moose;
}

{
    package Kid;
    use Moose;
    extends 'Daddy';
}

my $son      = Kid->new;
my $daughter = Kid->new;

my $sons_class                  = ($son->meta->class_precedence_list)[0];
my $daughters_class             = ($daughter->meta->class_precedence_list)[0];

my @sons_subclasses             = $son->meta->subclasses;     # or better...
my @daughters_subclasses        = $daughter->meta->direct_subclasses;

my @sons_superclasses           = $son->meta->superclasses;

my @Daddies_children            = Daddy->meta->direct_subclasses;

Also see this SO question/answer How can I find all the packages that inherit from a package in Perl?

/I3az/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文