Skip to content

Instantly share code, notes, and snippets.

@ynonp
Created June 5, 2012 20:08
Show Gist options
  • Save ynonp/2877490 to your computer and use it in GitHub Desktop.
Save ynonp/2877490 to your computer and use it in GitHub Desktop.
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