And the word for Today is Abstract

Well lets play with some code today.

So I spent a little time having a look at MooseX::AbstractMethod and at first glance it look promising as is suppose to allow on to create an Abstract Class. So having a little time I created a little test code and of course a test file.

The code is simple parent Abstract class that has one sub and one required or abstract sub and then a child class that extends that parent but does not have the required sub so it should fail. This give me a simple four test plan;

  1. use the extended child class
  2. fail to instantiate the child class
  3. use the abstract class
  4. fail to instantiate the abstract class

So in my first run with MooseX::AbstractMethod I get

ok 1 - use DA_AM::Memory;
ok 2 - DA_AM::Memory dies requires _execute sub
ok 3 - use DA_AM;
ok 4 - Directly opened DA

So I can load my child class I guess that is ok. The second fail is what I want to see but to actually get it to test correctly I have to do a little magic behind the curtain like this

 
my $new=DA_AM::Memory->new();
like(
        exception { $new->meta->make_immutable },
        qr/abstract methods have not been implemented/,
        'DA_AM::Memory dies requires _execute sub',
    );
As it will only fail when I use it directly. What this means is anyone using DA_AM will only see an error why they do this
 
my $new=DA_AM::Memory->new();
$new->retrieve();
This could be much later down the line than I like. They would of course see it earlier if they used the best practice of 'meta->make_immutable' but not everyone does.

As well I pass tests 3 and 4 which, if you read what MooseX::AbstractMethod does, is what one would expect as it dose not make the Class Abstract just methods.

Ok not what I was looking for onto the next

I did find MooseX::ABC while trolling CPAN for a solution to my last post. This time the same test plan yielded


not ok 1 - use DA_ABC::Memory
ok 2 - use DA_ABC;
not ok 3 - instantiating abstract class fails

Now I didn't need my test 2 in this plan as it fails the use which is good, I am able to use DA_ABC which is what I want and finally in test 3 I get exactly what I want, I cannot instantiate that abstract class.

That is all great and good but, the first line in its POD is

NOTE: This module is almost certainly a bad idea. You really want to just be using a role instead!

So maybe it is not what I want.

Finally I just did a test suing a simple moose role and got

not ok 1 - use DA::Memory
ok 2 - use DA;
ok 3 - Cannot Instantiate a Role
So in the end the simple Moose role code is the route I should take as it does not require any other than basic moose install and does in effect what I want.

I did have a look at something called Moops which will allow me to more closely mimic the original JAVA code but that is not really what I want to do. So decided I wanted to stay on the Moose path for now.

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations