Re: Extending an attribute with a trait

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

Okay, couple of problems with the code:

> around 'legal_options_for_inheritance' => sub {
>  my @options = inner();
>  push @options, 'foo';
>  return @options;
> };

This is not how to do an around, inner() is for augment, not for  
around. This should be:

around 'legal_options_for_inheritance' => sub {
     my $next = shift;
     my $self = shift;
     my @options = $self->$next();
     push @options, 'foo';
     return @options;
};

Then in your classes you had:

package MyClass;
use Moose;

has 'one' => (
     is => 'rw',
     isa => 'Str',
     traits => [qw/Foo/],
     foo => 'bar',
);

has 'two' => (
     is => 'rw',
     isa => 'Str',
);

package MySubClass;
use Moose;
extends 'MyClass';

has '+two' => (
     traits => [qw/Foo/],
     foo => 'baz',
);

You cannot modify "two" like that, your changing it too late for the  
Foo trait to take effect. The application of the trait takes effect  
after the options are parsed, not before.

package MySubClass;
use Moose;
extends 'MyClass';

has '+one' => (
     foo => 'baz',
);

will work correctly though.

- Stevan





On Jul 16, 2008, at 12:39 PM, Dan Harbin wrote:

> I've attached it.  You may have to twiddle the shabang line for your
> environment.
>
> Thanks for the help!
>
> On Wed, Jul 16, 2008 at 11:35 AM, Stevan Little
> <[email protected]> wrote:
>> Dan,
>>
>> Can you provide a complete and runnable example? It would make it  
>> easier to
>> test/debug.
>>
>> Thanks,
>>
>> - Stevan
>>
>> On Jul 16, 2008, at 12:33 PM, Dan Harbin wrote:
>>
>>> This still doesn't seem to work.  Maybe it only works by  
>>> extending the
>>> attribute metaclass rather than applying a role like I'm trying.
>>>
>>> Here's the code.  Help!
>>>
>>> -----------------------
>>>
>>> package Demo::Meta::Attribute::Trait::Foo;
>>> use Moose::Role;
>>>
>>> has foo => (
>>>    is => 'rw',
>>>    isa => 'Str',
>>>    required => 1,
>>> );
>>>
>>> around 'legal_options_for_inheritance' => sub {
>>>  my @options = inner();
>>>  push @options, 'foo';
>>>  return @options;
>>> };
>>>
>>> package Moose::Meta::Attribute::Custom::Trait::Foo;
>>> sub register_implementation { 'Demo::Meta::Attribute::Trait::Foo' }
>>>
>>>
>>> On Wed, Jul 16, 2008 at 10:35 AM, Stevan Little
>>> <[email protected]> wrote:
>>>>
>>>> Dan,
>>>>
>>>> Roles don't inherit, so you want 'around' instead of 'override'  
>>>> sorry, my
>>>> fault i used a term that is also a keyword :)
>>>>
>>>> - Stevan
>>>>
>>>> On Jul 16, 2008, at 11:16 AM, Dan Harbin wrote:
>>>>
>>>>> Stevan,
>>>>>
>>>>> I changed the code as follows, and it still fails.  This trait
>>>>> doesn't/can't inherit from Moose::Meta::Attribute, so I think the
>>>>> override method is a no-op.  How do I fix this?
>>>>>
>>>>> Dan
>>>>>
>>>>> -----------------
>>>>>
>>>>> package Demo::Meta::Attribute::Trait::Foo;
>>>>> use Moose::Role;
>>>>>
>>>>> has foo => (
>>>>>   is => 'rw',
>>>>>   isa => 'Str',
>>>>>   required => 1,
>>>>> );
>>>>>
>>>>> override 'legal_options_for_inheritance' => sub {
>>>>>  my @options = super();
>>>>>  push @options, 'foo';
>>>>>  return @options;
>>>>> };
>>>>>
>>>>> package Moose::Meta::Attribute::Custom::Trait::Foo;
>>>>> sub register_implementation  
>>>>> { 'Demo::Meta::Attribute::Trait::Foo' }
>>>>
>>>>
>>
>>
>> <test.pl>
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.