Re: Extends type questions
[email protected] (benh)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
If you really need to some how pull each in to eachother then this was
the only way I could get things to work:
[My::A]
package My::A;
use Moose;
has b => (
is => 'rw',
isa => 'My::B',
lazy =>1,
default => sub{
use My::B;
My::B->new;
}
);
sub kitten { return 'cute' };
1;
[My::B]
package My::B;
use Moose;
has a => (
is => 'rw',
isa => 'My::A',
lazy => 1,
default => sub{
use My::A;
My::A->new;
}
);
sub hello {return 'world';}
1;
[AB.pl]
#!/usr/bin/perl
use strict;
use warnings;
use Test::More qw{no_plan};
use My::A;
my $a = My::A->new;
is( $a->b->hello, 'world'); #PASS
is( $a->b->a->kitten, 'cute'); #PASS
On 7/2/08, benh <[email protected]> wrote:
> I'm with Chris, this is more of a design issue. What is the common
> part between A and B that could not be pulled out to a role?
>
>
> On 7/2/08, Chris Prather <[email protected]> wrote:
> > On Wed, 2 Jul 2008 17:22:44 -0700, Christopher Brown wrote:
> > > Hi All,
> > >
> > > What is the best way to moosify an existing CPAN class and all the classes
> > > called by this class. I know that *extends* works:
> > >
> > > package MooseX::Module::A;
> > > use Moose;
> > > extends 'Module::A';
> > >
> > >
> > > package MooseX::Module::B;
> > > use Moose;
> > > extends 'Module::B';
> > >
> > > sub my_b_extension {
> > > ...
> > > }
> > >
> > >
> > > But how do you extend both modules when Module::A relies on and calls
> > > Module::B? Such as.
> > >
> > > package Module::A;.
> > >
> > > sub create_b {
> > > my $self = shift;
> > > $self->{b} = Module::B->new();
> > > }
> > >
> > > ...
> > >
> > > Now in:
> > >
> > > package main;
> > > use MooseX::Module::A;
> > > use MooseX::Module::B;
> > >
> > > my $a = Moose::X::Module::A->new(); # Cool
> > > $a->create_b; # Cool
> > > $a->{b}->my_b_extension(); # FAIL!
> > >
> > > In this scenario, I will not see *my_b_extension*. In normal, non-Moose
> > > Perl, I would:
> > >
> > > package Module::AX;
> > > use *base* 'Module::A';
> > > ...
> > >
> > > package Module::BX;
> > > use *base* 'Module::B';
> > > ...
> > >
> >
> >
> > Well base.pm and moose play fairly nice together ... but I'm not sure
> > even base.pm would solve this ...
> >
> > $self->{b} = Module::B->new(); # you hardcode the module name here ...
> > nothing can save you now
> >
> > you'd need to override the create_b method in MX::Module::A ... which
> > you'd have to do in any perl code because you've hardcoded the
> > classname in the method. This isn't a Moose problem, this is a crappy
> > design decision problem.
> >
> > If you want to be able to subclass Module::B ... then create_b either
> > needs to make the classname for B configurable (via a parameter to the
> > method, or an attribute in A or something...) or you need to override
> > it in your subclass of A to call the class you need ... (or you
> > override it with something configurable).
> >
> >
> > -Chris
> >
>
>
>
> --
>
> benh~
>
--
benh~