Created
July 4, 2012 06:59
-
-
Save ynonp/3045801 to your computer and use it in GitHub Desktop.
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
use v5.14; | |
my %cache; | |
package MyDevice; | |
use Moose; | |
sub talk_to_device { return 7 } | |
package MyDummyDevice; | |
use Moose::Role; | |
sub init { | |
my $self = shift; | |
foreach my $method ( $self->meta->get_all_methods ) { | |
warn $method->name; | |
} | |
} | |
around 'talk_to_device' => sub { | |
my $orig = shift; | |
if ( ! $cache{talk_to_device} ) { | |
# call original function | |
warn 'calling original function'; | |
$cache{talk_to_device} = $orig->(@_); | |
} | |
return $cache{talk_to_device}; | |
}; | |
package main; | |
use Moose::Util qw/apply_all_roles/; | |
my $d = MyDevice->new; | |
apply_all_roles( $d, 'MyDummyDevice' ); | |
$d->init; | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment