Skip to content

Instantly share code, notes, and snippets.

@hdp
Created August 29, 2011 19:54
Show Gist options
  • Save hdp/1179234 to your computer and use it in GitHub Desktop.
Save hdp/1179234 to your computer and use it in GitHub Desktop.
package Stuffer;
use Moose;
has all_stuff => (
isa => 'HashRef',
traits => ['Hash'],
handles => {
get_stuffed => 'elements',
_has_stuff => 'exists',
_get_stuff => 'get',
_set_stuff => 'set',
},
);
sub add_stuff {
my ($self, %new_stuff) = @_;
for my $key (keys %new_stuff) {
$self->_set_stuff(
$key,
$self->_has_stuff($key)
? ($self->_get_stuff($key) . $new_stuff{$key})
: $new_stuff{$key}
);
}
}
use Data::Dumper;
my $s = Stuffer->new;
$s->add_stuff(x => 1);
$s->add_stuff(y => 2);
$s->add_stuff(x => 3);
print Dumper { $s->get_stuffed };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment