Re: Import x from package

[email protected] (Stevan Little)
Newsgroups perl.moose
Message-ID <[email protected]>
Christopher,

For roles, there is currently no easy way to exclude attributes or  
their generated accessors, only true methods (generated accessors  
don't count because they are not created at the time when the role is  
consumed). It is a new feature, so there is not many docs on this  
yet, but there are some tests for it, look here http:// 
search.cpan.org/src/STEVAN/Moose-0.40/t/030_roles/ 
012_method_exclusion_in_composition.t for some examples.

For classes, you don't want to do this, it is just plain bad OO  
practices. And to be honest, if you find that you are having this  
issues with Roles too, you probably want to break up your role into 2  
smaller roles. Another programmer would expect that if you consumed  
RoleX or subclassed ClassX and those both had method foo_bar, that  
foo_bar would be there, by removing it you are setting yourself and  
others up for issues.

- Stevan

On Mar 31, 2008, at 4:00 PM, Christopher Brown wrote:
> Stevan,
>
> I hope you and the rest of the Moose list is not getting sick of  
> me.  I am having a much fun with Moose and hope that I am  
> contributing a smidge to your efforts by kicking the tires and  
> reporting my troubles.
>
> Here is something I got stuck on recently:
>
> Is there a way to import only one or several, but not all atribute/ 
> method/symbol from a Moose role or class.  Sorta like the import  
> tags if you are using Exporter -or- like Python's import x from y.   
> Say I want to extend a class but don't want all of it's methods.   
> What is the Moose way?  I can, of course, undef unwanted symbols,  
> like so:
>
> use feature 'say';
> package Parent;
> use Moose;         # awesome!
>
>     has 'attrib_1' => ( is => 'rw', isa=> 'Str', default => 'one' );
>     has 'attrib_2' => ( is => 'rw', isa=> 'Str', default => 'two' );
>
> package Child;
> use Moose;         # Even better!
>     extends 'Parent';
>
>     undef &attrib_1;
>
> package main;
>
>     my $child = Child->new;
>     say $child->attrib_2;
>     say $child->attrib_1;
>
>
> But there seems like there should be a better way.
>
> Any thoughts on this one.  ( Like all Moose things, I am sure it is  
> simple and straight-forward. )
>
> Thanks in Advance,
>
> 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.