Re: Extends type questions

[email protected] (Chris Prather)
Newsgroups perl.moose
Message-ID <[email protected]>
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.