blib/lib/Finance/IIF.pm

filter: Statementstmt 57.1% 80 / 140 Branchbran 41.2% 33 / 80 Conditioncond 57.1% 8 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 88.2% 15 / 17 Podpod 100% 7 / 7 totaltotal 54.8% 146 / 266 CC 47 SCAR 42.5
CC
47
Cov
52%
_parseline
210.0
next
110.6
open
8.0
CRAP 70.4
↑ Summary
1 package Finance::IIF;
2  
3 1 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $VERSION = '0.20.01';
10 1 $VERSION = eval $VERSION;
11  
12 16 ▶sub new {
Subroutine new: called CC=6 SCAR=17.9
Pod documented
13 16 my $class = shift;
14 16 my %opt = @_;
15 16 my $self = {};
16  
17 16 ▶ $self->{debug} = $opt{debug} || 0;
Condition100%2/2
$opt{'debug'} || 0
l!l
115
MC/DC100%1/1
$opt{'debug'} || 0
$opt{'debug'}
Truth table100%2/2
A
A= $opt{'debug'}
Aresult
11
00
18 16 ▶ $self->{autodetect} = $opt{autodetect} || 0;
Condition100%2/2
$opt{'autodetect'} || 0
l!l
412
MC/DC100%1/1
$opt{'autodetect'} || 0
$opt{'autodetect'}
Truth table100%2/2
A
A= $opt{'autodetect'}
Aresult
11
00
19 16 ▶ $self->{field_separator} = $opt{field_separator} || "\t";
Condition100%2/2
$opt{'field_separator'} || "\t"
l!l
115
MC/DC100%1/1
$opt{'field_separator'} || "\t"
$opt{'field_separator'}
Truth table100%2/2
A
A= $opt{'field_separator'}
Aresult
11
00
20  
21 16 bless( $self, $class );
22  
23 16 ▶ if ( $opt{record_separator} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"record_separator"}) 1 15 16
24 1 $self->record_separator( $opt{record_separator} );
25 }
26  
27 16 ▶ if ( $opt{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"file"}) 6 10 16
28 6 $self->file( $opt{file} );
29 6 $self->open;
30 }
31 16 return $self;
32 }
33  
34 28 ▶sub file {
Subroutine file: called CC=5 SCAR=16.1
Pod documented
35 28 my $self = shift;
36 28 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 10 18 28
37 10 ▶ my @file = ( ref( $_[0] ) eq "ARRAY" ? @{ shift @_ } : (), @_ );
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 1 9 10
38 10 $self->{file} = [@file];
39 }
40 28 ▶ if ( $self->{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'file'}) { } 26 2 28
41 26 ▶ return wantarray ? @{ $self->{file} } : $self->{file}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 8 18 26
42 }
43 else {
44 2 return undef;
45 }
46 }
47  
48 11 ▶sub record_separator {
Subroutine record_separator: called CC=4 SCAR=14.1
Pod documented
49 11 my $self = shift;
50 11 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 7 11
51 4 ▶ $self->{record_separator} = $_[0] if ( $_[0] );
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] 4 0 4
52 }
53 11 ▶ return $self->{record_separator} || $/;
Condition66%2/3
$self->{'record_separator'} || $/
l!l&&r!l&&!r
830
MC/DC0%0/2
$self->{'record_separator'} || $/
$self->{'record_separator'} $/
Truth table66%2/3
A || B
A= $self->{'record_separator'}B= $/
ABresult
1X1
011
000
54 }
55  
56 26 ▶sub _filehandle {
Subroutine _filehandle: called CC=4 SCAR=13.9
57 26 my $self = shift;
58 26 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 19 26
59 7 my @args = @_;
60 7 ▶ $self->{_filehandle} = IO::File->new(@args)
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_filehandle"} = "IO::File"->new(@args) or croak("Failed to open file '$args[0]': $!") 7 0 7
61 or croak("Failed to open file '$args[0]': $!");
62 7 $self->{_linecount} = 0;
63 }
64 26 ▶ if ( !$self->{_filehandle} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_filehandle"}) 5 21 26
65 5 croak("No filehandle available");
66 }
67 21 return $self->{_filehandle};
68 }
69  
70 8 ▶sub open {
Subroutine open: called CC=8 SCAR=20.8
Pod documented
71 8 my $self = shift;
72 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 7 8
73 1 $self->file(@_);
74 }
75 8 ▶ if ( $self->file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) { } 7 1 8
76 7 $self->_filehandle( $self->file );
77 7 ▶ if ( $self->{autodetect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodetect"}) 4 3 7
78 4 ▶ if ( $self->_filehandle->seek( -2, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_filehandle->seek(-2, 2)) 3 1 4
79 3 my $buffer = "";
80 3 $self->_filehandle->read( $buffer, 2 );
81 3 ▶ if ( $buffer eq "\r\n" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($buffer eq "\r\n") { } 1 2 3
elsif ($buffer =~ /\n$/) { } 1 1 2
elsif ($buffer =~ /\r$/) { } 1 0 1
82 1 $self->record_separator("\r\n");
83 }
84 elsif ( $buffer =~ /\n$/ ) {
85 1 $self->record_separator("\n");
86 }
87 elsif ( $buffer =~ /\r$/ ) {
88 1 $self->record_separator("\r");
89 }
90 }
91 }
92 7 $self->reset();
93 }
94 else {
95 1 croak("No file specified");
96 }
97 }
98  
99 1 ▶sub next {
Subroutine next: called CC=12 SCAR=47.1
Pod documented
100 1 my $self = shift;
101 1 my %object;
102 1 my $continue = 1;
103 1 ▶ if ( $self->_filehandle->eof ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_filehandle->eof) 0 0 0
104 0 return undef;
105 }
106 0 ▶ while ( !$self->_filehandle->eof && $continue ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->_filehandle->eof and $continue) 0 0 0
Condition0%0/3
not $self->_filehandle->eof and $continue
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->_filehandle->eof and $continue
not $self->_filehandle->eof $continue
Truth table0%0/3
not(A) and B
A= not $self->_filehandle->eofB= $continue
ABresult
0X0
100
111
107 0 my $line = $self->_getline;
108 0 ▶ next if ( $line =~ /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
109 0 my @data = $self->_parseline($line);
110  
111 0 ▶ if ( $self->{debug} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} > 1) 0 0 0
112 0 warn("_getline: line($line)\n");
113 0 warn( "_parseline: data[" . scalar(@data) . "](@data)\n" );
114 }
115  
116 0 ▶ if ( $data[0] =~ /^!(.*)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data[0] =~ /^!(.*)$/) { } 0 0 0
elsif ($data[0] eq $self->{'header'}) { } 0 0 0
117 0 delete( $self->{headerfields} );
118 0 shift(@data);
119 0 $self->{header} = $1;
120 0 $self->{headerfields} = \@data;
121 }
122 elsif ( $data[0] eq $self->{header} ) {
123 0 $continue = 0;
124 0 $object{header} = shift(@data);
125 0 my $num_hdr = scalar( @{ $self->{headerfields} } );
126 0 my $num_dat = scalar(@data);
127  
128 # have seen IIF timer data where last column (USEID) was
129 # missing but QuickBooks imports the data without error
130 0 ▶ if ( $num_dat < ( $num_hdr - 1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_dat < $num_hdr - 1) { } 0 0 0
131 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 0 $self->_warning( "parse error: found $num_dat fields but"
133 . " expected $num_hdr." );
134 warn(
135 "error info: [header,data] "
136 . join(
137 ' ',
138 map( "$_" . '['
139 0 . $self->{headerfields}->[$_] . ','
140 . $data[$_] . ']',
141 0 .. ( $num_hdr - 1 ) )
142 )
143 );
144 }
145 else {
146 0 ▶ for ( my $i = 0 ; $i <= $#{ $self->{headerfields} } ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"headerfields"};}) 0 0 0
147 0 ▶ my $val = defined( $data[$i] ) ? $data[$i] : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $data[$i] ? : 0 0 0
148 0 $object{ $self->{headerfields}[$i] } = $val;
149 }
150 }
151 }
152 else {
153 0 $self->_warning("unable to parse line '$_'");
154 }
155 }
156  
157 0 ▶ if ($continue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($continue) { } 0 0 0
158 0 return undef;
159 }
160 else {
161 0 return \%object;
162 }
163 }
164  
165 0 ▶sub _parseline {
Subroutine _parseline: not called CC=14 SCAR=53.5
166 0 my $self = shift;
167 0 my $line = shift;
168 0 ▶ my $sep = $self->{field_separator} || "\t";
Condition0%0/2
$self->{'field_separator'} || "\t"
l!l
00
MC/DC0%0/1
$self->{'field_separator'} || "\t"
$self->{'field_separator'}
Truth table0%0/2
A
A= $self->{'field_separator'}
Aresult
11
00
169 0 my @data;
170 0 ▶ while ( defined $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
171 0 ▶ if ( $line =~ /^"(.*?)(?:[^\\]["])[$sep](.*)/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($line =~ /^"(.*?)(?:[^\\]["])[$sep](.*)/) { } 0 0 0
elsif ($line =~ /^([^$sep]+)[$sep](.*)/) { } 0 0 0
elsif ($line =~ /^[$sep](.*)/) { } 0 0 0
elsif ($line =~ /^"(.*?)(?:[^\\]["])$/) { } 0 0 0
elsif ($line =~ /^(.+)$/) { } 0 0 0
172 0 ▶ warn("parse1: data($1) line($2)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
173 0 $line = $2;
174 0 push( @data, $1 );
175 }
176 elsif ( $line =~ /^([^$sep]+)[$sep](.*)/ ) {
177 0 ▶ warn("parse2: data($1) line($2)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
178 0 $line = $2;
179 0 push( @data, $1 );
180 }
181 elsif ( $line =~ /^[$sep](.*)/ ) {
182 0 ▶ warn("parse3: data() line($1)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
183 0 $line = $1;
184 0 push( @data, "" );
185 }
186 elsif ( $line =~ /^"(.*?)(?:[^\\]["])$/ ) {
187 0 ▶ warn("parse4: data($1) line()\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
188 0 $line = undef;
189 0 push( @data, $1 );
190 }
191 elsif ( $line =~ /^(.+)$/ ) {
192 0 ▶ warn("parse5: data($1) line()\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
193 0 $line = undef;
194 0 push( @data, $1 );
195 }
196 else {
197 0 ▶ warn("parse6: data() line($line)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
198 0 $line = undef;
199 0 push( @data, "" );
200 }
201 }
202 0 return @data;
203 }
204  
205 1 ▶sub _getline {
Subroutine _getline: called CC=1 SCAR=1.2
206 1 my $self = shift;
207 1 local $/ = $self->record_separator;
208 1 my $line = $self->_filehandle->getline;
209 0 chomp($line);
210 0 $self->{_linecount}++;
211 0 return $line;
212 }
213  
214 0 ▶sub _warning {
Subroutine _warning: not called CC=1 SCAR=6.9
215 0 my $self = shift;
216 0 my $message = shift;
217 carp( $message
218 . " in file '"
219 . $self->file
220 . "' line "
221 0 . $self->{_linecount} );
222 }
223  
224 8 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
225 8 my $self = shift;
226 8 $self->_filehandle->seek( 0, 0 );
227 }
228  
229 1 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
230 1 my $self = shift;
231 1 $self->_filehandle->close;
232 }
233  
234 1 1;
235  
236 __END__
↑ Summary