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
    This topic came up on a Perl 6 mailing list, and the answer there was to read up on Synopsis 6 Wrapping

    As for an explanation of that section- await the wisdom of a more experienced monk!

    update: added link to mailing list archive, just in case the conversation there continues beyond "see S06". And apologies for creating a circle, if you are the original poster there!

      I am the origin poster in the mailing list for the same topic. Is it not OK for asking same questions in two places?
        Is it not OK for asking same questions in two places?

        It is OK. Some (like those who frequent both places) prefer that you disclose that upfront to cut-down on duplication of effort.