llancet has asked for the wisdom of the Perl Monks concerning the following question:
Hi monks!
I just started to play with Perl6, and encountered some problem. Consider such kind of modular codes implemented using Moose:
################################################## # toHash() in base class provides an empty hashref ################################################## { package Base; use Moose; sub toHash { return {} } } ############################################# # In a role, specific attributes are going to # be stored in hashref provided by base class ############################################# { package FooRole; use Moose::Role; has foo => (is=>'rw', isa=>'Str'); has bar => (is=>'rw', isa=>'Str'); around toHash => sub { my($orig,$self) = @_; my $hash = $self->$orig(); # I only want store foo, but not bar $hash->{foo} = $self->foo; return $hash; }; } ################################################### # We may have many other roles that provide further # modifications to the function. # And those roles may be assembled in arbitrary # pattern, which provides flexibility. ################################################### { package Foo; use Moose; extends 'Base'; with 'FooRole'; } my $foo = Foo->new( foo => 'foo_value', bar => 'bar_value' ); my $data = $foo->toHash # produces { foo => 'foo_value' }
Through this way, I can add role-spicific modifications to a method. How can I implement that on Perl6? Somebody told me to use function wrapping, but I don't really understand that...
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Moose method modifier equivalent on Perl 6
by Yary (Pilgrim) on Jun 22, 2010 at 01:51 UTC | |
by llancet (Friar) on Jun 22, 2010 at 03:13 UTC | |
by Anonymous Monk on Jun 22, 2010 at 06:56 UTC | |
by llancet (Friar) on Jun 23, 2010 at 01:08 UTC |