Stream/XLS.pm

filter: Statementstmt 91.7% 89 / 97 Branchbran 81.2% 26 / 32 Conditioncond 75.0% 15 / 20 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 77.7% 14 / 18 Podpod 0.0% 0 / 1 totaltotal 83.3% 150 / 180 CC 25 SCAR 16.8
CC
25
Cov
87%
set_next_row
9.0
next_row
8.1
sheet
6.0
CRAP 5.4
« Stream.pm ↑ Summary
1 package Spreadsheet::ParseExcel::Stream::XLS;
2  
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 8 ▶use Spreadsheet::ParseExcel;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Scalar::Util qw(weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use Coro;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 8 our $VERSION = '0.11';
11  
12 8 ▶sub new {
Subroutine new: called CC=5 SCAR=16.9
Pod undocumented
13 8 my ($class, $file, $opts) = @_;
14  
15 8 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
80
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
16 8 my @password;
17 8 ▶ if ( defined($opts->{Password}) && length($opts->{Password}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts->{"Password"} and length $opts->{"Password"}) 0 8 8
Condition33%1/3
defined $opts->{"Password"} and length $opts->{"Password"}
!ll&&!rl&&r
800
MC/DC0%0/2
defined $opts->{"Password"} and length $opts->{"Password"}
defined $opts->{"Password"} length $opts->{"Password"}
Truth table33%1/3
A and B
A= defined $opts->{"Password"}B= length $opts->{"Password"}
ABresult
0X0
100
111
18 0 @password = ( Password => $opts->{Password} );
19 }
20  
21 8 my $main = Coro::State->new();
22 8 my ($xls,$parser);
23  
24 8 my ($wb, $idx, $row, $col, $cell);
25 my $tmp = my $handler = sub {
26 ($wb, $idx, $row, $col, $cell) = @_;
27 $parser->transfer($main);
28 8 };
29  
30 my $tmp_p = $parser = Coro::State->new(sub {
31 $xls->Parse($file);
32 # Flag the generator that we're done
33 undef $xls;
34 # If we don't transfer back when done parsing,
35 # it's an implicit program exit (oops!)
36 $parser->transfer($main)
37 8 });
38 8 weaken($parser);
39  
40 8 $xls = Spreadsheet::ParseExcel->new(
41 CellHandler => $handler,
42 NotSetCell => 1,
43 @password,
44 );
45  
46 # Returns the next cell of the spreadsheet
47 my $generator = sub {
48  
49 # Just in case we ask for the next cell when we're already done
50 return unless $xls;
51  
52 $main->transfer($parser);
53 return [ $wb, $idx, $row, $col, $cell ] if $xls;
54  
55 # We're done with these threads
56 $main->cancel();
57 $parser->cancel();
58 return;
59 8 };
60 8 my $nxt_cell = $generator->();
61  
62 my $self = bless {
63 # Save a reference to the parser so it doesn't disappear
64 # until the object is destroyed.
65 PARSER => $tmp_p,
66 NEXT_CELL => $nxt_cell,
67 SUB => $generator,
68 TRIM => $opts->{TrimEmpty},
69 8 NEW_WS => 0,
70 }, 'Spreadsheet::ParseExcel::Stream::Sheet';
71 0 ▶ $self->bind_columns( @{$opts->{BindColumns}} ) if $opts->{BindColumns};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"BindColumns"} 0 8 8
72 8 return $self;
73 }
74  
75 package Spreadsheet::ParseExcel::Stream::Sheet;
76  
77 24 ▶sub sheet {
Subroutine sheet: called CC=6 SCAR=18.0
78 24 my $self = shift;
79 24 ▶ return unless $self->{NEXT_CELL};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"NEXT_CELL"} 8 16 24
80  
81 # NEW_WS:
82 # undef - in the middle of a sheet.
83 # 0 - Hit end of previous sheet and fetched next row.
84 # 1 - At end of sheet but not fetched next row yet.
85 # 0 and 1 will be treated as same, just undef NEW_WS and return.
86 # If undef, advance to the next sheet.
87 16 ▶ if ( ! defined $self->{NEW_WS} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"NEW_WS"}) 1 15 16
88 # Advance to the next sheet
89 1 my $curr_cell = $self->{NEXT_CELL};
90 1 my $curr_sheet = $curr_cell->[1];
91 1 my $f = $self->{SUB};
92 1 my $nxt_cell = $f->();
93 1 ▶ while ( $nxt_cell && $nxt_cell->[1] == $curr_sheet ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nxt_cell and $nxt_cell->[1] == $curr_sheet) 5 1 6
Condition66%2/3
$nxt_cell and $nxt_cell->[1] == $curr_sheet
!ll&&!rl&&r
015
MC/DC50%1/2
$nxt_cell and $nxt_cell->[1] == $curr_sheet
$nxt_cell $nxt_cell->[1] == $curr_sheet
Truth table66%2/3
A and B
A= $nxt_cellB= $nxt_cell->[1] == $curr_sheet
ABresult
0X0
100
111
94 5 $nxt_cell = $f->();
95 }
96 1 ▶ $self->{NEXT_CELL} = $nxt_cell or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"NEXT_CELL"} = $nxt_cell or return 1 0 1
97 }
98 16 $self->{NEW_WS} = undef;
99 16 return $self;
100 }
101  
102 0 ▶sub workbook {
Subroutine workbook: not called CC=1 SCAR=6.9
103 0 my $self = shift;
104 0 my $row = $self->{NEXT_CELL};
105 0 return $row->[0];
106 }
107  
108 15 ▶sub worksheet {
Subroutine worksheet: called CC=1 SCAR=0.0
109 15 my $self = shift;
110 15 my $row = $self->{NEXT_CELL};
111 15 my $wb = $row->[0];
112 15 return $wb->worksheet($row->[1]);
113 }
114  
115 15 ▶sub name {
Subroutine name: called CC=1 SCAR=0.0
116 15 my $self = shift;
117 15 return $self->worksheet()->{Name};
118 }
119  
120 45 ▶sub set_next_row {
Subroutine set_next_row: called CC=9 SCAR=22.0
121 45 my ($self, $current) = @_;
122 45 ▶ return $self->{CURR_ROW} if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 0 45 45
123  
124 45 ▶ return $self->{NEW_WS} = 0 if $self->{NEW_WS};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"NEW_WS"} 15 30 45
125  
126 # Save original cell so we can detect change in worksheet
127 30 my $curr_cell = $self->{NEXT_CELL};
128 30 my $f = $self->{SUB};
129  
130 # Initialize row with first cell
131 30 my @row = ();
132 30 my $nxt_cell = $f->();
133  
134 my $min_col = $self->{TRIM}
135 30 ▶ ? ( $curr_cell->[0]->worksheet( $curr_cell->[1] )->col_range)[0]
Branch: 1 branch
BranchTrueFalseTotal
$self->{'TRIM'} ? : 10 20 30
136 : 0;
137 30 $row[ $curr_cell->[3] - $min_col ] = $curr_cell;
138  
139 # Collect current row on current worksheet
140 30 my ( $curr_sheet, $curr_row ) = @$curr_cell[1,2];
141 30 ▶ while ( $nxt_cell && $nxt_cell->[1] == $curr_sheet && $nxt_cell->[2] == $curr_row ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nxt_cell and $nxt_cell->[1] == $curr_sheet and $nxt_cell->[2] == $curr_row) 60 30 90
Condition100%6/6
$nxt_cell and $nxt_cell->[1] == $curr_sheet and $nxt_cell->[2] == $curr_row
!ll&&!rl&&r
151560
$nxt_cell and $nxt_cell->[1] == $curr_sheet
!ll&&!rl&&r
8775
MC/DC100%3/3
$nxt_cell and $nxt_cell->[1] == $curr_sheet and $nxt_cell->[2] == $curr_row
$nxt_cell $nxt_cell->[1] == $curr_sheet $nxt_cell->[2] == $curr_row
Truth table100%4/4
A and B and C
A= $nxt_cellB= $nxt_cell->[1] == $curr_sheetC= $nxt_cell->[2] == $curr_row
ABCresult
0XX0
10X0
1100
1111
142 60 $curr_cell = $nxt_cell;
143 60 $row[ $curr_cell->[3] - $min_col ] = $curr_cell;
144 60 $nxt_cell = $f->();
145 }
146 30 $self->{NEXT_CELL} = $nxt_cell;
147 30 ▶ $self->{NEW_WS}++ if !$nxt_cell || $curr_sheet != $nxt_cell->[1];
Branch: 1 branch
BranchTrueFalseTotal
if not $nxt_cell or $curr_sheet != $nxt_cell->[1] 15 15 30
Condition100%3/3
not $nxt_cell or $curr_sheet != $nxt_cell->[1]
l!l&&r!l&&!r
8715
MC/DC100%2/2
not $nxt_cell or $curr_sheet != $nxt_cell->[1]
not $nxt_cell $curr_sheet != $nxt_cell->[1]
Truth table100%3/3
not(A) or B
A= not $nxt_cellB= $curr_sheet != $nxt_cell->[1]
ABresult
1X1
011
000
148 30 $self->{CURR_ROW} = \@row;
149 }
150  
151 45 ▶sub next_row {
Subroutine next_row: called CC=8 SCAR=20.9
152 45 my ($self, $current, $f) = @_;
153 45 ▶ $f ||= sub {$_->[4]};
Condition66%2/3
$f ||= sub { $_->[4]; }
l!l&&r!l&&!r
3960
MC/DC0%0/2
$f ||= sub { $_->[4]; }
$f sub { $_->[4]; }
Truth table66%2/3
A ||= B
A= $fB= sub { $_->[4]; }
ABresult
1X1
011
000
154 45 ▶ unless ($current) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current) 45 0 45
155 45 my $row = $self->set_next_row();
156 45 ▶ return unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 15 30 45
157 30 ▶ if ( $self->{BIND} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"BIND"}) 4 26 30
158 4 ▶ my @curr_row = map { defined $_ ? $f->() : $_ } @{$self->{CURR_ROW}};
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 12 0 12
159 4 $$_ = shift @curr_row for @{$self->{BIND}};
160 4 return 1;
161 }
162 }
163 26 ▶ return [ map { defined $_ ? $f->() : $_ } @{$self->{CURR_ROW}} ];
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 78 14 92
164 }
165  
166 33 ▶sub row {
Subroutine row: called CC=1 SCAR=0.4
167 33 my ($self,$current) = @_;
168 0 ▶ return $self->next_row($current, sub {$_->[4]->value});
Subroutine __ANON__: not called CC=1 SCAR=1.2
169 }
170  
171 6 ▶sub unformatted {
Subroutine unformatted: called CC=1 SCAR=0.4
172 6 my ($self, $current) = @_;
173 0 ▶ return $self->next_row($current, sub {$_->[4]->unformatted});
Subroutine __ANON__: not called CC=1 SCAR=1.2
174 }
175  
176 1 ▶sub bind_columns {
Subroutine bind_columns: called CC=1 SCAR=0.0
177 1 my $self = shift;
178 1 $self->{BIND} = [ @_ ];
179 }
180  
181 0 ▶sub unbind_columns { delete $_[0]->{BIND} }
Subroutine unbind_columns: not called CC=1 SCAR=6.9
182  
183 8 1;
184  
185 __END__
« Stream.pm ↑ Summary