File Coverage

blib/lib/App/TypecastTemplates.pm
Criterion Covered Total %
statement 17 51 33.3
branch 0 18 0.0
condition 0 2 0.0
subroutine 6 10 60.0
pod 4 4 100.0
total 27 85 31.7


line stmt bran cond sub pod time code
1             package App::TypecastTemplates;
2              
3 1     1   75163 use 5.006;
  1         4  
4 1     1   6 use strict;
  1         2  
  1         23  
5 1     1   5 use warnings;
  1         2  
  1         57  
6              
7 1     1   710 use Template;
  1         24725  
  1         38  
8 1     1   1036 use Text::CSV;
  1         23643  
  1         85  
9              
10             =head1 NAME
11              
12             App::TypecastTemplates - Format records with different templates.
13              
14             =head1 VERSION
15              
16             Version v0.3.0
17              
18             =cut
19              
20             our $VERSION = 'v0.3.0';
21              
22             =head1 SYNOPSIS
23              
24             This module allows to print records from a table using different templates.
25             The template to be used for a record is chosen by the value of the column
26             named "type" for that record.
27             The table is expected to be CSV formatted.
28              
29             The module can be used without any script using the following command line:
30              
31             perl -M App::TypecastTemplates -e tt_run
32              
33             It expects the table in CSV format at STDIN and prints the formatted records
34             to STDOUT.
35              
36             =head1 EXPORT
37              
38             This module exports the function C<< run >>, that does the formatting.
39              
40             =cut
41              
42             our @EXPORT = qw( tt_run read_templates set_columns);
43 1     1   13 use Exporter;
  1         2  
  1         555  
44             our @ISA = qw( Exporter );
45              
46             my $templates = {};
47             my $columns = "";
48              
49             =head1 SUBROUTINES/METHODS
50              
51             =head2 set_columns
52              
53             Set the column names for a CSV file
54             that doesn't provide them in the first line.
55              
56             =cut
57              
58             sub set_columns {
59 0     0 1   $columns = shift;
60             } # set_columns()
61              
62             =head2 tt_file
63              
64             Read a file that defines the templates.
65              
66             =cut
67              
68             sub tt_file {
69 0     0 1   my ($fn) = @_;
70 0 0         open(my $handle, '<' . $fn)
71             or die "can't open template file '$fn'";
72 0           read_template($handle);
73 0           close($handle);
74             }
75              
76             =head2 tt_run
77              
78             Run the application as in
79              
80             perl -MApp::TypecastTemplates -e tt_run
81              
82             =cut
83              
84             sub tt_run {
85              
86 0     0 1   my $tt = new Template();
87 0           my $csv = Text::CSV->new({
88             binary => 1,
89             auto_diag => 1,
90             sep_char => ',',
91             });
92 0   0       my $fn = $main::ARGV[0] || '-';
93 0 0         if (!keys %$templates) {
94 0           print "\$0: $0\n";
95 0 0         if ($0 cmp '-e') {
96 0           read_templates(\*main::DATA);
97             }
98 0 0         if (!keys %$templates) {
99 0           read_templates(\*DATA);
100             }
101             }
102 0 0         open(my $handle, '<' . $fn)
103             or die "can't open credentials file '$fn'";
104 0 0         if ($columns) {
105 0           $csv->column_names( split(/,/,$columns) );
106             }
107             else {
108 0           my @cols = $csv->getline( $handle );
109 0           $csv->column_names( @cols );
110             }
111 0           while (my $r = $csv->getline_hr( $handle )) {
112 0 0         if (exists $templates->{$r->{type}}) {
    0          
113 0           my $template = $templates->{$r->{type}};
114 0           $tt->process(\$template, $r);
115             }
116             elsif (exists $templates->{'*'}) {
117 0           my $template = $templates->{'*'};
118 0           $tt->process(\$template, $r);
119             }
120             else {
121 0           die "No template for type '$r->{type}'";
122             }
123             }
124 0           close($handle);
125             } # tt_run()
126              
127             =head2 read_templates
128              
129             =cut
130              
131             sub read_templates {
132 0     0 1   my ($fh) = @_;
133 0           $templates = {};
134              
135 0           while (my $tl = <$fh>) {
136 0           my ($type,$line) = split /:/, $tl, 2;
137 0 0         if (exists $templates->{$type}) {
138 0           $templates->{$type} .= $line;
139             }
140             else {
141 0           $templates->{$type} = $line;
142             }
143             }
144             } # read_templates
145              
146             =head1 AUTHOR
147              
148             Mathias Weidner, C<< >>
149              
150             =head1 BUGS
151              
152             Please report any bugs or feature requests to C, or through
153             the web interface at L. I will be notified, and then you'll
154             automatically be notified of progress on your bug as I make changes.
155              
156             =head1 SUPPORT
157              
158             You can find documentation for this module with the perldoc command.
159              
160             perldoc App::TypecastTemplates
161              
162              
163             You can also look for information at:
164              
165             =over 4
166              
167             =item * RT: CPAN's request tracker (report bugs here)
168              
169             L
170              
171             =item * AnnoCPAN: Annotated CPAN documentation
172              
173             L
174              
175             =item * CPAN Ratings
176              
177             L
178              
179             =item * Search CPAN
180              
181             L
182              
183             =back
184              
185              
186             =head1 ACKNOWLEDGEMENTS
187              
188              
189             =head1 LICENSE AND COPYRIGHT
190              
191             This software is Copyright (c) 2021 by Mathias Weidner.
192              
193             This is free software, licensed under:
194              
195             The Artistic License 2.0 (GPL Compatible)
196              
197              
198             =cut
199              
200             1; # End of App::TypecastTemplates
201              
202             __DATA__