Created
October 21, 2011 16:41
-
-
Save melo/1304292 to your computer and use it in GitHub Desktop.
Create delegations dynamically on roles
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 DB; | |
use Moose; | |
sub create { | |
use Data::Dump qw(pp); print STDERR ">>>>>> ", pp(@_), "\n"; | |
} | |
package Role; | |
use Moose::Role; | |
has 'db' => ( isa => 'DB', is => 'ro', default => sub { DB->new } ); | |
my $meta = __PACKAGE__->meta; | |
for my $m (qw( create read update delete )) { | |
$meta->add_method($m, sub { my $s = shift; return $s->db->$m($s, @_) }); | |
} | |
package My; | |
use Moose; | |
with 'Role'; | |
package main; | |
my $o = My->new; | |
$o->create; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment