C++-like usage of Moose with Perl for OOP

﹥>﹥吖頭↗ 提交于 2019-12-03 07:41:06
draegtun

I can think of this way using roles instead of subclassing:

{
    package AbstractRole;
    use Moose::Role;
    requires 'stuff';  
}

{
    package Real;
    use Moose;
    with 'AbstractRole';
}

This will give a compilation error because Real doesn't have stuff defined.

Adding stuff method to Real will now make it work:

{
    package Real;
    use Moose;
    with 'AbstractRole';

    sub stuff { print "Using child function!\n" }
}

You might also want to take a look at Jesse Luehrs' MooseX::ABC. It seems very similar to some of the implementations here. From the synopsis:

package Shape;
use Moose;
use MooseX::ABC;

requires 'draw';

package Circle;
use Moose;
extends 'Shape';

sub draw {
    # stuff
}

my $shape = Shape->new; # dies
my $circle = Circle->new; # succeeds

package Square;
use Moose;
extends 'Shape'; # dies, since draw is unimplemented

I know that Jesse is a C++ programmer during the day.

It appears I can't do exactly what I want with Moose, but I can come very close with Roles. Here is the information from the Moose manual entry for Roles:

Roles Versus Abstract Base Classes

If you are familiar with the concept of abstract base classes in other languages, you may be tempted to use roles in the same way.

You can define an "interface-only" role, one that contains just a list of required methods.

However, any class which consumes this role must implement all of the required methods, either directly or through inheritance from a parent. You cannot delay the method requirement check so that they can be implemented by future subclasses.

Because the role defines the required methods directly, adding a base class to the mix would not achieve anything. We recommend that you simply consume the interface role in each class which implements that interface.

Here is was my attempt (without Roles, for information on Roles see the other answers):

package Abstract;
use Moose;

sub stuff;

package Real;
use Moose;
extends 'Abstract';

override 'stuff' => sub { print "Using child function!\n"; }
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!