Module::Generate - Assisting with module generation.
Version 0.28
use Module::Generate; Module::Generate->dist('Planes') ->author('LNATION') ->email('email@lnation.org') ->version('0.01') ->class('Planes') ->abstract('Over my head.') ->our('$type') ->begin(sub { $type = 'boeing'; }) ->new ->pod('Instantiate a new plane.') ->example('my $plane = Planes->new') ->accessor('airline') ->sub('type') ->code(sub { $type }) ->pod('Returns the type of plane.') ->example('$plane->type') ->sub('altitude') ->code(sub { $_[1] / $_[2]; ... }) ->pod('Discover the altitude of the plane.') ->example('$plane->altitude(100, 100)') ->generate; ... Module::Generate->dist('Holiday') ->author('LNATION') ->email('email@lnation.org') ->version('0.01') ->class('Feed::Data') ->use('Data::LnArray') ->our('$holiday') ->begin(sub { $holiday = Data::LnArray->new; }) ->sub('parse') ->sub('write') ->sub('render') ->sub('generate') ->sub('_raw') ->sub('_text') ->sub('_json') ->generate;
Instantiate a new Module::Generate object.
my $mg = Module::Generate->start;
Provide a name for the distribution.
my $dist = Module::Generate->dist('Planes');
Provide a path where the generated files will be compiled.
my $module = Module::Generate->lib('./path/to/lib');
Provide a path where the generated test will be compiled.
my $module = Module::Generate->tlib('./path/to/t');
The author of the distribution/module.
my $module = Module::Generate->author('LNATION');
The authors email of the distribution/module.
my $module = Module::Generate->email('email@lnation.org');
The version number of the distribution/module.
my $version = Module::Generate->version('0.01');
Start a new class/package/module..
my $class = Module::Generate->class('Planes');
Provide abstract text for the class.
$class->abstract('Over my head.');
Provide a synopsis for the class.
$class->synopsis('...');
Disable strict by flag.
$class->no_strict('refs');
Disable warnings by flag.
$class->no_warnings('reserved');
Declare modules that should be included in the class.
$class->use(qw/Moo MooX::LazierAttributes/);
Establish an ISA relationship with base classes at compile time.
Unless you are using the fields pragma, consider this discouraged in favor of the lighter-weight parent.
$class->base(qw/Foo Bar/);
$class->parent(qw/Foo Bar/);
Require library files to be included if they have not already been included.
$class->require(qw/Foo Bar/);
Declare variable of the same name in the current package for use within the lexical scope.
$class->our(qw/$one $two/);
Define a code block is executed as soon as possible.
$class->begin(sub { ... });
Define a code block that is executed just after the unit which defined them has been compiled.
$class->unitcheck(sub { ... });
Define a code block that is executed just after the initial Perl compile phase ends and before the run time begins.
$class->check(sub { ... });
Define a code block that is executed just before the Perl runtime begins execution.
$class->init(sub { ... });
Define a code block is executed as late as possible.
$class->end(sub { ... });
Define an object constructor.
$class->new;
equivalent to:
sub new { my ($cls, %args) = (shift, scalar @_ == 1 ? %{$_[0]} : @_); bless \%args, $cls; }
optionally you can pass your own sub routine.
$class->new(sub { ... });
Define a accessor.
$class->accessor('test');
sub test { my ($self, $value) = @_; if ($value) { $self->{$sub} = $value; } return $self->{$sub} }";
Define a sub routine/method.
my $sub = $class->sub('name');
Define the code that will be run for the sub.
$sub->code(sub { return 'Robert'; });
Provide pod text that describes the sub.
$sub->pod('What is my name?');
Provide a code example which will be suffixed to the pod definition.
$sub->example('$foo->name');
Provide tests for the sub.
$sub->test(['is', '$obj->name', q|'test'|], [ ... ], ...)
Implement a macro that can be inserted across classes.
my $mg = Module::Generate->author('LNATION') ->email('email@lnation.org') ->version('0.01'); $mg->macro('self', sub { my ($self, $value) = @_; }); my $class = $mg->class('Foo'); $class->sub('bar') ->code(sub { &self; $value; }); $mg->generate; ### package Foo; use strict; use warnings; our $VERSION = 0.01; sub bar { my ( $self, $value ) = @_; $value; } 1; __END__
Implement a keyword that can be used accross classes.
my $mg = Module::Generate ->author('LNATION') ->email('email@lnation.org'); $mg->keyword('with', sub { my ($meta) = @_; return qq|with $meta->{with};|; }); $mg->keyword('has', CODE => sub { my ($meta) = @_; $meta->{is} ||= q|'ro'|; my $attributes = join ', ', map { ($meta->{$_} ? (sprintf "%s => %s", $_, $meta->{$_}) : ()) } qw/is required/; my $code = qq| has $meta->{has} => ( $attributes );|; return $code; }, KEYWORDS => [qw/is required/], POD_TITLE => 'ATTRIBUTES', POD_POD => 'get or set $keyword', POD_EXAMPLE => "\$obj->\$keyword;\n\n\t\$obj->\$keyword(\$value);" ); $mg->class('Keyword') ->use('Moo') ->with(qw/'Keyword::Role'/) ->test( ['ok', q|my $obj = Keyword->new( thing => 'abc', test => 'def' )|], ['is', q|$obj->test|, q|'def'|] ) ->has('thing')->required(1) ->test( ['ok', q|my $obj = Keyword->new( thing => 'abc' )|], ['is', q|$obj->thing|, q|'abc'|], ['eval', q|$obj = Keyword->new()|, 'required'] ); $mg->class('Keyword::Role') ->use('Moo::Role') ->has('test')->is(q|'rw'|) ->test( ['ok', q|my $obj = do { eval q{ package FooBar; use Moo; with 'Keyword::Role'; 1; }; 1; } && FooBar->new| ], ['is', q|$obj->test|, q|undef|], ['ok', q|$obj->test('abc')|], ['is', q|$obj->test|, q|'abc'|] );
Compile the code.
$sub->generate();
LNATION, <email at lnation.org>
<email at lnation.org>
Please report any bugs or feature requests to bug-module-generate at rt.cpan.org, or through the web interface at https://rt.cpan.org/NoAuth/ReportBug.html?Queue=Module-Generate. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
bug-module-generate at rt.cpan.org
You can find documentation for this module with the perldoc command.
perldoc Module::Generate
You can also look for information at:
RT: CPAN's request tracker (report bugs here)
https://rt.cpan.org/NoAuth/Bugs.html?Dist=Module-Generate
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Module-Generate
CPAN Ratings
https://cpanratings.perl.org/d/Module-Generate
Search CPAN
https://metacpan.org/release/Module-Generate
This software is Copyright (c) 2020 by LNATION.
This is free software, licensed under:
The Artistic License 2.0 (GPL Compatible)
To install Module::Generate, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Module::Generate
CPAN shell
perl -MCPAN -e shell install Module::Generate
For more information on module installation, please visit the detailed CPAN module installation guide.