Learn Moose X: handles
Writing Moose is really very fun and happy.
I'm happy that sunnavy picked up the Moose for Net::Google::Code.
Today I'm implementing some attributes for the Net::Google::Code. the code would be something like
with Moose, with hanldes, we can simply write code like
in this way, it makes the code really clean and small.
http://code.bestpractical.com/bps-public/Net-Google-Code/trunk/lib/Net/Google/Code.pm
http://code.bestpractical.com/bps-public/Net-Google-Code/trunk/lib/Net/Google/Code/Home.pm
the magic is $project->owners == $project->home->owners, more example can be found: http://search.cpan.org/src/DROLSKY/Moose-0.64/t/020_attributes/010_attribute_delegation.t.
if you never use Moose before, you SHOULD use Moose today!
Thanks.
I'm happy that sunnavy picked up the Moose for Net::Google::Code.
Today I'm implementing some attributes for the Net::Google::Code. the code would be something like
use Net::Google::Code;
my $project = Net::Google::Code->new( project => 'net-google-code' );
print join(', ', @{ $project->owners } );so in old plain Perl code, you must write code in Net/Google/Code.pm or else you need sub owners { Net::Google::Code::Home->new()->owners }, something like that.with Moose, with hanldes, we can simply write code like
has 'home' => (
isa => 'Net::Google::Code::Home',
is => 'ro',
lazy => 1,
default => sub {
require Net::Google::Code::Home;
Net::Google::Code::Home->new( parent => $_[0] );
},
handles => [ 'owners', 'members' ],
);in this way, it makes the code really clean and small.
http://code.bestpractical.com/bps-public/Net-Google-Code/trunk/lib/Net/Google/Code.pm
http://code.bestpractical.com/bps-public/Net-Google-Code/trunk/lib/Net/Google/Code/Home.pm
the magic is $project->owners == $project->home->owners, more example can be found: http://search.cpan.org/src/DROLSKY/Moose-0.64/t/020_attributes/010_attribute_delegation.t.
if you never use Moose before, you SHOULD use Moose today!
Thanks.
Labels: Moose
3 Comments:
Late last night I was working on a project that needed solution like this and also came across Moose's "handles".
Initially I came across this...
http://search.cpan.org/dist/Moose/lib/Moose/Cookbook/FAQ.pod#How_do_I_make_non-Moose_constructors_work_with_Moose?
This suggested delegation approach but no clear example so after a bit more surfing I found the tests u've already linked to and also....
http://www.iinteractive.com/moose/slides/slide20d.html
This approach hit the spot!
/I3az/
Moose is really very nice. :)
Certainly is!
Also like that u can include other attribs in the handles like so...
has 'url' => ( isa => 'Str', is => 'ro', default => 'http://freshmeat.net/xmlrpc/' );
has 'agent' => (
isa => 'XML::RPC',
is => 'ro',
default => sub { XML::RPC->new( $_[0]->{url} ) },
handles => [ qw/call/ ],
lazy => 1,
required => 1,
);
NB. Hopefully Blogger won't mangle above code too mich!
Also playing with Moose Roles last night... now there are very nice.
/I3az/
Post a Comment
Links to this post:
Create a Link
<< Home