Created
February 10, 2012 18:55
-
-
Save ynonp/1791637 to your computer and use it in GitHub Desktop.
roles as abilities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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