greengaroo has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am reaching out to those of you who have experience with Moose and MooseX-Declare.

I have created an Abstract class and it works fine, I just want to know if there is a better way to do it, more specifically, the part where we prevent instantiation of this Abstract class.

So, this is how I do it:

use MooseX::Declare; class AbstractClass { sub BUILD { my $self = shift; # This is the mechanism that prevents instantiation if ( ref($self) eq __PACKAGE__ ) { # throw exception here } } # other methods go here }

Anyone knows a better way to do it? Should I expect problems from doing this?

Thank you!

Take my advice. I don't use it anyway.

Replies are listed 'Best First'.
Re: Abstract class under MooseX-Declare
by tobyink (Canon) on Aug 09, 2012 at 21:27 UTC

    Use MooseX::ABC.

    use MooseX::Declare; class AbstractClass { use MooseX::ABC; }
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thank you very much! I didn't know about MooseX-ABC, I will look into it!

      Take my advice. I don't use it anyway.

        It's pretty obscurely named. Something like MooseX::Abstract would be saner.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Abstract class under MooseX-Declare
by grondilu (Friar) on Aug 08, 2012 at 18:56 UTC

    I'm no expert but since no one already answered this I'll give it a try.

    It seems to me that the problem you might encounter is that your protection will work fine for AbstractClass, but not for any abstract child of AstractClass. If you keep this in mind, it should work.

    About a better way to do it, I don't know but I'm not sure it is a good idea to redefine "BUILD" for this kind of thing. What about just putting your exception in the constructor?

    sub new { die "attempt to instanciate an abstract class" }

      First, thank you very much for your input, I really appreciate it.

      Let me answer your second statement first, about putting the exception in the constructor instead of the BUILD method. According to Moose best practices: Overriding new is a very bad practice. Instead, you should use a BUILD or BUILDARGS methods to do the same thing. When you override new, Moose can no longer inline a constructor when your class is immutabilized. So this is basically why I don't use it in the constructor.

      Now, why I use the BUILD method is because in Moose, the BUILD methods are called in order from the parent to the child, and not the other way around like normal methods. So instantiating the Abstract Class itself would trigger the call of its own BUILD method first, throwing an exception right away without executing more code.

      So, for an Abstract Class child of the first one, I would probably have to put an exact copy of the BUILD method I used in my example to prevent this child class from being instantiated too. Its BUILD method would be called in second place, just after its parent's.

      -- Take my advice. I don't use it anyway.