Listman/exporter.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 99 CC 5 SCAR 14.2
CC
5
Cov
0%
new
6.0
set_file_name
6.0
save_file
6.0
CRAP 4.1
1 # exporter.pm - this file is part of the CGI::Listman distribution
2 #
3 # CGI::Listman is Copyright (C) 2002 iScream multimédia <info@iScream.ca>
4 #
5 # This package is free software; you can redistribute it and/or
6 # modify it under the same terms as Perl itself.
7 #
8 # Author: Wolfgang Sourdeau <Wolfgang@Contre.COM>
9  
10 use strict;
11  
12 package CGI::Listman::exporter;
13  
14 use Carp;
15 use Text::CSV_XS;
16  
17 =pod
18  
19 =head1 NAME
20  
21 CGI::Listman::exporter - exporting database records with bookkeeping
22  
23 =head1 SYNOPSIS
24  
25 use CGI::Listman::exporter;
26  
27 =head1 DESCRIPTION
28  
29 CGI::Listman::exporter provides some simple API to export instances of
30 I<CGI::Listman::line> to "Comma-Separated Values"-formatted files. Such
31 files will generally be transmitted through a CGI (see the
32 I<file_contents> method below). Between instances of this CGI, you want
33 to use the I<save_file> method since there is no way for I<CGI::Listman>
34 to know when a CGI session get closed.
35  
36 =head1 API
37  
38 =head2 new
39  
40 Creates an initialized instance of I<CGI::Listman::exporter>.
41  
42 =over
43  
44 =item Parameters
45  
46 All the parameters are optional with this method.
47  
48 =over
49  
50 =item filename
51  
52 An optional string representing the filename of the export file.
53  
54 =item separator
55  
56 An optional single-character string representing the CSV separator to be
57 used. If not specified, the default will of course be a comma.
58  
59 =back
60  
61 =item Return values
62  
63 An instance of I<CGI::Listman::exporter>
64  
65 =back
66  
67 =cut
68  
69 sub new {
70 my $class = shift;
71  
72 my $self = {};
73  
74 my @lines;
75 $self->{'file_name'} = shift;
76 $self->{'separator'} = shift || ',';
77 $self->{'lines'} = \@lines;
78 $self->{'_csv'} = Text::CSV_XS->new ({sep_char => $self->{'separator'},
79 binary => 1});
80 $self->{'_file_read'} = 0;
81  
82 bless $self, $class;
83 $self->_read_file () if (defined $self->{'file_name'});
84  
85 return $self;
86 }
87  
88 =pod
89  
90 =head2 set_file_name
91  
92 This method is of mandatory use if the filename is not specified when
93 calling the I<new> method. It cannot however be called twice.
94  
95 =over
96  
97 =item Parameters
98  
99 =over
100  
101 =item filename
102  
103 A string representing the filename of the export file.
104  
105 =back
106  
107 =item Return values
108  
109 This methods returns nothing.
110  
111 =back
112  
113 =cut
114  
115 sub set_file_name {
116 my ($self, $file_name) = @_;
117  
118 croak "A file name is already defined for this instance"
119 ." of CGI::Listman::exporter.\n"
120 if (defined $self->{'file_name'});
121 $self->{'file_name'} = $file_name;
122 $self->_read_file ();
123 }
124  
125 =pod
126  
127 =head2 set_separator
128  
129 This method's name is quite explicit. It is useful when people want to
130 use any other separator than the comma. People should however not be
131 afraid of using the latter since even textual data containing such a
132 character will be encapsulated in a way that prevent formatting
133 conflicts.
134  
135 =over
136  
137 =item Parameters
138  
139 =over
140  
141 =item separator
142  
143 A single-character string representing the separator in the CSV file.
144  
145 =back
146  
147 =item Return values
148  
149 This methods returns nothing.
150  
151 =back
152  
153 =cut
154  
155 sub set_separator {
156 my ($self, $sep) = @_;
157  
158 $self->{'separator'} = $sep;
159 }
160  
161 =pod
162  
163 =head2 add_line
164  
165 Add an instance of I<CGI::Listman::line> to your instance of
166 I<CGI::Listman::exporter>.
167  
168 =over
169  
170 =item Parameters
171  
172 =over
173  
174 =item line
175  
176 A single instance of I<CGI::Listman::line> to be exported.
177  
178 =back
179  
180 =item Return values
181  
182 This method returns nothing.
183  
184 =back
185  
186 =cut
187  
188 sub add_line {
189 my ($self, $line) = @_;
190  
191 my $csv = $self->{'_csv'};
192  
193 my $data_ref = $line->{'data'};
194 my @columns = @$data_ref;
195 $csv->combine (@columns);
196 my $csv_line = $csv->string ();
197 my $lines_ref = $self->{'lines'};
198 push @$lines_ref, $csv_line;
199 $line->mark_exported ();
200 }
201  
202 =pod
203  
204 =head2 add_selection
205  
206 Add instances of I<CGI::Listman::line> in batch to your instance of
207 I<CGI::Listman::exporter>.
208  
209 =over
210  
211 =item Parameters
212  
213 =over
214  
215 =item selection
216  
217 An instance of I<CGI::Listman::selection>.
218  
219 =back
220  
221 =item Return values
222  
223 This method returns nothing.
224  
225 =back
226  
227 =cut
228  
229 sub add_selection {
230 my ($self, $selection) = @_;
231  
232 my $sel_list_ref = $selection->{'list'};
233 foreach my $line (@$sel_list_ref) {
234 $self->add_line ($line);
235 }
236 }
237  
238 =pod
239  
240 =head2 file_contents
241  
242 =over
243  
244 =item Parameters
245  
246 This method takes no parameter.
247  
248 =item Return values
249  
250 A perl string containing the contents of the export file in a
251 DOS-formatted fashion (with both a "linefeed" and a "carriage return" at
252 the end of each line). This is to ensure compatibility with the
253 world-dominating operating systems.
254  
255 =back
256  
257 =cut
258  
259 sub file_contents {
260 my $self = shift;
261  
262 my $contents = undef;
263 my $lines_ref = $self->{'lines'};
264 foreach my $line (@$lines_ref) {
265 $contents .= $line."\r\n";
266 }
267  
268 return $contents;
269 }
270  
271 =pod
272  
273 =head2 save_file
274  
275 Except when defining and exporting the data put in a
276 I<CGI::Listman::exporter> during the same CGI session, it is wise to save
277 its contents in-between.
278  
279 =over
280  
281 =item Parameters
282  
283 This method takes no parameter.
284  
285 =item Return values
286  
287 This method returns nothing.
288  
289 =back
290  
291 =cut
292  
293 sub save_file {
294 my $self = shift;
295  
296 croak "No file to export to.\n"
297 unless (defined $self->{'file_name'});
298 my $contents = $self->file_contents ();
299  
300 open EFOUT, '>'.$self->{'file_name'}
301 or croak "Could not open export file (\""
302 .$self->{'file_name'}."\") for writing.\n";
303 print EFOUT $contents;
304 close EFOUT;
305 }
306  
307 ### private methods
308  
309 sub _read_file {
310 my $self = shift;
311  
312 if (-f $self->{'file_name'}) {
313 open EFIN, $self->{'file_name'}
314 or croak "Could not open export file ('".$self->{'file_name'}."').\n";
315  
316 my $lines_ref = $self->{'lines'};
317 while (<EFIN>) {
318 my $line = $_;
319 chomp $line;
320 push @$lines_ref, $line;
321 }
322 close EFIN;
323  
324 $self->{'_file_read'} = 1;
325 }
326 }
327  
328 1;
329 __END__