-
-
Save kanatohodets/aff43baf1f6c2564b916 to your computer and use it in GitHub Desktop.
dynamic method generation in 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
package Foobar; | |
use Moose; | |
with 'Foobar::Gigarole', { | |
number => 5 | |
}; | |
1; |
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
use 5.20.2; | |
use Foobar; | |
my $foo = Foobar->new; | |
$foo->blorg_3; | |
$foo->blorg_5; | |
$foo->blorg_1; | |
# blorg_3 was called | |
# blorg_5 was called | |
# blorg_1 was called |
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
package Foobar::Gigarole; | |
use MooseX::Role::Parameterized; | |
parameter number => ( | |
is => 'ro', | |
isa => 'Str', | |
required => 1, | |
); | |
role { | |
my $p = shift; | |
my $num = $p->number; | |
foreach my $i (1 .. $num) { | |
method "blorg_$i" => sub { | |
print "blorg_$i called\n"; | |
} | |
} | |
}; | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment