App-TypecastTemplates
view release on metacpan or search on metacpan
lib/App/TypecastTemplates.pm view on Meta::CPAN
use Exporter;
our @ISA = qw( Exporter );
my $templates = {};
my $columns = "";
=head1 SUBROUTINES/METHODS
=head2 set_columns
Set the column names for a CSV file
that doesn't provide them in the first line.
=cut
sub set_columns {
$columns = shift;
} # set_columns()
=head2 tt_file
Read a file that defines the templates.
=cut
sub tt_file {
my ($fn) = @_;
open(my $handle, '<' . $fn)
or die "can't open template file '$fn'";
read_template($handle);
close($handle);
}
=head2 tt_run
Run the application as in
perl -MApp::TypecastTemplates -e tt_run
=cut
sub tt_run {
my $tt = new Template();
my $csv = Text::CSV->new({
binary => 1,
auto_diag => 1,
sep_char => ',',
});
my $fn = $main::ARGV[0] || '-';
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}};
$tt->process(\$template, $r);
}
elsif (exists $templates->{'*'}) {
my $template = $templates->{'*'};
$tt->process(\$template, $r);
}
else {
die "No template for type '$r->{type}'";
}
}
close($handle);
} # tt_run()
=head2 read_templates
=cut
sub read_templates {
my ($fh) = @_;
$templates = {};
while (my $tl = <$fh>) {
my ($type,$line) = split /:/, $tl, 2;
if (exists $templates->{$type}) {
$templates->{$type} .= $line;
}
else {
$templates->{$type} = $line;
}
}
} # read_templates
=head1 AUTHOR
Mathias Weidner, C<< <mamawe at cpan.org> >>
=head1 BUGS
Please report any bugs or feature requests to C<bug-app-typecasttemplates at rt.cpan.org>, or through
the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=App-TypecastTemplates>. I will be notified, and then you'll
automatically be notified of progress on your bug as I make changes.
=head1 SUPPORT
You can find documentation for this module with the perldoc command.
perldoc App::TypecastTemplates
You can also look for information at:
( run in 0.981 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )