Created
June 5, 2012 20:08
-
-
Save ynonp/2877490 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 strict; | |
use warnings; | |
package RoleMaker; | |
use Moose; | |
use File::Slurp qw/write_file/; | |
has 'name', is => 'ro', required => 1; | |
has 'methods' => ( | |
is => 'rw', | |
traits => ['Array'], | |
handles => { add_method => 'push' } | |
); | |
sub AUTOLOAD { | |
our $AUTOLOAD; | |
my $self = shift; | |
my $method_name = $AUTOLOAD; | |
$method_name =~ s/.*://; | |
return if $method_name=~ /DESTROY/; | |
$self->add_method( $method_name ); | |
} | |
sub write_pm { | |
my $self = shift; | |
my $methods = 'qw/' . join(' ', @{ $self->methods }) . '/'; | |
my $pkg_name = $self->name; | |
my $doc = <<"END"; | |
package $pkg_name; | |
use Moose::Role; | |
requires $methods; | |
1; | |
END | |
write_file $self->name . '.pm', $doc; | |
} | |
package main; | |
my $rm = RoleMaker->new( name => 'TestRole' ); | |
$rm->foo; | |
$rm->bar; | |
$rm->write_pm; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment