Re: Using attributes for 'requires'

[email protected] (benh)
Newsgroups perl.moose
Message-ID <[email protected]>
so far the best hack is :

sub location;
has location => (...);



On Sun, Jan 18, 2009 at 3:40 AM, Paul Fenwick <[email protected]> wrote:
> G'day Moosers,
>
> I'm currently working on some quick lightning talks demonstrating Moose and
> why it's useful.  One of the examples I'm using for roles looks like this:
>
>        package SunSmart;
>
>        use Moose::Role;
>        use Weather::Google;
>
>        use constant HOT => 28;         # Deg C.
>
>        requires 'go_outside';
>        requires 'location';
>
>        before 'go_outside' => sub {
>            my ($self) = @_;
>
>            my $weather = Weather::Google->new(
>                $self->location
>            );
>
>           if ($weather->current->{temp_c} > HOT) {
>              print "Don't forget your hat!\n";
>           }
>        }
>
> In short, if something has a location, and the ability to go_outside, then
> we can give it the SunSmart role, and it won't forget its hat if it's too hot.
>
> Unfortunately for me, `location` is an attribute on practically all of my
> classes, and Moose doesn't seem to like having an attribute to satisfy a
> 'requires':
>
>        'SunSmart' requires the method 'location' to be implemented by
>        'Person::Social'but the method is only an attribute accessor at
>        /usr/lib/perl5/site_perl/5.8/Moose/Meta/Role/Application.pm line 59
>
> This makes me sad, since I really *do* require a location method, but it's
> absolutely fine if it's an attribute.
>
> I know there have been some discussions about this in the past, with the
> possibility of a 'requires_attr' or similar keyword, although to the best of
> my knowledge this is not yet implemented.
>
> In the meantime, is there any easy[1] way to tell Moose that attributes are
> just fine to satisify 'requires' dependencies?
>
> Many thanks,
>
>        Paul
>
> [1] Since I'm writing short talks for mixed audiences, I can't show anything
> too scary.  Sure, I'm willing to dig around in the guts of all sorts of
> things, but I'm trying to dispel the myth that as a regular Perl programmer
> you should have to.
>
> --
> Paul Fenwick <[email protected]> | http://perltraining.com.au/
> Director of Training                   | Ph:  +61 3 9354 6001
> Perl Training Australia                | Fax: +61 3 9354 2681
>
>



-- 
benh~

http://three.sentenc.es/
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.