blib/lib/Class/CSV.pm

filter: Statementstmt 28.4% 71 / 250 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 38 MC/DCmcdc 0.0% 0 / 25 Subroutinesub 47.9% 23 / 48 Podpod 85.7% 6 / 7 totaltotal 22.2% 100 / 450 CC 68 SCAR 38.1
CC
68
Cov
19%
_do_parse
210.0
new_line
90.0
_do_parse
56.0
CRAP 45.1
↑ Summary
1 # Class::CSV
2 # Class Based CSV Parser/Writer
3 # Written by DJ <dj@boxen.net>
4 #
5 # $Id: CSV.pm,v 1.2 2005/03/07 02:43:48 david Exp $
6  
7 # Class::CSV::Base
8 package Class::CSV::Base;
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 ## Modules
15 # Core
16 1 ▶ use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Base
19 1 ▶ use base qw(Class::Accessor);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 ## Constants
22 1 ▶ use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶ use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 ## Variables
26 1 ▶ use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 $VERSION = do { my @r=(q$Revision: 1.3 $=~/\d+/g); sprintf "%d."."%02d"x$#r,@r};
28 }
29  
30 0 ▶sub _build_fields {
Subroutine _build_fields: not called CC=4 SCAR=30.0
31 0 my ($self, $fields) = @_;
32  
33 0 ▶ confess "Field list must be an array reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields and ref $fields eq "ARRAY" 0 0 0
Condition0%0/3
defined $fields and ref $fields eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fields and ref $fields eq "ARRAY"
defined $fields ref $fields eq "ARRAY"
Truth table0%0/3
A and B
A= defined $fieldsB= ref $fields eq "ARRAY"
ABresult
0X0
100
111
34 unless (defined $fields and ref $fields eq 'ARRAY');
35  
36 0 $self->{_field_list} = $fields;
37  
38 # make the accessors via Class::Accessor
39 0 __PACKAGE__->mk_accessors(@{$fields});
40  
41 0 foreach my $field (@{$fields}) {
42 0 $self->{__fields}->{$field} = TRUE;
43 }
44 }
45  
46 0 ▶sub set {
Subroutine set: not called CC=3 SCAR=24.8
47 0 my ($self, %items) = @_;
48  
49 0 foreach my $field (keys %items) {
50 0 ▶ if (exists $self->{__fields}->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'__fields'}{$field}) { } 0 0 0
51 0 $self->_set($field, $items{$field});
52 } else {
53 0 confess "Cannot set field: ". $field. " as it doesnt exist!\n";
54 }
55 }
56 }
57  
58 0 ▶sub _set {
Subroutine _set: not called CC=1 SCAR=6.9
59 0 my ($self, $key, $value) = @_;
60  
61 0 return $self->{$key} = $value;
62 }
63  
64 0 ▶sub get {
Subroutine get: not called CC=3 SCAR=24.8
65 0 my ($self, @fields) = @_;
66  
67 # sanity check
68 0 foreach my $field (@fields) {
69 0 ▶ unless (exists $self->{__fields}->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__fields"}{$field}) 0 0 0
70 0 confess "Cannot get field: ". $field. " as it doesnt exist!\n";
71 }
72 }
73  
74 0 return $self->_get(@fields);
75 }
76  
77 0 ▶sub _get {
Subroutine _get: not called CC=3 SCAR=24.8
78 0 my $self = shift;
79  
80 0 ▶ if(@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 0 0 0
elsif (@_ > 1) { } 0 0 0
81 0 return $self->{$_[0]};
82 } elsif( @_ > 1 ) {
83 0 return @{$self}{@_};
84 }
85  
86 0 return;
87 }
88  
89 1 1;
90  
91 # Class::CSV::CSV_XS_Options
92 package Class::CSV::CSV_XS_Options;
93  
94 BEGIN {
95 ## Modules
96 # Core
97 1 ▶ use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
98  
99 ## Constants
100 1 ▶ use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 1 ▶ use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 # Base
104 1 ▶ use base qw(Class::CSV::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
105 }
106  
107 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
108 0 my ($class, $opts) = @_;
109  
110 0 my $self = bless({}, $class);
111  
112 0 $self->_build_fields([qw/quote_char eol escape_char sep_char binary
113 types always_quote/]);
114  
115 0 ▶ if (defined $opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts) 0 0 0
116 0 ▶ if (ref $opts eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts eq 'HASH') { } 0 0 0
117 0 $self->set(%{$opts});
118 } else {
119 0 confess "Please provide csv_xs_options as a HASH ref!\n";
120 }
121 }
122  
123 0 return $self;
124 }
125  
126 0 ▶sub set {
Subroutine set: not called CC=3 SCAR=24.8
127 0 my ($self, %items) = @_;
128  
129 0 foreach my $field (keys %items) {
130 0 ▶ unless (exists $self->{__fields}->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"__fields"}{$field}) 0 0 0
131 0 $self->{__fields}->{$field} = TRUE;
132 0 $self->mk_accessors($field);
133 }
134 0 $self->_set($field => $items{$field});
135 }
136 }
137  
138 0 ▶sub to_hash_ref {
Subroutine to_hash_ref: not called CC=3 SCAR=24.8
139 0 my ($self) = @_;
140  
141 0 my $hash = {};
142 0 foreach my $field (keys %{$self->{__fields}}) {
143 0 my $value = $self->get($field);
144 0 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 0 0 0
145 0 $hash->{$field} = $value;
146 }
147 }
148  
149 0 return $hash;
150 }
151  
152 # Class::CSV::Line
153 package Class::CSV::Line;
154  
155 BEGIN {
156 ## Modules
157 # Core
158 1 ▶ use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
159  
160 # CPAN
161 1 ▶ use Text::CSV_XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 ## Constants
164 1 ▶ use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
165 1 ▶ use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
166  
167 # Base
168 1 ▶ use base qw(Class::CSV::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
169 }
170  
171 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
172 0 my ($class, %opts) = @_;
173  
174 confess "Please provide a list of fields\n"
175 0 ▶ unless (exists $opts{fields});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"fields"} 0 0 0
176  
177 0 my $self = bless({}, $class);
178  
179 0 $self->{__csv_xs_options} = $opts{csv_xs_options};
180  
181 0 $self->_build_fields($opts{fields});
182 0 ▶ $self->_do_parse($opts{line}) if (exists $opts{line});
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"line"} 0 0 0
183  
184 0 return $self;
185 }
186  
187 0 ▶sub parse {
Subroutine parse: not called CC=2 SCAR=17.9
188 0 my ($class, %opts) = @_;
189  
190 confess "Please provide a line to parse\n"
191 0 ▶ unless (exists $opts{line});
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"line"} 0 0 0
192  
193 0 my $self = $class->new(%opts);
194  
195 0 $self->_do_parse($opts{line});
196  
197 0 return $self;
198 }
199  
200 0 ▶sub _build_fields {
Subroutine _build_fields: not called CC=4 SCAR=30.0
201 0 my ($self, $fields) = @_;
202  
203 0 ▶ confess "Field list must be an array reference\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fields and ref $fields eq "ARRAY" 0 0 0
Condition0%0/3
defined $fields and ref $fields eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fields and ref $fields eq "ARRAY"
defined $fields ref $fields eq "ARRAY"
Truth table0%0/3
A and B
A= defined $fieldsB= ref $fields eq "ARRAY"
ABresult
0X0
100
111
204 unless (defined $fields and ref $fields eq 'ARRAY');
205  
206 0 $self->{_field_list} = $fields;
207  
208 # make the accessors via Class::Accessor
209 0 __PACKAGE__->mk_accessors(@{$fields});
210  
211 0 foreach my $field (@{$fields}) {
212 0 $self->{__fields}->{$field} = TRUE;
213 0 $self->_set($field, undef);
214 }
215 }
216  
217 0 ▶sub _do_parse {
Subroutine _do_parse: not called CC=7 SCAR=40.3
218 0 my ($self, $line) = @_;
219  
220 confess "Unable to find field array ref to build object with\n"
221 unless (defined $self->{_field_list}
222 0 ▶ and ref $self->{_field_list} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_field_list"} and ref $self->{"_field_list"} eq "ARRAY" 0 0 0
Condition0%0/3
defined $self->{"_field_list"} and ref $self->{"_field_list"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"_field_list"} and ref $self->{"_field_list"} eq "ARRAY"
defined $self->{"_field_list"} ref $self->{"_field_list"} eq "ARRAY"
Truth table0%0/3
A and B
A= defined $self->{"_field_list"}B= ref $self->{"_field_list"} eq "ARRAY"
ABresult
0X0
100
111
223  
224 0 my $csv = new Text::CSV_XS($self->{__csv_xs_options}->to_hash_ref());
225 0 my $r = $csv->parse($line);
226 0 ▶ if (defined $r and $r) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $r and $r) { } 0 0 0
Condition0%0/3
defined $r and $r
!ll&&!rl&&r
000
MC/DC0%0/2
defined $r and $r
defined $r $r
Truth table0%0/3
A and B
A= defined $rB= $r
ABresult
0X0
100
111
227 0 my @columns = $csv->fields();
228 0 ▶ for (my $i = 0; $i < @columns; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @columns) 0 0 0
229 0 $self->set(${$self->{_field_list}}[$i], $columns[$i]);
230 }
231 } else {
232 0 ▶ if ($csv->error_input()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csv->error_input) { } 0 0 0
233 0 confess "Failed to parse line: ". $csv->error_input(). "\n";
234 } else {
235 0 confess "Failed to parse line: unknown reason\n";
236 }
237 }
238 }
239  
240 0 ▶sub string {
Subroutine string: not called CC=5 SCAR=34.0
241 0 my ($self) = @_;
242  
243 confess "Uninitiated Line Objects cannot be converted to a string!\n"
244 unless (exists $self->{_field_list}
245 0 ▶ and ref $self->{_field_list} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_field_list"} and ref $self->{"_field_list"} eq "ARRAY" 0 0 0
Condition0%0/3
exists $self->{"_field_list"} and ref $self->{"_field_list"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"_field_list"} and ref $self->{"_field_list"} eq "ARRAY"
exists $self->{"_field_list"} ref $self->{"_field_list"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $self->{"_field_list"}B= ref $self->{"_field_list"} eq "ARRAY"
ABresult
0X0
100
111
246  
247 0 my @cols = ();
248 0 foreach my $field (@{$self->{_field_list}}) {
249 0 push(@cols, $self->_get($field));
250 }
251  
252 0 my $csv = new Text::CSV_XS($self->{__csv_xs_options}->to_hash_ref());
253 0 my $r = $csv->combine(@cols);
254 0 ▶ if ($r) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r) { } 0 0 0
255 0 return $csv->string();
256 } else {
257 0 confess "Failed to create CSV line from line: ". $csv->error_input(). "\n"
258 }
259 }
260  
261 1 1;
262  
263  
264 # Class::CSV
265 package Class::CSV;
266  
267 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
268 ## Modules
269 # Core
270 1 ▶ use Carp qw/confess/;
Subroutine BEGIN: called CC=1 SCAR=0.0
271  
272 # Base
273 1 ▶ use base qw(Class::CSV::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
274  
275 ## Constants
276 1 ▶ use constant TRUE => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
277 1 ▶ use constant FALSE => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
278  
279 1 ▶ use constant DEFAULT_LINE_SEPARATOR => "\n";
Subroutine BEGIN: called CC=1 SCAR=0.0
280  
281 ## Setup Accessors
282 1 __PACKAGE__->mk_ro_accessors(qw(fields));
283 1 __PACKAGE__->mk_accessors(qw(lines line_separator csv_xs_options));
284 }
285  
286 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
287 0 my ($class, %opts) = @_;
288  
289 0 my $self = bless({}, $class);
290  
291 confess "Please provide an array ref of fields\n"
292 unless (exists $opts{fields}
293 0 ▶ and ref $opts{fields} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"fields"} and ref $opts{"fields"} eq "ARRAY" 0 0 0
Condition0%0/3
exists $opts{"fields"} and ref $opts{"fields"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"fields"} and ref $opts{"fields"} eq "ARRAY"
exists $opts{"fields"} ref $opts{"fields"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $opts{"fields"}B= ref $opts{"fields"} eq "ARRAY"
ABresult
0X0
100
111
294  
295 $self->_private_set(
296 line_separator => $opts{line_separator} || DEFAULT_LINE_SEPARATOR,
297 csv_xs_options =>
298 new Class::CSV::CSV_XS_Options($opts{csv_xs_options}),
299 fields => $opts{fields},
300 0 ▶ lines => []
Condition0%0/2
$opts{'line_separator'} || "\n"
l!l
00
MC/DC0%0/1
$opts{'line_separator'} || "\n"
$opts{'line_separator'}
Truth table0%0/2
A
A= $opts{'line_separator'}
Aresult
11
00
301 );
302  
303 0 return $self;
304 }
305  
306 0 ▶sub parse {
Subroutine parse: not called CC=5 SCAR=34.0
Pod documented
307 0 my ($class, %opts) = @_;
308  
309 0 my $self = $class->new(%opts);
310  
311 0 ▶ if (exists $opts{classdbi_objects}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts{"classdbi_objects"}) 0 0 0
312 0 $opts{objects} = $opts{classdbi_objects};
313 0 delete($opts{classdbi_objects});
314 }
315  
316 0 ▶ if (exists $opts{filename} or exists $opts{filehandle}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'filename'} or exists $opts{'filehandle'}) { } 0 0 0
elsif (exists $opts{'objects'}) { } 0 0 0
Condition0%0/3
exists $opts{"filename"} or exists $opts{"filehandle"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $opts{"filename"} or exists $opts{"filehandle"}
exists $opts{"filename"} exists $opts{"filehandle"}
Truth table0%0/3
A or B
A= exists $opts{"filename"}B= exists $opts{"filehandle"}
ABresult
1X1
011
000
317 0 $self->_do_parse(%opts);
318 } elsif (exists $opts{objects}) {
319 0 $self->_do_parse_objects(%opts);
320 } else {
321 0 confess "Please provide objects or a filename/filehandle to parse\n";
322 }
323  
324 0 return $self;
325 }
326  
327 0 ▶sub _do_parse {
Subroutine _do_parse: not called CC=14 SCAR=53.5
328 0 my ($self, %opts) = @_;
329  
330 0 my @CSV_Content = ();
331 0 ▶ if (exists $opts{'filename'} and defined $opts{'filename'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $opts{'filename'} and defined $opts{'filename'}) { } 0 0 0
elsif (exists $opts{'filehandle'} and defined $opts{'filehandle'}) { } 0 0 0
Condition0%0/3
exists $opts{"filename"} and defined $opts{"filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"filename"} and defined $opts{"filename"}
exists $opts{"filename"} defined $opts{"filename"}
Truth table0%0/3
A and B
A= exists $opts{"filename"}B= defined $opts{"filename"}
ABresult
0X0
100
111
332 confess "Cannot find filename: ". $opts{'filename'}. "\n"
333 0 ▶ unless (-f $opts{'filename'});
Branch: 1 branch
BranchTrueFalseTotal
unless -f $opts{"filename"} 0 0 0
334 confess "Cannot read filename: ". $opts{'filename'}. "\n"
335 0 ▶ unless (-r $opts{'filename'});
Branch: 1 branch
BranchTrueFalseTotal
unless -r $opts{"filename"} 0 0 0
336 open(CSV, $opts{'filename'})
337 0 ▶ or confess "Failed to open filename: ". $opts{'filename'}. ': '. $!. "\n";
Branch: 1 branch
BranchTrueFalseTotal
open CSV, $opts{"filename"} or confess('Failed to open filename: ' . $opts{"filename"} . ': ' . $! . "\n") 0 0 0
338 0 ▶ while (my $line = <CSV>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline CSV)) 0 0 0
339 0 push(@CSV_Content, $self->strip_crlf($line));
340 }
341 0 close(CSV);
342 ▶ } elsif (exists $opts{'filehandle'} and defined $opts{'filehandle'}) {
Condition0%0/3
exists $opts{"filehandle"} and defined $opts{"filehandle"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts{"filehandle"} and defined $opts{"filehandle"}
exists $opts{"filehandle"} defined $opts{"filehandle"}
Truth table0%0/3
A and B
A= exists $opts{"filehandle"}B= defined $opts{"filehandle"}
ABresult
0X0
100
111
343 confess "filehandle provided is not a file handle\n"
344 0 ▶ unless (defined(fileno($opts{'filehandle'})));
Branch: 1 branch
BranchTrueFalseTotal
unless defined fileno $opts{"filehandle"} 0 0 0
345 0 my $fh = $opts{'filehandle'};
346 0 ▶ while (my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 0 0 0
347 0 push(@CSV_Content, $self->strip_crlf($line));
348 }
349 } else {
350 0 confess "Please provide a filename/filehandle to parse\n";
351 }
352  
353 0 foreach my $line (@CSV_Content) {
354 0 ▶ unless ($line and $line !~ /^([,"']|\s)+$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line and not $line =~ /^([,"']|\s)+$/) 0 0 0
Condition0%0/3
$line and not $line =~ /^([,"']|\s)+$/
!ll&&!rl&&r
000
MC/DC0%0/2
$line and not $line =~ /^([,"']|\s)+$/
$line not $line =~ /^([,"']|\s)+$/
Truth table0%0/3
A and not(B)
A= $lineB= not $line =~ /^([,"']|\s)+$/
ABresult
0X0
100
111
355 # Skip empty lines
356 0 next;
357 }
358 0 push(@{$self->{lines}}, $self->new_line(undef, { line => $line }));
359 }
360 }
361  
362 0 ▶sub _do_parse_objects {
Subroutine _do_parse_objects: not called CC=5 SCAR=34.0
363 0 my ($self, %opts) = @_;
364  
365 confess "Please specify objects as an ARRAY ref!\n"
366 0 ▶ unless (ref $opts{objects} eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $opts{"objects"} eq "ARRAY" 0 0 0
367  
368 0 foreach my $object (@{$opts{objects}}) {
369 0 my $line = $self->new_line();
370  
371 0 foreach my $field (@{$self->fields()}) {
372 0 ▶ confess ((ref $object). " does not contain method ". $field. "\n")
Branch: 1 branch
BranchTrueFalseTotal
unless $object->can($field) 0 0 0
373 unless ($object->can($field));
374  
375 0 $line->set( $field => $object->$field );
376 }
377  
378 0 push(@{$self->{lines}}, $line);
379 }
380 }
381  
382 0 ▶sub new_line {
Subroutine new_line: not called CC=9 SCAR=45.0
Pod documented
383 0 my ($self, $args, $opts) = @_;
384  
385 0 my %opts = ();
386 0 ▶ if ($opts and ref $opts eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts and ref $opts eq "HASH") 0 0 0
Condition0%0/3
$opts and ref $opts eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
$opts and ref $opts eq "HASH"
$opts ref $opts eq "HASH"
Truth table0%0/3
A and B
A= $optsB= ref $opts eq "HASH"
ABresult
0X0
100
111
387 0 %opts = %{$opts};
388 }
389  
390 0 my $line = new Class::CSV::Line(
391 fields => $self->fields(),
392 csv_xs_options => $self->csv_xs_options(),
393 %opts
394 );
395  
396 0 ▶ confess "Failed to create new line\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
397 unless ($line);
398  
399 0 ▶ if (defined $args) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args) 0 0 0
400 0 ▶ if (ref $args eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args eq 'ARRAY') { } 0 0 0
elsif (ref $args eq 'HASH') { } 0 0 0
401 0 my @dr_array = @{$args};
402 0 foreach my $field (@{$self->fields()}) {
403 0 my $value = shift @dr_array;
404 0 $line->set( $field => $value );
405 }
406 } elsif (ref $args eq 'HASH') {
407 0 foreach my $field (keys %{$args}) {
408 0 $line->set( $field => $args->{$field} );
409 }
410 } else {
411 0 confess "Need the arguments passed as either an ARRAY ref or a HASH ref!\n";
412 }
413 }
414  
415 0 return $line;
416 }
417  
418 0 ▶sub add_line {
Subroutine add_line: not called CC=3 SCAR=24.8
Pod documented
419 0 my ($self, $args) = @_;
420  
421 0 ▶ confess "Cannot call add_line without an argument!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args and $args 0 0 0
Condition0%0/3
defined $args and $args
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args and $args
defined $args $args
Truth table0%0/3
A and B
A= defined $argsB= $args
ABresult
0X0
100
111
422 unless (defined $args and $args);
423  
424 0 my $line = $self->new_line($args);
425  
426 0 push(@{$self->{lines}}, $line);
427 }
428  
429 0 ▶sub string {
Subroutine string: not called CC=2 SCAR=17.9
Pod documented
430 0 my ($self) = @_;
431  
432 0 ▶ confess "No lines to write!\n" unless (ref $self->lines() eq 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->lines eq "ARRAY" 0 0 0
433  
434 0 my @string = ();
435 0 map { push(@string, $_->string()); } @{$self->lines()};
436  
437 0 return join($self->line_separator(), @string). $self->line_separator();
438 }
439  
440 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
441 0 my ($self) = @_;
442  
443 0 print $self->string();
444 }
445  
446 0 ▶sub strip_crlf {
Subroutine strip_crlf: not called CC=1 SCAR=6.9
Pod undocumented
447 0 my ($self, $string) = @_;
448  
449 0 $string =~ s/[\n\r]+$//g;
450  
451 0 return $string;
452 }
453  
454 0 ▶sub _private_set {
Subroutine _private_set: not called CC=2 SCAR=17.9
455 0 my ($self, %items) = @_;
456  
457 0 foreach my $field (keys %items) {
458 0 $self->{$field} = $items{$field};
459 0 $self->{__fields}->{$field} = TRUE;
460 }
461 }
462  
463 1 1;
464  
465 __END__
↑ Summary