Skip to content

Instantly share code, notes, and snippets.

@timbunce
Created September 22, 2010 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timbunce/591718 to your computer and use it in GitHub Desktop.
Save timbunce/591718 to your computer and use it in GitHub Desktop.
perl 6 method wrapping via a role
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