blib/lib/Finance/IIF.pm

filter: Statementstmt 54.0% 81 / 150 Branchbran 38.3% 33 / 86 Conditioncond 57.1% 8 / 14 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 88.2% 15 / 17 Podpod 100% 7 / 7 totaltotal 52.1% 147 / 282 CC 50 SCAR 44.8
CC
50
Cov
49%
_parseline
210.0
next
182.0
open
8.0
CRAP 88.5
↑ 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 qw(carp croak);
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.21_02';
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 7 $self->{_filehandle}->binmode();
64 }
65 26 ▶ if ( !$self->{_filehandle} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_filehandle"}) 5 21 26
66 5 croak("No filehandle available");
67 }
68 21 return $self->{_filehandle};
69 }
70  
71 8 ▶sub open {
Subroutine open: called CC=8 SCAR=20.8
Pod documented
72 8 my $self = shift;
73 8 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 7 8
74 1 $self->file(@_);
75 }
76 8 ▶ if ( $self->file ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->file) { } 7 1 8
77 7 $self->_filehandle( $self->file );
78 7 ▶ if ( $self->{autodetect} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"autodetect"}) 4 3 7
79 4 ▶ if ( $self->_filehandle->seek( -2, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_filehandle->seek(-2, 2)) 3 1 4
80 3 my $buffer = "";
81 3 $self->_filehandle->read( $buffer, 2 );
82 3 ▶ if ( $buffer eq "\015\012" ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($buffer eq "\r\n") { } 1 2 3
elsif ($buffer =~ /\012$/) { } 1 1 2
elsif ($buffer =~ /\015$/) { } 1 0 1
83 1 $self->record_separator("\015\012");
84 }
85 elsif ( $buffer =~ /\012$/ ) {
86 1 $self->record_separator("\012");
87 }
88 elsif ( $buffer =~ /\015$/ ) {
89 1 $self->record_separator("\015");
90 }
91 }
92 }
93 7 $self->reset();
94 }
95 else {
96 1 croak("No file specified");
97 }
98 }
99  
100 1 ▶sub next {
Subroutine next: called CC=15 SCAR=52.0
Pod documented
101 1 my $self = shift;
102 1 my %object;
103 1 my $continue = 1;
104 1 ▶ if ( $self->_filehandle->eof ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_filehandle->eof) 0 0 0
105 0 return undef;
106 }
107 0 my $line;
108 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
109  
110 # IIF files sometimes contain embedded record_separators so if
111 # the previous line didn't have enough fields keep adding to it
112 0 ▶ if ( $self->{_need_more} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_need_more'}) { } 0 0 0
113 0 ▶ if ( $self->{_need_more} > 8 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_need_more'} > 8) { } 0 0 0
114 0 $self->_warning("Giving up trying to get next record");
115 0 delete $self->{_need_more};
116 0 $continue = 0;
117 }
118 else {
119 0 $line .= $self->record_separator . $self->_getline;
120 }
121 }
122 else {
123 0 $line = $self->_getline;
124 0 ▶ next if ( $line =~ /^\s*$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*$/ 0 0 0
125 }
126  
127 0 my @data = $self->_parseline($line);
128  
129 0 ▶ if ( $self->{debug} > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"} > 1) 0 0 0
130 0 warn("_getline: line($line)\n");
131 0 warn( "_parseline: data[" . scalar(@data) . "](@data)\n" );
132 }
133  
134 0 ▶ if ( $data[0] =~ /^!(.*)$/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data[0] =~ /^!(.*)$/) { } 0 0 0
elsif ($data[0] eq $self->{'header'}) { } 0 0 0
135 0 delete( $self->{headerfields} );
136 0 shift(@data);
137 0 $self->{header} = $1;
138 0 $self->{headerfields} = \@data;
139 }
140 elsif ( $data[0] eq $self->{header} ) {
141 0 $object{header} = shift(@data);
142 0 my $num_hdr = scalar( @{ $self->{headerfields} } );
143 0 my $num_dat = scalar(@data);
144  
145 # have seen IIF timer data where last column (USEID) was
146 # missing but QuickBooks imports the data without error
147 0 ▶ if ( $num_dat < ( $num_hdr - 1 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($num_dat < $num_hdr - 1) { } 0 0 0
elsif ($num_dat > $num_hdr) { } 0 0 0
148 0 $self->{_need_more}++;
149 }
150 elsif ( $num_dat > $num_hdr ) {
151 0 $self->{_need_more} = $continue = 0;
152 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
153 0 $self->_warning( "parse error: found $num_dat fields but"
154 . " expected $num_hdr." );
155 warn(
156 "error info: [header,data] "
157 . join(
158 ' ',
159 map( "$_" . '['
160 0 . $self->{headerfields}->[$_] . ','
161 . $data[$_] . ']',
162 0 .. ( $num_hdr - 1 ) )
163 )
164 );
165 }
166 else {
167 0 $self->{_need_more} = $continue = 0;
168 0 ▶ for ( my $i = 0 ; $i <= $#{ $self->{headerfields} } ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#{$$self{"headerfields"};}) 0 0 0
169 0 ▶ my $val = defined( $data[$i] ) ? $data[$i] : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $data[$i] ? : 0 0 0
170 0 $object{ $self->{headerfields}[$i] } = $val;
171 }
172 }
173 }
174 else {
175 0 $self->_warning("unable to parse line '$_'");
176 }
177 }
178  
179 0 ▶ if ($continue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($continue) { } 0 0 0
180 0 return undef;
181 }
182 else {
183 0 return \%object;
184 }
185 }
186  
187 0 ▶sub _parseline {
Subroutine _parseline: not called CC=14 SCAR=53.5
188 0 my $self = shift;
189 0 my $line = shift;
190 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
191 0 my @data;
192 0 ▶ while ( defined $line ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $line) 0 0 0
193 0 ▶ if ( $line =~ /^"(.*?)(?:[^\\]["])[$sep](.*)/s ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($line =~ /^"(.*?)(?:[^\\]["])[$sep](.*)/s) { } 0 0 0
elsif ($line =~ /^([^$sep]+)[$sep](.*)/s) { } 0 0 0
elsif ($line =~ /^[$sep](.*)/s) { } 0 0 0
elsif ($line =~ /^"(.*?)(?:[^\\]["])$/s) { } 0 0 0
elsif ($line =~ /^(.+)$/s) { } 0 0 0
194 0 ▶ warn("parse1: data($1) line($2)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
195 0 $line = $2;
196 0 push( @data, $1 );
197 }
198 elsif ( $line =~ /^([^$sep]+)[$sep](.*)/s ) {
199 0 ▶ warn("parse2: data($1) line($2)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
200 0 $line = $2;
201 0 push( @data, $1 );
202 }
203 elsif ( $line =~ /^[$sep](.*)/s ) {
204 0 ▶ warn("parse3: data() line($1)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
205 0 $line = $1;
206 0 push( @data, "" );
207 }
208 elsif ( $line =~ /^"(.*?)(?:[^\\]["])$/s ) {
209 0 ▶ warn("parse4: data($1) line()\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
210 0 $line = undef;
211 0 push( @data, $1 );
212 }
213 elsif ( $line =~ /^(.+)$/s ) {
214 0 ▶ warn("parse5: data($1) line()\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
215 0 $line = undef;
216 0 push( @data, $1 );
217 }
218 else {
219 0 ▶ warn("parse6: data() line($line)\n") if ( $self->{debug} > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} > 2 0 0 0
220 0 $line = undef;
221 0 push( @data, "" );
222 }
223 }
224 0 return @data;
225 }
226  
227 1 ▶sub _getline {
Subroutine _getline: called CC=1 SCAR=1.2
228 1 my $self = shift;
229 1 local $/ = $self->record_separator;
230 1 my $line = $self->_filehandle->getline;
231 0 $self->{_linecount}++;
232 0 chomp($line);
233 0 return $line;
234 }
235  
236 0 ▶sub _warning {
Subroutine _warning: not called CC=1 SCAR=6.9
237 0 my $self = shift;
238 0 my $message = shift;
239 carp( $message
240 . " in file '"
241 . $self->file
242 . "' line "
243 0 . $self->{_linecount} );
244 }
245  
246 8 ▶sub reset {
Subroutine reset: called CC=1 SCAR=0.0
Pod documented
247 8 my $self = shift;
248 8 $self->_filehandle->seek( 0, 0 );
249 }
250  
251 1 ▶sub close {
Subroutine close: called CC=1 SCAR=0.0
Pod documented
252 1 my $self = shift;
253 1 $self->_filehandle->close;
254 }
255  
256 1 1;
257  
258 __END__
↑ Summary