Moose Relations

So after yesterdays re-loadfest of getting all my win- registry in order, downloading the latest Strawberry perl, upgrading Moose and getting all the other perls in a row. I tried to work on my DA again and got

Class::MOP::load_class is deprecated at C:/Dwimperl/perl/site/lib/Class/MOP.pm line 69

yet agin.

Well this time I took the time to look at the rather shortened message, only about 15 lines compared to about 120+ for the first time I tired these new MooseXes so at least some things have been fixed up.

Anyway I spotted the problem seems 'MooseX::Role::Parameterized::Meta::Role::Parameterized' needed to be updated if MooseX::RelatedClassRoles is going to work. So a quick cpan install and the latest version was up and running. Now I only get

MooseX::RelatedClassRoles::__ANON__::SERIAL::1' requires the method 'LSD_class' to be implemented by 'DA_RC' at C:\Dwimperl\perl\site\lib\Moose\Meta\Role.pm line 472


So at least I can proceed. It is nice to see, reading though a few bug reports, how quickly the maintainers of that mod got the fix in very quickly though it seems I must have a very old version installed as the fix came in back in September 2013. Well at least I am nice and up to date now.

So today I am looking at MooseX::RelatedClassRoles and I have to adjust my DA class a little by adding in this first;

use Moose;
with 'MooseX::RelatedClassRoles' => { name => 'DA::LSD'};
and I get the error above

MooseX::RelatedClassRoles::__ANON__::SERIAL::1' requires the method 'DA::LSD_class...
now I do not like the name 'DA::LSD_class' but fortunately HDP added in 'class_accessor_name' so I duly added that and the required class

use Moose;
with 'MooseX::RelatedClassRoles' => { name => 'DA::LSD', 
class_accessor_name=>'lsd_driver'};

has lsd_driver => (
default => 'DA::LSD',
is => 'rw',
);


Now the above accessor is just a simple string scalar as the is the name of the class I wand to apply the role to. So after addeding the above and re-running my tests I get

ok 1 - use DA_RC;

and now I need another param to name to sub that will apply the LSD role and I am off to the races.


use Moose;
with 'MooseX::RelatedClassRoles' => { name => 'DA::LSD', 
class_accessor_name=>'lsd_driver',
apply_method_name=> 'install_lsd',
};
Now in retrieve I do this

sub retrieve {
   my $self=shift;
   my ($conn,$container,$opt) = @_;
   
   if ($conn eq 'DBH'){
     $self->install_lsd("DA_RC::LSD::SQL");
   }
   elsif ($conn eq 'MONGO'){
     $self->install_lsd("DA_RC::LSD::Mongo");
   }
    return $self->lsd_driver->DA($self);
   return $self->lsd_driver->_execute("retrieve",$conn,$container,$opt);
}
and it all should work but opps!

Can't use string ("Moose::Meta::Class::__ANON__::SE"...) as a HASH ref while "strict refs" in use at accessor DA::LSD::DA (defined at D:\GitHub\DA-blog\lib/DA_RC.pm line 109) line 6.

Seem that when you use this you can only access the methods in the underlying new lsd_driver and not attributes so to get things to work I had to pass the DA down into the lsd_driver like this,

 --return $self->lsd_driver->DA($self);
   return $self->lsd_driver->_execute($self,"retrieve",$conn,$container,$opt);
and then make a few quick changes in the LSD like this

--my ( $da,$connection, $container, $opts ) = @_;
++my ( $da,$connection, $container, $opts ) = @_;

-- foreach my $element ( @{ $self->DA->elements() } ) {
++ foreach my $element ( @{ $da->elements() } ) {


and all my tests pass.

Well not a great change from what I had before and I not 100% sold on sending the DA down as a param shure ash shulock someone will muck with it or forget to use it.

Well lets see what the next one brings, or maybe some more time to look at how attributes could be used with this I tried good old trad prel OO sub

sub lsd {
  my $self = shift'
  my $lsd = @_;
  $self->{lsd} = $lsd
     if ($lsd);

  return $self->{lsd};

}
and got the same
Can't use string ("Moose::Meta::Class::__ANON__::SE"...) as a HASH …

Well onto other things tomorrow.

white.jpeg

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