No Moose Today, Well not much really

So in honour YAPCE 2018 it's shiftin bobbins day here in the Moose-pen.

Carrying on from where I left off in yesterday's post I decided it was time I cleaned up a little more of my code. Seems I do this

use Moose;
with qw(Database::Accessor::Roles::Base
use MooseX::Aliases;
use MooseX::Constructor::AllErrors;

or code similar to it a almost a dozen times in the file. Now there is nothing wrong with this but I think I can clean it up a bit by a just a little re-factoring or shifting things about (hence the link at the top).

One might say why re-factor not that you almost got it all working aren't you taking a change of breaking all stuff. Well yes but, I do have an extensive test suite all written up and if I do it on a test case by test case bases I should be ok.

And here we go!

We know that in Moose a Role lives in its own space and and such you can not have this

package Database::Accessor::Roles::Comparators;
use Moose::Role;
use MooseX::Aliases;

and then this

package Database::Accessor::Function;
use Moose;
with qw(Database::Accessor::Roles::Base

and expect to have the 'MooseX::Aliases' work in the class that consumed the role. So if I want to get rid of all those extra use I will have to go with a Moose 'extends'

To start I created a new base class;

package Database::Accessor::Base;
use Moose;
use MooseX::Constructor::AllErrors;
use MooseX::Aliases;
with qw(Database::Accessor::Types);

has 'name' => (
required => 0,
is => 'rw',
isa => 'Str'

and then a quick change to my Database::Accessor::View

package Database::Accessor::View;
use Moose;
extends 'Database::Accessor::Element';

and a run of my test case 10_view. and opps!

Error: Could not find an attribute by the name of 'name' to inherit from in Database::Accessor::Element at

which is telling me I have to fix my 'Element' class to get Accessor to run. Seem I had taken the 'with ('Database::Accessor::Roles::Base') out of Alias role. So a quick switch like

package Database::Accessor::Element;
use Moose;
extends 'Database::Accessor::Base';
with qw(Database::Accessor::Roles::Alias );

will clean that up. Now run my test case;
ok 1 - use Database::Accessor;
ok 2 - use Database::Accessor::View;
ok 3 - Person is a View
not ok 4 - View does role Database::Accessor::Roles::Base
ok 5 - Has name Accessor

with '4' being an expected fail as my 'view' class no longer does that role,

So after a few rounds of making similar changes, to the one above, to each of my Database::Accessor classes and then running the test case for each, I had all the cases passing until I got to 30_view.t and it failed with;

Found unknown argument 'alias' in the has declaration for 'close_parenthes' in role Database::Accessor::Roles::Comparators

Looks like I was a little to diligent at removing my MooseXes so I added 'MooseX::Aliases' back into the 'Comparators' role and that test case passed.

After I made the the final changes to the last class, Database::Accessor::Sort, I ran into this on when I ran test case '39_sorts.t'

# Failed test 'use Database::Accessor;'
# at 39_sorts.t line 13.
# Tried to use 'Database::Accessor'.
# Error: Recursive inheritance detected in package 'Database::Accessor::Element' at C:/Dwimperl/perl/site/lib/Class/MOP/ line 950.
# Compilation failed in require at 39_sorts.t line 13.

ouch a really nasty one! Fortunately I know all my tests cases where passing before I mucked with the Sort class so I when to that class and after and a few seconds spotted my bug

package Database::Accessor::Sort;
use Moose;
extends Database::Accessor::Element;

seem extends likes be like this

package Database::Accessor::Sort;
use Moose;
extends 'Database::Accessor::Element';

So after that fix the test case ran as did all the others so I ended the night feeling rather good that I got rid of some dozen lines of code.


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