Skip to content

Instantly share code, notes, and snippets.

@kidd
Created January 6, 2011 02:29
Show Gist options
  • Save kidd/767409 to your computer and use it in GitHub Desktop.
Save kidd/767409 to your computer and use it in GitHub Desktop.
Generates a minimal scaffold for perl Moose classes
#!/usr/bin/perl
# vim: set tabstop=4 shiftwidth=4 foldmethod=marker :
#{{{
=head1 NAME
genclass.pl - Generates templates for moose classes
=head1 SEE ALSO
Vim, emacs, or any decent editor with macros
=head1 SYNOPSIS
kidd@yoda [ /tmp/proj ] %genclass.pl lib/My/App/Module
#generates package My::App::Module
#TIP: use tab completion wisely :)
=head1 DESCRIPTION
Moose classes are really concise and expressive, and there's
very little boilerplate to write, but I keep forgetting
writting the right paths and so. A couple of attributes are
provided to show default and lazy attributes.
=head1 AUTHOR
Raimon Grau Cuscó <[email protected]>
=head1 Credits
=cut#}}}
use Data::Dumper;
use Getopt::Long;
use Pod::Usage;
use File::Path qw(make_path);
use strict;
use warnings;
sub man {#{{{
pod2usage(
-exitval => 1,
-verbose => 2
);
}#}}}
my $path_and_classname=shift or die "must supply a path+classname";
die "file $path_and_classname already exists" if -f "$path_and_classname.pm";
$path_and_classname =~ m[^(.*)/(.*)] ;
my $class_path = $1;
my $class_name = $2;
make_path $1 and warn "Warning: directory $1 alredy exists. continuing";
open my $fh ,'>',"$path_and_classname.pm" or die "cannot create file. $!";
$path_and_classname =~ s|^lib/||;
$path_and_classname =~ s|/|::|g;
while ( <DATA> ) {
s/%%%/$path_and_classname/;
print $fh $_;
}
# main
GetOptions (
'man' => \&man,
);
__END__
package %%%;
use Moose;
use autobox;
use autobox::Core;
use Perl6::Say;
has 'one' => (is =>'ro', isa =>'Int' , default=> sub { 1 }});
has 'attr' => (is =>'rw', isa =>'Num' , lazy_build=>1);
sub _build_attr {
my $self = shift;
return $self->one+1;
}
no Moose;
__PACKAGE__->meta->make_immutable;
1; # End of %%%;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment