NAME

Code::Generator::Perl - Perl module for generating perl modules

VERSION

0.03

SYNOPSIS

use Code::Generator::Perl;

my $generator = new Code::Generator::Perl(generated_by => 'somescript.pl');

my @fib_sequence = ( 1, 1, 2, 3, 5, 8 );

$generator->new_package( 'Fibonacci' );

$generator->add_comment( 'Single digit fibonacci numbers' );
$generator->add( fib_sequence => \@fib_sequence );
$generator->create_or_die();
# This will generate the file Fibonacci.pm:
#
#     package Fibonacci;
#
#     use strict;
#     use warnings;
#
#     # You should never edit this file. Everything in here is automatically
#     # generated by somescript.pl.
#
#     # Single digit fibonacci numbers
#     our $sequence = [
#       1,
#       1,
#       2,
#       3,
#       5,
#       8
#     ];
#
#     1;

my @single_digit_numbers = ( 1..9 );
$generator->new_package( 'Number::Single::Digit' );
$generator->add(single_digits => \@single_digit_numbers);

# Generates Number/Single/Digit.pm
$generator->create_or_die();

DESCRIPTION

Code::Generator::Perl generates perl modules for you.

The idea is that you specify the module name and what variables it has and it will spit out the .pm files for you, using Data::Dumper to do the actual nitty-gritty work.

It was born out of the need to generate perl modules for representing static data relationship from relational database tables. The static data doesn't change all that often so having them pre-calculated in some perl module somewhere saves precious cpu time that would have been spent on doing table joins to come up with the same data.

Methods

new( option => value, ... )

Creates the generator object. Available options are

outdir

Specifies the directory where the generated files will be saved to.

base_package

The base package to be prepended to the package name.

readonly

Set this to true if you would like all the variables in all the packages to be generated to be readonly. This requires the Readonly module. You can overide this in per-package or per-variable readonly option.

generated_by

Set this to the name of your script so that people that view the generated file know which script generates your generated files.

new_package( 'Package::Name', option => value, ... )

Prepare the generator for creating a new package. Previous contents are cleared. Valid options are:

outdir

The output directory for this package.

use

An array ref to a list of other modules to use. By default 'strict' and 'warnings' are included. Specify the 'nowarnings' and 'nostrict' if you don't want them (see below).

nowarnings

Exclude 'use warnings' if set to true.

nostrict

Exclude 'use strict' if set to true.

package_generated_by

Similar to 'generated_by' option to new but for this package only.

base_package

The base package name to be prepended to this package.

package_readonly

Set to 1 if you would like all variables in this package to be readonly.

add_comment( 'some comment', 'another comment' )

Add comments. They will be joined with newlines.

add( variable_name => $ref, { option => value } )

Add a variable with the given name, pointing to $ref. Options are:

sortkeys

This value will be passed to $Data::Dumper::Sortkeys. See the Data::Dumper documentation for how this value is used.

readonly

If set to 1 the variable will be set to readonly using the Readonly module.

use( 'Foo', 'Bar', ... )

Add "use Foo;", "use Bar;" and so on to the package. It ensures that no packages are used twice.

create( { option => value } )

Write the package into .pm file and try to 'use' it and warn if there is any syntax errors. Options:

verbose

If set to true the package filename is printed to stdout.

create_or_die( $die_message, { option => value } )

Same like create() but die on any syntax error in the created package. If given, $die_message will be printed if the package fails perl's eval. Options:

verbose

If set to true the package filename is printed to stdout.

SEE ALSO

Data::Dumper

AUTHOR

Nazri Ramliy, <ayiehere@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2011 by Nazri Ramliy

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.10.1 or, at your option, any later version of Perl 5 you may have available.