Thanks for the example. I completely ignored the usage and conceptual power of handles. In fact, handles is a dish I haven't learned to cook. Is there a recipe for it in Moose::Cookbook? Can't find it. Well, in any case the section on handles in the Moose.pm POD is extensive and I'm reading it over as I write this.
BTW, the POD mentions a tree example built with handles that would become a recipe, but in the binary tree recipe handles are not used at all. Maybe this "build your own Nexus6" could become a recipe for it.
I had completely overlooked the existence and possibilities of the array-ref feature in prototyping. That's a great golf tip. Nice. Just one thing, it looks likearound [qw eyes ears nose mouth ] => sub { ... code to make accessors return $self here ... }
Anyway, your example of using handles for delegation will probably answer most of my chaining needs, but I'd still don't get the subtleties of using handles against using MooseX::CurriedHandles --something I'll look into carefully soon.
Also, defining relationships --from body to eyes and from eyes to body-- seems a little wordy to me. How about this:
package Body; use Moose; has 'eyes' => ( is=> 'rw', isa=> 'Eyes', metaclass=> 'MooseX::NestedPackage', class=> q{ has 'color'=> (is=> 'rw', default=> 'blue'); } ); package main; my $body=new Body(); ## gets new eyes too print $body->eyes; ## and they're blue $body->eyes(new Eyes(color=>'brown')); $body->eyes->color('green');
Would this syntax be way left field? The extension looks like this (with some quite ugly eval's there).
package MooseX::NestedPackage; use Moose; use Moose::Util; extends 'Moose::Meta::Attribute'; has 'class' => (is=> 'ro',isa=> 'Str'); after 'attach_to_class' => sub { my ($attr, $class) = @_; my $name = $attr->name; # 'eyes' my $class_name = $attr->{isa}; # 'Eyes' my $class_code = $attr->class; # all the antlers my $parent = $class->{package}; # 'Body' my $parent_lc = lc($parent); #'body' $attr->{trigger} = sub { my $self = shift; $self->$name->$parent_lc($self); }; my $code = qq{ package $class_name; use Moose; $class_code; has '$parent_lc' => (is=> 'rw',isa=> '$parent',weak_ref=> 1); }; eval $code; $@ && die qq{ $@:\n$code }; ## default is not needed $attr->{default} = sub{ $class_name->new() }; ## cos defaults should go directly in each nested attribute $attr->{lazy} = 0; ## lazy stopped working outside due to a missi +ng default };
-rodrigo
In reply to Re^3: curried-up moose
by rodd
in thread curried-up moose
by rodd
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |