Re: Bug / feature / idiot user?
[email protected] (Paul Driver)
| Newsgroups | perl.moose |
|---|---|
| Message-ID | <[email protected]> |
One way to do is is to do the 'has' the supplies the delegated
method before making the requires check, a la:
<code>
package A::Role;
use Moose::Role;
requires qw(a_method);
package Method::Provider;
use Moose;
sub a_method {
warn "called";
}
package A::Class;
use Moose;
# 'with' wouldn't work here, because 'has' hasn't happened yet
has 'an_attribute' => (
isa => 'Method::Provider',
handles => ['a_method'],
default => sub { Method::Provider->new },
);
# It works here because 'with' is just a function that applies the
# role, which is now in the class (because 'has' installed it!)
with 'A::Role';
package main;
my $foo = A::Class->new;
$foo->a_method();
</code>
Kind of annoying that it's order-dependant like that, but that's the
way it's implemented. :)
On Mon, Sep 22, 2008 at 03:10:26PM +0100, Andy Armstrong wrote:
> On 22 Sep 2008, at 14:48, Dave Rolsky wrote:
>> Ah, sorry, I missed that part.
>
> Figured :)
>
>> The problem is that attributes cannot satisfy a "requires"
>> requirement. The requires check happens before the attributes have a
>> chance to make their methods.
>
>
> I assumed that it was something of that sort. The only thing that
> confused me was that I thought I had it working - but it seems that /
> that/ was almost certainly finger trouble.
>
> Anyway, just to reiterate: I'm absolutely loving Moose - thanks all :)
>
> --
> Andy Armstrong, Hexten
>
>
>