App-TypecastTemplates

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN


Format records from a table with different templates.

This module allows to print records from a table using different templates.
The template to be used for a record is chosen by the value of the column
named "type" for that record.

The table is expected to be CSV formatted.

The original use case for this module is a script that allows to generate
.cloginrc files for RANCID to easily add new credentials. The accompanying
script does exact this and may be used as an example to create other scripts
using this module.

The module can be used without any script using the following command line:

        perl -MApp::TypecastTemplates -e tt_run

It expects the table in CSV format at STDIN and prints the formatted records
to STDOUT.

bin/create_cloginrc  view on Meta::CPAN


Version v0.3.0

=cut

our $VERSION = 'v0.3.0';

=head1 SYNOPSIS

 create_loginrc -h
 create_loginrc [options] credentials.csv > .cloginrc
 create_loginrc [options] < credentials.csv > .cloginrc

=cut

use App::TypecastTemplates;
use Getopt::Long;
use Pod::Usage;

=head1 OPTIONS

=over 8

bin/create_cloginrc  view on Meta::CPAN

if (exists $opts->{columns}) {
	set_columns($opts->{columns});
}

tt_run;

=head1 DESCRIPTION

This program takes a CSV (comma separated value) file
and turns it into something that is suitable
to use as credentials file (.cloginrc) for RANCID.

=head2 CSV Input

The CSV file may have arbitrary columns
as long as there is a column named I<< type >>,
which is used to switch between different templates,
and a column for every variable used in the templates.

The column names should either be given in the first line of the CSV file
or with the command line options C<< --columns >>

lib/App/TypecastTemplates.pm  view on Meta::CPAN

	if (!keys %$templates) {
		print "\$0: $0\n";
		if ($0 cmp '-e') {
			read_templates(\*main::DATA);
		}
		if (!keys %$templates) {
			read_templates(\*DATA);
		}
	}
	open(my $handle, '<' . $fn)
		or die "can't open credentials file '$fn'";
	if ($columns) {
		$csv->column_names( split(/,/,$columns) );
	}
	else {
		my @cols = $csv->getline( $handle );
		$csv->column_names( @cols );
	}
	while (my $r = $csv->getline_hr( $handle )) {
		if (exists $templates->{$r->{type}}) {
			my $template = $templates->{$r->{type}};



( run in 1.563 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )