Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created February 10, 2012 18:55
Show Gist options
  • Save ynonp/1791637 to your computer and use it in GitHub Desktop.
Save ynonp/1791637 to your computer and use it in GitHub Desktop.
roles as abilities
package Abilities::Base;
use Moose::Role;
sub attack { }
package Abilities::Strong;
use Moose::Role;
with 'Abilities::Base';
after attack => sub {
my ($self, $enemy) = @_;
$enemy->lose_life(2);
};
package Abilities::Undead;
use Moose::Role;
use Moose::Util qw/apply_all_roles/;
with 'Abilities::Base';
after 'attack' => sub {
my ($self, $enemy) = @_;
apply_all_roles($enemy, 'Abilities::Undead');
};
package Character;
use Moose;
with 'Abilities::Base';
package main;
use Moose::Util qw/apply_all_roles/;
my $joeblow = Character->new;
my $zombie = Character->new;
apply_all_roles($zombie, 'Abilities::Undead');
$zombie->attack($joeblow);
# print 1 because he was hit by a zombie
warn $joeblow->does('Abilities::Undead');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment