Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created July 4, 2012 06:59
Show Gist options
  • Save ynonp/3045801 to your computer and use it in GitHub Desktop.
Save ynonp/3045801 to your computer and use it in GitHub Desktop.
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