Created
September 22, 2010 14:00
-
-
Save timbunce/591718 to your computer and use it in GitHub Desktop.
perl 6 method wrapping via a role
This file contains 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
role R { | |
method m() { say 42 } | |
} | |
class C does R { | |
} | |
my $x = C.new; | |
log-calls($x, R); | |
my @log; | |
sub log-calls($obj, Role $r) { | |
my $wrapper = RoleHOW.new; | |
for $r.^methods -> $m { | |
$wrapper.^add_method($m.name, method (|$c) { | |
push @log, "called $m.name"; | |
nextsame; | |
}); | |
} | |
$obj does $wrapper.^compose(); | |
} | |
$x.m(); | |
say @log.perl; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment