################################################## # 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' }