Edit.pm

filter: Statementstmt 84.6% 1397 / 1651 Branchbran 62.4% 457 / 732 Conditioncond 54.6% 219 / 401 MC/DCmcdc 25.7% 55 / 214 Subroutinesub 81.2% 191 / 235 Podpod 79.6% 51 / 64 totaltotal 71.8% 2370 / 3297 CC 586 SCAR 30.0
CC
586
Cov
74%
write_csv
127.2
write_spreadsheet
110.0
__fmt_uqlistwithqw
72.0
CRAP 20.0
« Edit/Log.pm ↑ Summary Edit/Preload.pm »
1 # License: http://creativecommons.org/publicdomain/zero/1.0/
2 # (CC0 or Public Domain). To the extent possible under law, the author,
3 # Jim Avera (email jim.avera at gmail) has waived all copyright and
4 # related or neighboring rights to this document.
5 # Attribution is requested but not required.
6  
7 # Pod documentation is below (use perldoc to view)
8  
9 11 ▶use 5.18.0; # lexical subs with bug we have worked around
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use strict; use warnings FATAL => 'all'; use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶no warnings qw(experimental::lexical_subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use feature qw(state lexical_subs current_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 package Spreadsheet::Edit;
15  
16 # Allow "use <thismodule> <someversion>;" in development sandbox to not bomb
17 11 ▶{ no strict 'refs'; ${__PACKAGE__."::VER"."SION"} = 1999.999; }
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 11 our $VERSION = '1001.005'; # VERSION from Dist::Zilla::Plugin::OurPkgVersion
20 11 our $DATE = '2026-11-11'; # DATE from Dist::Zilla::Plugin::OurDate
21  
22 # FIXME: cmd_nesting does nothing except prefix >s to log messages.
23 # Shouldn't it skip that many "public" call frames???
24  
25 # TODO FIXME: Integrate with Spreadsheet::Read and provide a formatting API
26  
27 # TODO: Allow & support undef cell values (see Text::CSV_XS), used to
28 # represent "NULL" when interfacing with database systems.
29 # OTOH, this conflicts with failed optional alias keys
30  
31 # TODO: Add some way to exit an apply() early, e.g. return value?
32 # or maybe provide an abort_apply(resultval) function
33 # which throws an exception we can catch in apply?
34  
35 # TODO: Use Tie::File to avoid storing entire sheet in memory
36 # (requires seekable, so must depend on OpenAsCsv
37 # copying "-" to a temp file if it is not a plain file).
38  
39 #########################################
40 # sub NAMING CONVENTION:
41 # sub public_method_or_function
42 # sub _internal_method
43 # sub __internal_function
44 #########################################
45  
46 # If these globals are *defined* then the corresponding
47 # (downcased) options default accordingly when new sheets are created.
48 11 our ($Debug, $Verbose, $Silent);
49  
50 ############################## Exporting stuff ############################
51 #
52 11 ▶use Exporter::Tiny 1.001_000 (); # just to require version with non-sub generator support
Subroutine BEGIN: called CC=1 SCAR=0.0
53 11 ▶use parent "Exporter::Tiny"; # make us be a derived class
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 11 require mro; # makes next::can available
56  
57 23 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
58 # copied from List::Util
59 # (RT88848) Touch the caller's $a and $b, to avoid the warning of
60 # 'Name "main::a" used only once: possible typo'
61 23 my $pkg = caller;
62 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 23 ${"${pkg}::a"} = ${"${pkg}::a"};
64 23 ${"${pkg}::b"} = ${"${pkg}::b"};
65  
66 23 my $this = $_[0];
67 23 goto &{ $this->next::can }; # see 'perldoc mro'
68 #goto &maybe::next::method ; ???
69 }
70  
71 11 ▶use Symbol qw/gensym/;
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 11 our @EXPORT = qw(
74 alias apply apply_all apply_exceptrx apply_torx attributes
75 spectocx data_source delete_col delete_cols delete_row delete_rows
76 first_data_rx transpose join_cols join_cols_sep last_data_rx move_col
77 move_cols insert_col insert_cols insert_row insert_rows new_sheet only_cols
78 options read_spreadsheet rename_cols reverse_cols
79 sheet sheetname sort_rows sort_indicies
80 split_col tie_column_vars title2ident title_row
81 title_rx unalias write_csv write_spreadsheet );
82  
83 11 my @stdvars = qw( $title_rx $first_data_rx $last_data_rx $num_cols
84 @rows @linenums @meta_info %colx %colx_desc $title_row
85 $rx $linenum @crow %crow );
86  
87 11 our @EXPORT_OK = (@stdvars, qw/logmsg cx2let let2cx fmt_sheet/);
88  
89 11 our %EXPORT_TAGS = (
90 STDVARS => [@stdvars],
91 FUNCS => [@EXPORT],
92 default => [':FUNCS'],
93 DEFAULT => [':default'],
94 #all => [qw(:STDVARS :FUNCS cx2let let2cx logmsg)],
95 all => [qw(:STDVARS :FUNCS)],
96 );
97  
98 # Although the magic globals $row, $rx and friends are "imported" by 'use',
99 # they can not simply alias variables in Spreadsheet::Edit becuase they
100 # need to be tied with parameters specific to the user's package
101 # so that each package has its own 'current sheet'.
102 #
103 # To accomplish this, we use Exporter:Tiny's "generator" mechanism to
104 # create a new, unique variables tied appropriately for each import.
105 # See perldoc Exporter::Tiny::Manual::QuickStart .
106 #
107 # If "methods" named _generate_SUBNAME, _generateScalar_SCALARNAME,
108 # _generateArray_ARRAYNAME or _generateHash_HASHNAME exist in the
109 # exporting package (that's us), then they are called to obtain
110 # a ref to an object to export as SUBNAME, SCALARNAME, ARRAYNAME or HASHNAME.
111 #
112 # For example _generateScalar_rx() is called to get a ref to an $rx variable.
113  
114 136 ▶sub __gen_x {
Subroutine __gen_x: called CC=4 SCAR=13.9
115 136 my ($myclass, $sigilname, $args, $globals, # supplied by Exporter::Tiny
116 $Type, $helpersub, @args) = @_;
117 136 ▶ my $sigl = $Type eq 'Hash' ? '%' : $Type eq 'Array' ? '@' : '$';
Branch: 2 branches
BranchTrueFalseTotal
$Type eq 'Hash' ? : 23 113 136
$Type eq 'Array' ? : 35 78 113
118 136 my $tieclassname = 'Tie::Indirect::'.$Type;
119 136 my $ref = *{gensym()}{uc($Type)}; # e.g. "ARARY"
120 # e.g. tie $$ref, 'Tie::Indirect::Scalar', \&_scal_tiehelper, ...
121 136 eval "tie ${sigl}\$ref, \$tieclassname, \$helpersub,
122 \$globals->{into}, \$sigilname, \@args";
123 136 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 136 136
124 136 $ref
125 }
126 45 ▶sub __gen_scalar {
Subroutine __gen_scalar: called CC=1 SCAR=0.0
127 45 my ($myclass, $sigilname, $args, $globals,
128 $canon_ident, $onlyinapply) = @_;
129 45 __gen_x($myclass, $sigilname, $args, $globals,
130 "Scalar", \&Spreadsheet::Edit::_scal_tiehelper,
131 $canon_ident, $onlyinapply);
132 }
133 12 ▶sub _generateScalar_num_cols { __gen_scalar(@_, "num_cols") }
Subroutine _generateScalar_num_cols: called CC=1 SCAR=0.0
134 11 ▶sub _generateScalar_first_data_rx { __gen_scalar(@_, "first_data_rx") }
Subroutine _generateScalar_first_data_rx: called CC=1 SCAR=0.0
135 11 ▶sub _generateScalar_last_data_rx { __gen_scalar(@_, "last_data_rx") }
Subroutine _generateScalar_last_data_rx: called CC=1 SCAR=0.0
136 11 ▶sub _generateScalar_rx { __gen_scalar(@_, "current_rx", 1) }
Subroutine _generateScalar_rx: called CC=1 SCAR=0.0
137  
138 #sub _generateScalar_title_rx { __gen_scalar(@_, "title_rx") }
139 11 ▶sub _generateScalar_title_rx {
Subroutine _generateScalar_title_rx: called CC=1 SCAR=0.0
140 __gen_x(@_, "Scalar",
141 1131 ▶ sub{
Subroutine __ANON__: called CC=1 SCAR=0.0
142 1131 my ($mutating, $pkg, $uvar, $onlyinapply)=@_;
143 1131 my $sheet = &Spreadsheet::Edit::__getsheet(@_);
144 \$$sheet->{title_rx}
145 11 }, 0 # onlyinapply
146 )
147 }
148  
149 22 ▶sub __gen_aryelem {
Subroutine __gen_aryelem: called CC=1 SCAR=0.0
150 22 my ($myclass, $sigilname, $args, $globals,
151 $index_ident, $array_ident, $onlyinapply) = @_;
152 # N.B. _aryelem_tiehelper has special logic for 'current_rx' and 'title_rx'
153 22 __gen_x($myclass, $sigilname, $args, $globals,
154 "Scalar", \&Spreadsheet::Edit::_aryelem_tiehelper,
155 $index_ident, $array_ident, $onlyinapply);
156 }
157 11 ▶sub _generateScalar_title_row { __gen_aryelem(@_, "title_rx", "rows") }
Subroutine _generateScalar_title_row: called CC=1 SCAR=0.0
158 11 ▶sub _generateScalar_linenum { __gen_aryelem(@_, "current_rx", "linenums", 1) }
Subroutine _generateScalar_linenum: called CC=1 SCAR=0.0
159  
160 23 ▶sub __gen_hash {
Subroutine __gen_hash: called CC=1 SCAR=0.0
161 23 my ($myclass, $sigilname, $args, $globals,
162 $field_ident, $onlyinapply) = @_;
163 23 __gen_x($myclass, $sigilname, $args, $globals,
164 "Hash", \&Spreadsheet::Edit::_refval_tiehelper,
165 $field_ident, $onlyinapply, 0); # mutable => 0
166 }
167 12 ▶sub _generateHash_colx { __gen_hash(@_, "colx", 0) }
Subroutine _generateHash_colx: called CC=1 SCAR=0.0
168 11 ▶sub _generateHash_colx_desc { __gen_hash(@_, "colx_desc", 0) }
Subroutine _generateHash_colx_desc: called CC=1 SCAR=0.0
169  
170 35 ▶sub __gen_array{
Subroutine __gen_array: called CC=1 SCAR=0.0
171 35 my ($myclass, $sigilname, $args, $globals,
172 $field_ident, $onlyinapply, $mutable) = @_;
173 35 __gen_x($myclass, $sigilname, $args, $globals,
174 "Array", \&Spreadsheet::Edit::_refval_tiehelper,
175 $field_ident, $onlyinapply, $mutable);
176 }
177 12 ▶sub _generateArray_rows { __gen_array(@_, "rows", 0, 1) }
Subroutine _generateArray_rows: called CC=1 SCAR=0.0
178  
179 # Currently @linenums is not mutable but maybe it should be?
180 12 ▶sub _generateArray_linenums { __gen_array(@_, "linenums", 0) }
Subroutine _generateArray_linenums: called CC=1 SCAR=0.0
181  
182 ## FIXME: is meta_info still valid?
183 11 ▶sub _generateArray_meta_info { __gen_array(@_, "meta_info", 0) }
Subroutine _generateArray_meta_info: called CC=1 SCAR=0.0
184  
185 6826 ▶sub __get_currentrow {
Subroutine __get_currentrow: called CC=1 SCAR=0.0
186 6826 my ($mutating, $pkg, $uvar) = @_;
187 # Ignore mutating, as it applies to the element not the container
188 6826 my $sheet = &Spreadsheet::Edit::__getsheet(0, $pkg, $uvar, 1);
189 # Returns the dual-typed ref (Magicrow) for the current row
190 6816 $$sheet->{rows}->[$$sheet->{current_rx}]
191 }
192 12 ▶sub _generateArray_crow { # @crow aliases the current row during apply
Subroutine _generateArray_crow: called CC=1 SCAR=0.0
193 12 my ($myclass, $sigilname, $args, $globals) = @_;
194 12 my $aref = *{gensym()}{ARRAY};
195 tie @$aref, 'Tie::Indirect::Array',
196 12 \&__get_currentrow, $globals->{into}, $sigilname ;
197 12 $aref
198 }
199 12 ▶sub _generateHash_crow { # %crow indexes cells in the current row during apply
Subroutine _generateHash_crow: called CC=1 SCAR=0.0
200 12 my ($myclass, $sigilname, $args, $globals) = @_;
201 12 my $href = *{gensym()}{HASH};
202 tie %$href, 'Tie::Indirect::Hash',
203 12 \&__get_currentrow, $globals->{into}, $sigilname ;
204 12 $href
205 }
206 #
207 ########################### End of Exporting stuff ##########################
208  
209 11 ▶use Spreadsheet::Edit::Log qw/log_call fmt_call log_methcall fmt_methcall oops :btw/;
Subroutine BEGIN: called CC=1 SCAR=0.0
210 11 our %SpreadsheetEdit_Log_Options = ( subst_pkg => "SE" );
211  
212 11 ▶use Data::Dumper ();
Subroutine BEGIN: called CC=1 SCAR=0.0
213 11 ▶use Data::Dumper::Interp 7.003 qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
214  
215 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
216 11 our @CARP_NOT = qw(
217 Spreadsheet::Edit::RowsTie
218 Spreadsheet::Edit::IO
219 Tie::Indirect::Array Tie::Indirect::Hash
220 Tie::Indirect::Scalar
221 Spreadsheet::Edit::Magicrow;
222 );
223  
224 11 ▶use Scalar::Util qw(looks_like_number openhandle reftype refaddr blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
225 11 ▶use List::Util qw(min max sum0 first any all pairs pairgrep);
Subroutine BEGIN: called CC=1 SCAR=0.0
226 11 ▶use Path::Tiny 0.146 qw/path/;
Subroutine BEGIN: called CC=1 SCAR=0.0
227 #use File::Temp qw(tempfile tempdir);
228 11 ▶use File::Basename qw(basename dirname fileparse);
Subroutine BEGIN: called CC=1 SCAR=0.0
229 11 ▶use File::Find ();
Subroutine BEGIN: called CC=1 SCAR=0.0
230 11 ▶use Symbol qw(gensym);
Subroutine BEGIN: called CC=1 SCAR=0.0
231 11 ▶use POSIX qw(INT_MAX);
Subroutine BEGIN: called CC=1 SCAR=0.0
232 11 ▶use Guard qw(scope_guard);
Subroutine BEGIN: called CC=1 SCAR=0.0
233  
234 11 require Tie::Indirect; # OUR CUSTOM STUFF: stop using it?
235  
236 11 ▶use Text::CSV 1.90_01; # 1st version with known_attributes()
Subroutine BEGIN: called CC=1 SCAR=0.0
237 11 ▶use Spreadsheet::Edit::IO qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
238 OpenAsCsv @sane_CSV_read_options @sane_CSV_write_options
239 convert_spreadsheet
240 11 sheetname_from_spec filepath_from_spec form_spec_with_sheetname);
241  
242 11 ▶use constant _CALLER_OVERRIDE_CHECK_OK =>
Subroutine BEGIN: called CC=2 SCAR=7.6
Condition33%1/3
defined &Carp::CALLER_OVERRIDE_CHECK_OK && &Carp::CALLER_OVERRIDE_CHECK_OK
!ll&&!rl&&r
0011
MC/DC0%0/2
defined &Carp::CALLER_OVERRIDE_CHECK_OK && &Carp::CALLER_OVERRIDE_CHECK_OK
defined &Carp::CALLER_OVERRIDE_CHECK_OK &Carp::CALLER_OVERRIDE_CHECK_OK
Truth table33%1/3
A && B
A= defined &Carp::CALLER_OVERRIDE_CHECK_OKB= &Carp::CALLER_OVERRIDE_CHECK_OK
ABresult
0X0
100
111
243 (defined(&Carp::CALLER_OVERRIDE_CHECK_OK)
244 11 && &Carp::CALLER_OVERRIDE_CHECK_OK);
245  
246 # sub __mytraceback() { ... } archived in commit 482e09b6009
247  
248 11 ▶use constant DEFAULT_WRITE_ENCODING => 'UTF-8';
Subroutine BEGIN: called CC=1 SCAR=0.0
249 #use constant DEFAULT_READ_ENCODINGS => 'UTF-8,windows-1252';
250  
251 # This global is used by logmsg() to infer the current sheet if an apply is
252 # active, even if logmsg is called indirectly via another pkg
253 11 our $_inner_apply_sheet; # see &_apply_to_rows
254  
255 # The "current sheet", to which tied globals refer in any given package.
256 11 our %pkg2currsheet;
257  
258 2578 ▶sub __looks_like_aref($) { eval{ 1+scalar(@{$_[0]}) } } #actual or overloaded
Subroutine __looks_like_aref: called CC=1 SCAR=0.0
259  
260 11 my $identifier_re = qr/[A-Za-z_]\w*/a; # not anchored!
261 11 my $packname_re = qr/${identifier_re}(?: :: ${identifier_re})*/x; # not anchored!
262  
263 # Utility FUNCTIONS
264 #
265 228 ▶sub to_array(@) { @_ != 1 ? @_ # 0 or multiple values
Subroutine to_array: called CC=4 SCAR=17.9
Pod undocumented
266 0 ▶ : ref($_[0]) eq "ARRAY" ? @{$_[0]}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 38 38
267 0 ▶ : ref($_[0]) eq "HASH" ? @{ %{$_[0]} } # (key, value, ...)
Branch: 2 branches
BranchTrueFalseTotal
@_ != 1 ? : 190 38 228
ref $_[0] eq 'ARRAY' ? : 0 38 38
268 : $_[0] # just 1 value
269 }
270 0 ▶sub to_aref(@) { [ to_array(@_) ] }
Subroutine to_aref: not called CC=1 SCAR=6.9
Pod undocumented
271 0 ▶sub to_wanted(@) { goto &to_array if wantarray; goto &to_aref }
Subroutine to_wanted: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
272  
273 190 ▶sub to_hash(@) {
Subroutine to_hash: called CC=4 SCAR=17.9
Pod undocumented
274 190 ▶ @_==1 && ref($_[0]) eq "HASH" ? $_[0] :
Branch: 2 branches
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 190 190
@_ % 2 != 0 ? : 0 190 190
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
19000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
275 (@_ % 2)!=0 ? croak("odd arg count, expecting key => value pairs") :
276 { to_array(@_) }
277 }
278  
279 # Currently these are also exported by Spreadsheet::Edit::IO
280 # completely redundant!
281 12168 ▶sub cx2let(_) { # default arg is $_
Subroutine cx2let: called CC=2 SCAR=6.9
Pod undocumented
282 12168 my $cx = shift;
283 12168 my $ABC="A"; ++$ABC for (1..$cx);
284 12168 return $ABC
285 }
286 580 ▶sub let2cx(_) {
Subroutine let2cx: called CC=2 SCAR=7.9
Pod undocumented
287 580 my $ABC = shift;
288 580 my $n = ord(substr($ABC,0,1,"")) - ord('A');
289 580 ▶ while (length $ABC) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $ABC) 0 580 580
290 0 my $letter = substr($ABC,0,1,"");
291 0 $n = (($n+1) * 26) + (ord($letter) - ord('A'));
292 }
293 580 return $n;
294 }
295  
296 # Produce the "automatic alias" identifier for an arbitrary title
297 #
298 5882 ▶sub title2ident($) {
Subroutine title2ident: called CC=2 SCAR=6.9
Pod undocumented
299 5882 local $_ = shift;
300 5882 s/^\s+//; s/\s+$//;
301 5882 s/\W/_/g;
302 5882 s/^(?=\d)/_/;
303 # Prepend underscore to reserved identifiers.
304 5882 ▶ $_ = "_".$_ if __is_unindexed_title($_, 0);
Branch: 1 branch
BranchTrueFalseTotal
if &__is_unindexed_title($_, 0) 98 5784 5882
305 5882 $_
306 }
307  
308 # Format list as "word,word,..." without parens ; Non-barewords are "quoted".
309 0 ▶sub __fmt_uqlist(@) { join(",",map{quotekey} @_) }
Subroutine __fmt_uqlist: not called CC=1 SCAR=6.9
310 0 ▶sub __fmt_uqarray(@) { "(" . &__fmt_uqlist . ")" }
Subroutine __fmt_uqarray: not called CC=1 SCAR=6.9
311  
312 # Format list as without parens with barewords in qw/.../
313 0 ▶sub __fmt_uqlistwithqw(@) {
Subroutine __fmt_uqlistwithqw: not called CC=8 SCAR=42.8
314 0 my $barewords;
315 0 my $s = "";
316 0 foreach (map{quotekey} @_) {
317 0 ▶ if (/^\w/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\w/u) { } 0 0 0
318 0 ▶ if ($barewords++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($barewords++) { } 0 0 0
319 0 $s .= " ";
320 } else {
321 0 ▶ $s .= ", " if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
322 0 $s .= "qw/";
323 }
324 } else {
325 0 ▶ if ($barewords) {
Branch: 1 branch
BranchTrueFalseTotal
if ($barewords) 0 0 0
326 0 $s .= "/";
327 0 $barewords = 0;
328 }
329 0 ▶ $s .= ", " if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
330 }
331 0 $s .= $_;
332 }
333 0 ▶ $s .= "/" if $barewords;
Branch: 1 branch
BranchTrueFalseTotal
if $barewords 0 0 0
334 0 $s
335 }
336 0 ▶sub __fmt_uqarraywithqw(@) { "(" . &__fmt_uqlistwithqw . ")" }
Subroutine __fmt_uqarraywithqw: not called CC=1 SCAR=6.9
337  
338 # Format list of pairs as "key1 => val1, key2 => val2, ..." without parens
339 170 ▶sub __fmt_pairlist(@) {
Subroutine __fmt_pairlist: called CC=4 SCAR=13.9
340 170 my $result = "";
341 170 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 127 170 297
342 127 ▶ confess "Odd arg count, expecting key => value pairs" if @_==1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 127 127
343 127 ▶ $result .= ", " if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 4 123 127
344 127 my $key = shift @_;
345 127 my $val = shift @_;
346 127 $result .= quotekey($key)." => ".vis($val);
347 }
348 $result
349 170 }
350 0 ▶sub __fmt_pairs(@) {
Subroutine __fmt_pairs: not called CC=1 SCAR=6.9
351 0 __fmt_pairlist( map{ @$_ } @_ );
352 }
353  
354 # Concatenate strings separated by spaces, folding as necessary
355 # (strings are never broken; internal newlines go unnoticed).
356 # All lines (including the first) are indented the specified number of
357 # spaces. Explicit line-breaks may be included as "\n".
358 # A final newline is *not* included unless the last item ends with "\n".
359 49 ▶sub __fill($;$$) {
Subroutine __fill: called CC=10 SCAR=23.1
360 49 my ($items, $indent, $foldwidth) = @_;
361 49 ▶ $indent //= 4;
Condition50%1/2
$indent //= 4
l!l
049
MC/DC0%0/1
$indent //= 4
$indent
Truth table50%1/2
A
A= $indent
Aresult
11
00
362 49 ▶ $foldwidth //= 72;
Condition50%1/2
$foldwidth //= 72
l!l
049
MC/DC0%0/1
$foldwidth //= 72
$foldwidth
Truth table50%1/2
A
A= $foldwidth
Aresult
11
00
363 49 my $buf = "";
364 49 my $llen = 0;
365 49 foreach (@$items) {
366 904 ▶ if ($_ eq "\n" or
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq "\n" or $llen > $indent and $llen + length($_) > $foldwidth) 223 681 904
Condition100%6/6
$_ eq "\n" or $llen > $indent and $llen + length($_) > $foldwidth
l!l&&r!l&&!r
113110681
$llen > $indent and $llen + length($_) > $foldwidth
!ll&&!rl&&r
113568110
MC/DC100%3/3
$_ eq "\n" or $llen > $indent and $llen + length($_) > $foldwidth
$_ eq "\n" $llen > $indent $llen + length($_) > $foldwidth
Truth table100%4/4
A or B and C
A= $_ eq "\n"B= $llen > $indentC= $llen + length($_) > $foldwidth
ABCresult
1XX1
0111
00X0
0100
367 ($llen > $indent && ($llen + length($_)) > $foldwidth)) {
368 223 $buf .= "\n";
369 223 $llen = 0;
370 223 ▶ next if $_ eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "\n" 113 110 223
371 }
372 791 ▶ if ($llen == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($llen == 0) { } 223 568 791
373 223 $buf .= (" " x $indent);
374 223 $llen = $indent;
375 } else {
376 568 ▶ if (substr($buf,-1) =~ /\S/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($buf, -1) =~ /\S/u) 568 0 568
377 568 $buf .= " ";
378 568 ++$llen;
379 }
380 }
381 791 $buf .= $_;
382 791 $llen += length();
383 }
384 49 $buf;
385 }
386  
387 102 ▶sub __fmt_colspec_cx($$) {
Subroutine __fmt_colspec_cx: called CC=7 SCAR=19.5
388 # "cx NN" or "COLSPEC [cx NN]" or "COLSPEC (NOT DEFINED)" if undef cx
389 102 my ($colspec, $cx) = @_;
390 102 ▶ if (ref($colspec) eq "Regexp") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $colspec eq 'Regexp') { } 61 41 102
391 61 state $delimsets = [
392 [qw(/ /)], [qw({ })], [qw([ ])], [qw<( )>], [qw(< >)], [qw(« »)] ];
393 61 for (@$delimsets) {
394 62 my ($left, $right) = @$_;
395 62 ▶ if (index($colspec,$left)<0 && index($colspec,$right)<0) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($colspec, $left) == -1 and index($colspec, $right) == -1) 61 1 62
Condition66%2/3
index($colspec, $left) == -1 and index($colspec, $right) == -1
!ll&&!rl&&r
1061
MC/DC50%1/2
index($colspec, $left) == -1 and index($colspec, $right) == -1
index($colspec, $left) == -1 index($colspec, $right) == -1
Truth table66%2/3
A and B
A= index($colspec, $left) == -1B= index($colspec, $right) == -1
ABresult
0X0
100
111
396 61 $colspec = "qr${left}${colspec}${right}";
397 61 last;
398 }
399 }
400 } else {
401 41 $colspec = visq($colspec);
402 }
403 102 ▶ return "$colspec (NOT DEFINED)"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cx 1 101 102
404 if ! defined $cx;
405 101 ▶ $colspec eq "$cx" ? "cx $cx" : "$colspec [cx $cx]"
Branch: 1 branch
BranchTrueFalseTotal
$colspec eq "$cx" ? : 6 95 101
406 }
407 5236 ▶sub __fmt_cx($) {
Subroutine __fmt_cx: called CC=2 SCAR=6.9
408 5236 my ($cx) = @_;
409 5236 ▶ defined($cx) ? "col ".cx2let($cx)." (cx $cx)" : "(undefined)"
Branch: 1 branch
BranchTrueFalseTotal
defined $cx ? : 5235 1 5236
410 }
411  
412 # Format %colx keys "intelligently". cx values are not shown for keys which are
413 # absolute column refs. Keys with undef values (from alias {optional => 1})
414 # are omitted since they are not currently valid. A final newline IS included.
415 49 ▶sub _fmt_colx(;$$) {
Subroutine _fmt_colx: called CC=3 SCAR=11.0
416 49 my $self = shift;
417 49 my ($indent, $foldwidth) = @_;
418 49 my ($colx, $num_cols) = @$$self{qw{colx num_cols}};
419 # copy %$colx omitting keys with undef cx
420 49 ▶ my %hash = map{ defined($colx->{$_}) ? ($_ => $colx->{$_}) : () } keys %$colx;
Branch: 1 branch
BranchTrueFalseTotal
defined $$colx{$_} ? : 1184 0 1184
421 147 ▶ my sub sortbycx(@) { sort { ($colx->{$a}//-1) <=> ($colx->{$b}//-1) } @_ }
Subroutine sortbycx: called CC=3 SCAR=12.0
Condition50%2/4
$$colx{$a} // -1
l!l
26230
$$colx{$b} // -1
l!l
26230
MC/DC0%0/1
$$colx{$a} // -1
$$colx{$a}
MC/DC0%0/1
$$colx{$b} // -1
$$colx{$b}
Truth table50%1/2
A
A= $$colx{$a}
Aresult
11
00
Truth table50%1/2
A
A= $$colx{$b}
Aresult
11
00
422 196 ▶ my sub subset($) { # format items, deleting from %hash
Subroutine subset: called CC=6 SCAR=17.9
423 196 my $specs = shift;
424 196 my (@items, $curr, $curr_desc);
425 196 my $curr_cx = -1;
426 987 ▶ my sub flush() {
Subroutine flush: called CC=2 SCAR=6.9
427 987 ▶ return unless $curr_cx >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $curr_cx >= 0 212 775 987
428 775 push @items, $curr.$curr_desc;
429 775 $curr = $curr_desc = undef; ##DEBUGGING
430 775 $curr_cx = -1;
431 }
432 1184 ▶ my sub additem($$) {
Subroutine additem: called CC=8 SCAR=20.8
433 1184 (local $_, my $cx) = @_;
434 1184 ▶ flush() if $curr_cx != $cx;
Branch: 1 branch
BranchTrueFalseTotal
if $curr_cx != $cx 775 409 1184
435 1184 ▶ if ($curr_cx >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($curr_cx >= 0) { } 409 775 1184
436 409 $curr .= ",".quotekey($_);
437 } else {
438 775 $curr_cx = $cx;
439 775 $curr = quotekey($_);
440 my $misfit = (/^[A-Z]{1,2}$/ && $colx->{$_} != let2cx($_))
441 775 ▶ || (/^\d+$/ && $colx->{$_} != $_)
Condition91%11/12
/^[A-Z]{1,2}$/u && $$colx{$_} != &let2cx($_) || /^\d+$/u && $$colx{$_} != $_ || /^\D../u
l!l&&r!l&&!r
40317418
/^[A-Z]{1,2}$/u and $$colx{$_} != &let2cx($_) or /^\d+$/u and $$colx{$_} != $_
l!l&&r!l&&!r
1030735
/^[A-Z]{1,2}$/u and $$colx{$_} != &let2cx($_)
!ll&&!rl&&r
37738810
/^\d+$/u and $$colx{$_} != $_
!ll&&!rl&&r
735030
MC/DC80%4/5
/^[A-Z]{1,2}$/u && $$colx{$_} != &let2cx($_) || /^\d+$/u && $$colx{$_} != $_ || /^\D../u
/^[A-Z]{1,2}$/u $$colx{$_} != &let2cx($_) /^\d+$/u $$colx{$_} != $_ /^\D../u
Truth table45%5/11
A and B or C and D || E
A= /^[A-Z]{1,2}$/uB= $$colx{$_} != &let2cx($_)C= /^\d+$/uD= $$colx{$_} != $_E= /^\D../u
ABCDEresult
11XXX1
0X11X1
1011X1
0X0X11
0X1011
100X11
101011
0X0X00
0X1000
100X00
101000
442 || (/^\D../) # show titles with cx too
443 ;
444 775 ▶ $curr_desc = $misfit ? "(cx ".vis($hash{$_}).")" : "";
Branch: 1 branch
BranchTrueFalseTotal
$misfit ? : 357 418 775
445 }
446 }
447 196 foreach (@$specs) {
448 1200 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 16 1184 1200
449 16 flush();
450 16 push @items, $$_; # \"string" means insert "string" literally
451 } else {
452 # Work around old Perl lexical sub limitations...
453 1184 my $item=$hash{$_};
454 1184 ▶ oops(dvis '$_ $specs hash=',hvis(%hash)) unless defined $item;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $item 0 1184 1184
455 1184 additem($_, $item);
456 1184 ▶ delete $hash{$_} // oops;
Condition33%1/3
delete $hash{$_} // &oops()
l!l&&r!l&&!r
118400
MC/DC0%0/2
delete $hash{$_} // &oops()
delete $hash{$_} &oops()
Truth table33%1/3
A // B
A= delete $hash{$_}B= &oops()
ABresult
1X1
011
000
457 }
458 }
459 196 flush();
460 196 ▶ push @items, "\n" if @items; # wrap before next subset, or at end
Branch: 1 branch
BranchTrueFalseTotal
if @items 113 83 196
461 @items
462 196 }
463 49 my @ABCs = subset [ map{ my $A = cx2let($_);
464 #u($hash{$A}) eq $_ ? $A : \" "
465 404 ▶ u($hash{$A}) eq $_ ? $A : \"<$A masked>"
Branch: 1 branch
BranchTrueFalseTotal
&u($hash{$A}) eq $_ ? : 388 16 404
466 } 0..$num_cols-1 ];
467  
468 # More lexical sub bug work-arounds...
469 49 my @ss1 = subset [sortbycx grep{ /^(=.*\D)\w+$/ } keys %hash]; # normal titles
470 49 my @ss2 = subset [sortbycx grep{ /^\d+$/ } keys %hash]; # numeric titles
471 49 my @ss3 = subset [sortbycx keys %hash]; # oddities
472 49 __fill [
473 @ABCs,
474 @ss1,
475 @ss2,
476 @ss3,
477 ], $indent, $foldwidth;
478 }
479  
480 # Is a title a special symbol, looks like a cx number, or is a Perl
481 # built-in variable?
482 15730 ▶sub __is_unindexed_title($$) {
Subroutine __is_unindexed_title: called CC=9 SCAR=22.5
483 15730 my ($title, $num_cols) = @_;
484 15730 ▶ oops unless defined $title;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title 0 15730 15730
485 ### FIXME BUG(?): I think colx only needs to exclude titles ^ and $
486 ### (which are reserved by Spreadsheet::Edit to mean 1st & last col)
487 ### and numbers which exceed maxcx; all other titles should be
488 ### unambiguous as %colx keys, however...
489 ### --> Perl's reserved names must still be excluded as tied variable
490 ### names; this would need a separate check for _all_valid_idents()
491 15730 ▶my $r =
Condition80%17/21
$title eq '' || $title =~ /^\W$/u || $title =~ /\^\w+$/u || $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/u || $title =~ /::/u || $title =~ /^[0-9]$/u || $title =~ /^[1-9]\d+$/a && $title <= $num_cols
l!l&&r!l&&!r
260015470
$title eq "" or $title =~ /^\W$/u or $title =~ /\^\w+$/u or $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/u or $title =~ /::/u or $title =~ /^[0-9]$/u
l!l&&r!l&&!r
1966415470
$title eq "" or $title =~ /^\W$/u or $title =~ /\^\w+$/u or $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/u or $title =~ /::/u
l!l&&r!l&&!r
196015534
$title eq "" or $title =~ /^\W$/u or $title =~ /\^\w+$/u or $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/u
l!l&&r!l&&!r
1049215534
$title eq "" or $title =~ /^\W$/u or $title =~ /\^\w+$/u
l!l&&r!l&&!r
104015626
$title eq "" or $title =~ /^\W$/u
l!l&&r!l&&!r
109415626
$title =~ /^[1-9]\d+$/a and $title <= $num_cols
!ll&&!rl&&r
152022680
MC/DC50%4/8
$title eq '' || $title =~ /^\W$/u || $title =~ /\^\w+$/u || $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/u || $title =~ /::/u || $title =~ /^[0-9]$/u || $title =~ /^[1-9]\d+$/a && $title <= $num_cols
$title eq "" $title =~ /^\W$/u $title =~ /\^\w+$/u $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/u $title =~ /::/u $title =~ /^[0-9]$/u $title =~ /^[1-9]\d+$/a $title <= $num_cols
Truth table66%6/9
A or B or C or D or E or F || G and H
A= $title eq ""B= $title =~ /^\W$/uC= $title =~ /\^\w+$/uD= $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/uE= $title =~ /::/uF= $title =~ /^[0-9]$/uG= $title =~ /^[1-9]\d+$/aH= $title <= $num_cols
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
000000111
0000000X0
000000100
492 $title eq ""
493 || $title =~ /^\W$/ # ^ or $ or any single punctuation or control-char
494 || $title =~ /\^\w+$/ # ^Var to not confuse w Perl "control-character" names
495 || $title =~ /^(?:ARGV|ARGVOUT|_|REGERROR|REGMARK|AUTOLOAD|a|b)$/
496 || $title =~ /::/ # package::qualified::name
497 || $title =~ /^[0-9]$/ # $0 and regex $1 $2 .. $9
498 # (regardless of max cx)
499 || ($title =~ /^[1-9]\d+$/a
500 && $title <= $num_cols) # cx values > 9 # n.b. OR maxcx+1
501 ;
502 #btw ivis 'unindexed: $title' if $r;
503 15730 $r
504 }
505 4015 ▶sub _unindexed_title { #method for use by tests
Subroutine _unindexed_title: called CC=1 SCAR=0.0
506 4015 my $self = shift;
507 4015 __is_unindexed_title(shift(), $$self->{num_cols});
508 }
509  
510 # Return (normals, unindexed) where each is [title => cx, ...] sorted by cx
511 531 ▶sub _get_usable_titles {
Subroutine _get_usable_titles: called CC=7 SCAR=22.4
512 531 my $self = shift;
513 531 my ($rows, $title_rx, $num_cols) = @$$self{qw{rows title_rx num_cols}};
514 531 ▶ my $title_row = $rows->[$title_rx // oops];
Condition33%1/3
$title_rx // &oops()
l!l&&r!l&&!r
53100
MC/DC0%0/2
$title_rx // &oops()
$title_rx &oops()
Truth table33%1/3
A // B
A= $title_rxB= &oops()
ABresult
1X1
011
000
515 531 my @unindexed;
516 my @normals;
517 531 my %seen;
518 531 for my $cx (0 .. $num_cols-1) {
519 2865 my $title = $title_row->[$cx];
520 2865 ▶ next if $title eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $title eq "" 384 2481 2865
521 2481 ▶ if (exists $seen{$title}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $seen{$title}) 0 2481 2481
522 $self->_carponce("Warning: Non-unique title ", visq($title), " will not be usable for COLSPEC\n",
523 0 ▶ "(Col ",cx2let($seen{$title})," and ",cx2let($cx),")\n") unless $$self->{silent};
Branch: 1 branch
BranchTrueFalseTotal
unless $$$self{"silent"} 0 0 0
524 0 @normals = grep{ $_->[0] ne $title } @normals;
525 0 @unindexed = grep{ $_->[0] ne $title } @unindexed;
526 0 next;
527 }
528 2481 $seen{$title} = $cx;
529 2481 ▶ if (__is_unindexed_title($title, $num_cols)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&__is_unindexed_title($title, $num_cols)) { } 162 2319 2481
530 162 push @unindexed, [$title, $cx];
531 } else {
532 2319 push @normals, [$title, $cx];
533 }
534 }
535 3462 [sort { $a->[1] <=> $b->[1] } @normals],
536 0 [sort { $a->[1] <=> $b->[1] } @unindexed]
537 }
538  
539 # Non-OO api: Explicitly create a new sheet and make it the "current sheet".
540 # Options (e.g. to specify initial content) may be specified in an
541 # initial {OPTHASH} and/or as linear key => value pairs.
542 #
543 # Note: Most functions automatically create an empty sheet if no sheet
544 # exists, so this is only really needed when using more than one sheet
545 # or if you want to initialize a sheet from data in memory.
546 # N.B. the corresponding OO interface is Spreadsheet::Edit->new(...)
547 #
548 92 ▶sub new_sheet(@) {
Subroutine new_sheet: called CC=7 SCAR=20.4
Pod documented
549 92 my $opthash = &__opthash;
550 92 my %opts = (%$opthash, %{to_hash(@_)}); # new() merges these anyway
551  
552 92 my ($userpkg, $fn, $lno, $subname) = @{ __filter_frame(__usercall_info()) };
553 92 ▶ $userpkg = delete $opts{package} if exists $opts{package};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts{"package"} 0 92 92
554  
555 92 ▶ my $pkgmsg = $opts{package} ? " [for pkg $userpkg]" : "";
Branch: 1 branch
BranchTrueFalseTotal
$opts{'package'} ? : 0 92 92
556 92 ▶ croak "Invalid 'package' ",u($userpkg),"\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $userpkg and $userpkg =~ /^$packname_re$/u 0 92 92
Condition33%1/3
defined $userpkg and $userpkg =~ /^$packname_re$/u
!ll&&!rl&&r
0092
MC/DC0%0/2
defined $userpkg and $userpkg =~ /^$packname_re$/u
defined $userpkg $userpkg =~ /^$packname_re$/u
Truth table33%1/3
A and B
A= defined $userpkgB= $userpkg =~ /^$packname_re$/u
ABresult
0X0
100
111
557 unless defined($userpkg) && $userpkg =~ /^$packname_re$/;
558  
559 92 ▶ $opts{data_source} ||= "Created at ${fn}:$lno by $subname";
Condition100%2/2
$opts{"data_source"} ||= "Created at ${fn}:$lno by $subname"
l!l
587
MC/DC100%1/1
$opts{"data_source"} ||= "Created at ${fn}:$lno by $subname"
$opts{"data_source"}
Truth table100%2/2
A
A= $opts{"data_source"}
Aresult
11
00
560  
561 92 my $sheet = __silent_new(\%opts);
562  
563 log_call [$opthash, @_], [\(fmt_sheet($sheet), \$pkgmsg)]
564 92 ▶ if $$sheet->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$sheet{"verbose"} 0 92 92
565  
566 92 $pkg2currsheet{$userpkg} = $sheet
567 }
568  
569 # logmsg() - Concatenate strings to form a "log message", possibly
570 # prefixed with a description of a "focus" sheet and optionally
571 # a specific row. A final \n is appended if needed.
572 #
573 # An optional initial {OPTHASH} argument may contain
574 # package => <packagename>
575 #
576 # The "focus" sheet and row, if any, are determined as follows:
577 #
578 # If the first argument (after {OPTHASH}, if present) is a sheet
579 # object, [sheet_object], [sheet_object, rx], or [sheet_object, undef]
580 # then the indicated sheet and (optionally) row are used.
581 # Note that if called as a method the first arg will be the sheet object.
582 #
583 # Otherwise the first arg is not special and is included in the message.
584 #
585 # If no sheet is identified above, then the caller's package
586 # (or the package specified in {OPTHASH}) active sheet is used, if any.
587 #
588 # If still no sheet is identified, then the sheet of the innermost apply
589 # currently executing (anywhere up the stack) is used, if any; this sheet
590 # is internally saved in a global by the apply* methods.
591 #
592 # If a sheet is identified but no specific rx, then the
593 # "current row" of an active apply on that sheet is used, if any.
594 #
595 # If a focus sheet or sheet & row were identified, then the caller-supplied
596 # message is prefixed by "(<description>):" or "(row <num> <description>):"
597 # where <description> comes from:
598 #
599 # 1) If the sheet attribute {logmsg_pfx_gen} is defined to a subref,
600 # the sub is called and all returned items other than undef are
601 # concatenated (any undefs in the returned list are ignored); otherwise
602 #
603 # 2) The "sheetname" property is used, if defined; otherwise
604 #
605 # 3) the "data_source" property is used, which defaults to the name of the
606 # spreadsheet read by read_spreadsheet().
607 #
608 # FIXME: I should either rename logmsg_pfx_gen as logmsg_sheetdesc_gen
609 # to reflect that it only generates the sheet-description part,
610 # or else make prefix generators produce the entire message prefix
611 # including any row number.
612 #
613 325 ▶sub _default_pfx_gen($$) {
Subroutine _default_pfx_gen: called CC=3 SCAR=11.7
614 325 my ($sheet, $rx) = @_;
615 # N.B. $rx may be undef!
616 325 ▶ confess "bug" unless ref($sheet) =~ /^Spreadsheet::Edit\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless ref($sheet) =~ /^Spreadsheet::Edit\b/u 0 325 325
617 ($$sheet->{sheetname} || $$sheet->{data_source})
618 325 ▶}
Branch: 1 branch
BranchTrueFalseTotal
$$$sheet{"sheetname"} or $$$sheet{"data_source"} 0 325 325
619 336 ▶sub logmsg(@) {
Subroutine logmsg: called CC=21 SCAR=30.7
Pod documented
620 336 my %opts = %{ &__opthash };
621 336 my $package = delete $opts{package};
622 336 ▶ carp dvis 'Option other than "package" in {OPTHASH} passed to logmsg()'
Branch: 1 branch
BranchTrueFalseTotal
if keys %opts 0 336 336
623 if keys %opts; # really necessary?
624 336 my ($sheet, $rx);
625 336 ▶ if (@_ > 0 && ref($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 and ref $_[0]) 248 88 336
Condition100%3/3
@_ > 0 and ref $_[0]
!ll&&!rl&&r
880248
MC/DC100%2/2
@_ > 0 and ref $_[0]
@_ > 0 ref $_[0]
Truth table100%3/3
A and B
A= @_ > 0B= ref $_[0]
ABresult
0X0
100
111
626 248 ▶ if (ref($_[0]) =~ /^Spreadsheet::Edit\b/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref($_[0]) =~ /^Spreadsheet::Edit\b/u) { } 28 220 248
elsif (ref $_[0] eq 'ARRAY' and @{$_[0];} <= 2 and ref($_[0][0]) . '' =~ /^Spreadsheet::Edit\b/u) { } 220 0 220
627 28 $sheet = shift;
628 }
629 ▶ elsif (ref($_[0]) eq "ARRAY"
Condition33%2/6
ref $_[0] eq "ARRAY" and @{$_[0];} <= 2 and ref($_[0][0]) . "" =~ /^Spreadsheet::Edit\b/u
!ll&&!rl&&r
00220
ref $_[0] eq "ARRAY" and @{$_[0];} <= 2
!ll&&!rl&&r
00220
MC/DC0%0/3
ref $_[0] eq "ARRAY" and @{$_[0];} <= 2 and ref($_[0][0]) . "" =~ /^Spreadsheet::Edit\b/u
ref $_[0] eq "ARRAY" @{$_[0];} <= 2 ref($_[0][0]) . "" =~ /^Spreadsheet::Edit\b/u
Truth table25%1/4
A and B and C
A= ref $_[0] eq "ARRAY"B= @{$_[0];} <= 2C= ref($_[0][0]) . "" =~ /^Spreadsheet::Edit\b/u
ABCresult
0XX0
10X0
1100
1111
630 220 && @{ $_[0] } <= 2
631 && ref($_[0]->[0])."" =~ /^Spreadsheet::Edit\b/) {
632 220 ($sheet, $rx) = @{ shift @_ };
633 }
634 }
635 336 my $msgstr = join("", grep{defined} @_);
636 336 ▶ if (! defined $sheet) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sheet) 88 248 336
637 88 ▶ $sheet = $pkg2currsheet{$package // scalar(caller)};
Condition100%2/2
$package // scalar caller
l!l
1177
MC/DC100%1/1
$package // scalar caller
$package
Truth table100%2/2
A
A= $package
Aresult
11
00
638 }
639 336 ▶ if (! defined $sheet) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $sheet) 22 314 336
640 22 $sheet = $Spreadsheet::Edit::_inner_apply_sheet;
641 }
642 336 ▶ if (! defined $rx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rx) 144 192 336
643 144 ▶ $rx = eval{ $sheet->rx() } if defined($sheet);
Branch: 1 branch
BranchTrueFalseTotal
if defined $sheet 133 11 144
644 }
645 336 my @prefix;
646 336 ▶ if (defined $sheet) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sheet) 325 11 336
647 325 push @prefix, "(";
648 325 ▶ if (defined($rx) && ($rx < 0 || $rx > $#{$sheet->rows})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rx and $rx < 0 || $rx > $#{$sheet->rows;}) { } 96 229 325
Condition100%6/6
defined $rx and $rx < 0 || $rx > $#{$sheet->rows;}
!ll&&!rl&&r
9213796
$rx < 0 or $rx > $#{$sheet->rows;}
l!l&&r!l&&!r
4848137
MC/DC100%3/3
defined $rx and $rx < 0 || $rx > $#{$sheet->rows;}
defined $rx $rx < 0 $rx > $#{$sheet->rows;}
Truth table100%4/4
A and B or C
A= defined $rxB= $rx < 0C= $rx > $#{$sheet->rows;}
ABCresult
0XX0
1000
11X1
1011
649 96 push @prefix, "Row ".($rx+1)."[INVALID RX $rx] ";
650 96 $rx = undef; # avoid confusing user's prefix generator
651 } else {
652 229 ▶ push @prefix, "Row ".($rx+1)." " if defined($rx);
Branch: 1 branch
BranchTrueFalseTotal
if defined $rx 137 92 229
653 }
654 325 ▶ my $pfxgen = $sheet->attributes->{logmsg_pfx_gen} // \&_default_pfx_gen;
Condition50%1/2
$sheet->attributes->{"logmsg_pfx_gen"} // \&Spreadsheet::Edit::_default_pfx_gen
l!l
0325
MC/DC0%0/1
$sheet->attributes->{"logmsg_pfx_gen"} // \&Spreadsheet::Edit::_default_pfx_gen
$sheet->attributes->{"logmsg_pfx_gen"}
Truth table50%1/2
A
A= $sheet->attributes->{"logmsg_pfx_gen"}
Aresult
11
00
655 325 push @prefix, &$pfxgen($sheet, $rx), "): ";
656 }
657 336 ▶ my $suffix = ($msgstr =~ /\n\z/s ? "" : "\n");
Branch: 1 branch
BranchTrueFalseTotal
$msgstr =~ /\n\z/su ? : 56 280 336
658 # If the message string starts with newlines, put them before the prefix
659 336 ▶ if ($msgstr =~ s/\A(\n+)([^\n])/$2/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msgstr =~ s/\A(\n+)([^\n])/$2/su) 0 336 336
660 0 unshift @prefix, $1;
661 }
662 336 return join "", grep{defined} @prefix, $msgstr, $suffix;
663 }
664  
665 #####################################################################
666 # Locate the nearest call to a public sub in the call stack.
667 # [Now *GENERIC*]
668 #
669 # A "public" sub means anything named starting with [a-z], irrespective
670 # of package, and not mentioned in $USERCALL_NOT.
671 # The assumption is that internal subs are named with an initial
672 # underscore or are ALLCAPS (e.g. for constants).
673 #
674 # RETURNS
675 # ([frame], [called args]) in array context
676 # [frame] in scalar context
677 #
678 # "frame" means caller(n) results:
679 # 0 1 2 3
680 # package filename linenum subname ...
681 #
682 1405 ▶sub __usercall_info(;$) {
Subroutine __usercall_info: called CC=6 SCAR=18.0
683 1405 for (my $lvl=1 ; ; ++$lvl) {
684 5529 @DB::args = \$lvl if _CALLER_OVERRIDE_CHECK_OK; # see mytraceback()
685 5529 my @frame = do{ package
686 5529 DB; caller($lvl) };
687 5529 ▶ oops dvis('$lvl @frame') unless defined($frame[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame[0] 0 5529 5529
688 5529 ▶ if ( $frame[3] =~ /::([a-z][^:]*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame[3] =~ /::([a-z][^:]*)/u) 1405 4124 5529
689 1405 ▶ return \@frame unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 1307 98 1405
690 98 my @args;
691 98 my $hasargs = $frame[4];
692 98 ▶ if ($hasargs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hasargs) 98 0 98
693 98 eval{ @args = @DB::args };
694 98 ▶ @args=("<perl bug(?) prevented getting args>") if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 98 98
695 }
696 98 return (\@frame, \@args)
697 }
698 }
699 }
700  
701 194 ▶sub __filter_frame($) { #clean-up/abbreviate for display purposes
Subroutine __filter_frame: called CC=1 SCAR=0.0
702 194 my @frame = @{shift @_};
703 194 $frame[1] = basename $frame[1]; # filename
704 194 $frame[3] =~ s/.*:://; # subname
705 \@frame
706 194 }
707 96 ▶sub __fn_ln_methname() {
Subroutine __fn_ln_methname: called CC=1 SCAR=0.0
708 96 @{ __filter_frame(__usercall_info()) }[1,2,3]; # (fn, lno, subname)
709 }
710  
711 0 ▶sub __methname() {
Subroutine __methname: not called CC=1 SCAR=6.9
712 0 (&__fn_ln_methname())[2]
713 }
714  
715 1211 ▶sub __find_userpkg() {
Subroutine __find_userpkg: called CC=1 SCAR=0.0
716 1211 ${ __usercall_info() }[0];
717 }
718  
719 # This always returns the caller's caller's package but also
720 # checks that it is not an internal call, which should never happen
721 7 ▶sub __callerpkg() {
Subroutine __callerpkg: called CC=2 SCAR=7.1
722 7 my $pkg = (caller(1))[0];
723 7 ▶ oops if index($pkg,__PACKAGE__) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($pkg, __PACKAGE__) == 0 0 7 7
724 7 $pkg
725 }
726  
727 # Create a new object without allowing any logging. This is used when
728 # new() is called implicitly by something else and new's log messages
729 # might display an internal filename (namely Edit.pm).
730 #
731 # debug/verbose args are removed from the arguments passed to new()
732 # and put back into the object after it is created.
733 98 ▶sub __silent_new(@) {
Subroutine __silent_new: called CC=4 SCAR=14.0
734 98 my $opthash = &__opthash;
735 98 my $new_args = to_hash(@_);
736  
737 98 my %saved;
738 98 foreach my $key (qw/verbose debug/) {
739 196 ▶ $saved{$key} = $opthash->{$key} if exists($opthash->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$opthash{$key} 0 196 196
740 196 $opthash->{$key} = 0; # force off
741 196 ▶ $saved{$key} = delete($new_args->{$key}) if exists($new_args->{$key});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$new_args{$key} 0 196 196
742 }
743  
744 98 my $self = Spreadsheet::Edit->new($opthash, %$new_args);
745  
746 98 delete @$$self{qw/verbose debug/};
747 98 $self->_set_verbose_debug_silent(%saved);
748  
749 98 $self
750 }
751  
752 #####################################################################
753 # Get "self" for a function/method combo sub:
754 # If the first arg is an object ref we shift it off and use that
755 # (i.e. assume it is called as a method); otherwise we assume it's a
756 # functional-API function call and use the caller's "current sheet"
757 # (if none exists, __self creates one but __selfmust throws).
758 #
759 # This must be used with special syntax like
760 # my $self = &__self;
761 # which re-uses @_ so we can shift @_ as seen by our caller.
762  
763 14363 ▶sub __self_ifexists {
Subroutine __self_ifexists: called CC=4 SCAR=13.9
764  
765 # If the first arg is an object ref, shift it off and return it;
766 # Otherwise, if the caller's "current sheet" exists, return that;
767 # otherwise return undef.
768  
769 (defined(blessed($_[0])) && $_[0]->isa(__PACKAGE__) && shift(@_))
770 14363 ▶ || $pkg2currsheet{__find_userpkg()};
Branch: 1 branch
BranchTrueFalseTotal
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) and shift @_ or $Spreadsheet::Edit::pkg2currsheet{&__find_userpkg()} 13152 1211 14363
Condition83%5/6
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) and shift @_
!ll&&!rl&&r
1211013152
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
1208313152
MC/DC66%2/3
defined builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__) and shift @_
defined builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__) shift @_
Truth table75%3/4
A and B and C
A= defined builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)C= shift @_
ABCresult
0XX0
10X0
1100
1111
771 }
772 13757 ▶sub __selfmust { # sheet must exist, otherwise throw
Subroutine __selfmust: called CC=2 SCAR=10.5
773 13757 ▶ &__self_ifexists || do{
Branch: 1 branch
BranchTrueFalseTotal
&__self_ifexists or do { my $pkg = caller 1; confess(&__methname(), " : No sheet is active in package $pkg\n") } 13757 0 13757
774 0 my $pkg = caller(1);
775 #croak __methname()," : No sheet is active in package $pkg\n"
776 0 confess __methname()," : No sheet is active in package $pkg\n"
777 };
778 }
779  
780 606 ▶sub __self { # a new empty sheet is created if necessary
Subroutine __self: called CC=2 SCAR=6.9
781 606 ▶ &__self_ifexists || do{
Branch: 1 branch
BranchTrueFalseTotal
&__self_ifexists or do { my %opts; () = &__usercall_info(); () = @{&__filter_frame($frame);}; $opts{"data_source"} = "(Created implicitly by '${subname}' at ${fn}:$lno)"; my $self = ($Spreadsheet::Edit::pkg2currsheet{$userpkg} = &__silent_new(\%opts)); $self } 600 6 606
782 # Create a new empty sheet and make it the caller's "current sheet".
783 6 my %opts;
784 6 my ($frame, $args) = __usercall_info();
785  
786 6 my ($userpkg, $fn, $lno, $subname) = @{ __filter_frame($frame) };
787 6 $opts{data_source} = "(Created implicitly by '$subname' at ${fn}:$lno)";
788  
789 6 my $self = $pkg2currsheet{$userpkg} = __silent_new(\%opts);
790 6 $self
791 }
792 }
793  
794  
795 ## Helpers...
796  
797 21807 ▶sub __opthash {
Subroutine __opthash: called CC=2 SCAR=6.9
798 21807 ▶ ref($_[0]) eq "HASH" ? shift(@_) : {}
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 407 21400 21807
799 }
800 578 ▶sub __selfmust_opthash {
Subroutine __selfmust_opthash: called CC=1 SCAR=0.0
801 578 my $self = &__selfmust;
802 578 my $opthash = &__opthash;
803 578 ($self, $opthash)
804 }
805 78 ▶sub __self_opthash {
Subroutine __self_opthash: called CC=1 SCAR=0.0
806 78 my $self = &__self;
807 78 my $opthash = &__opthash;
808 78 ($self, $opthash)
809 }
810 328 ▶sub __selfonly {
Subroutine __selfonly: called CC=2 SCAR=7.1
811 328 my $self = &__self;
812 328 ▶ confess __methname, " expects no arguments!\n" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 328 328
813 328 $self
814 }
815 12445 ▶sub __selfmustonly {
Subroutine __selfmustonly: called CC=2 SCAR=7.1
816 12445 my $self = &__selfmust;
817 12445 ▶ confess __methname, " expects no arguments!\n" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 12445 12445
818 12445 $self
819 }
820  
821 69 ▶sub __self_opthash_Nargs($@) { # (num_expected_args, @_)
Subroutine __self_opthash_Nargs: called CC=2 SCAR=7.0
822 69 my $Nargs = shift;
823 69 my ($self, $opthash) = &__self_opthash;
824 #croak
825 69 ▶ croak __methname, " expects $Nargs arguments, not ",scalar(@_),"\n"
Branch: 1 branch
BranchTrueFalseTotal
if $Nargs != @_ 0 69 69
826 if $Nargs != @_;
827 69 ($self, $opthash, @_)
828 }
829 0 ▶sub __self_opthash_0args { unshift @_,0; goto &__self_opthash_Nargs }
Subroutine __self_opthash_0args: not called CC=1 SCAR=6.9
830 69 ▶sub __self_opthash_1arg { unshift @_,1; goto &__self_opthash_Nargs }
Subroutine __self_opthash_1arg: called CC=1 SCAR=0.0
831 0 ▶sub __self_opthash_2args { unshift @_,2; goto &__self_opthash_Nargs }
Subroutine __self_opthash_2args: not called CC=1 SCAR=6.9
832 0 ▶sub __self_opthash_3args { unshift @_,3; goto &__self_opthash_Nargs }
Subroutine __self_opthash_3args: not called CC=1 SCAR=6.9
833  
834 # Check that an option hash has only valid keys, and values aren't undef
835 733 ▶sub __validate_opthash($$;@) {
Subroutine __validate_opthash: called CC=8 SCAR=22.1
836 733 my ($opthash, $valid_keys, %opts) = @_;
837 733 ▶ return unless defined $opthash; # silently accept undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opthash 0 733 733
838 733 foreach my $k (keys %$opthash) {
839 croak "Unrecognized ",($opts{desc}//"option")," '$k'\n"
840 194 ▶ unless first{$_ eq $k} @$valid_keys;
Subroutine __ANON__: called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq $k; } , @$valid_keys) 0 194 194
Condition0%0/2
$opts{"desc"} // "option"
l!l
00
MC/DC0%0/1
$opts{"desc"} // "option"
$opts{"desc"}
Truth table0%0/2
A
A= $opts{"desc"}
Aresult
11
00
841 confess "Option '$k' may not be 'undef'"
842 if $opts{undef_ok_only} && !defined($opthash->{$k})
843 194 ▶ && !grep{$_ eq $k} @{$opts{undef_ok_only}};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"undef_ok_only"} and not defined $$opthash{$k} and not grep({$_ eq $k;} @{$opts{"undef_ok_only"};}) 0 194 194
Condition83%5/6
$opts{"undef_ok_only"} and not defined $$opthash{$k} and not grep({$_ eq $k;} @{$opts{"undef_ok_only"};})
!ll&&!rl&&r
19220
$opts{"undef_ok_only"} and not defined $$opthash{$k}
!ll&&!rl&&r
21902
MC/DC0%0/3
$opts{"undef_ok_only"} and not defined $$opthash{$k} and not grep({$_ eq $k;} @{$opts{"undef_ok_only"};})
$opts{"undef_ok_only"} not defined $$opthash{$k} not grep({$_ eq $k;} @{$opts{"undef_ok_only"};})
Truth table75%3/4
A and not(B) and not(C)
A= $opts{"undef_ok_only"}B= not defined $$opthash{$k}C= not grep({$_ eq $k;} @{$opts{"undef_ok_only"};})
ABCresult
0XX0
10X0
1100
1111
844 }
845 $opthash
846 733 }
847  
848 # Copy verbose/debug/silent options into $self, deleting them from
849 # the provided options hash.
850 # RETURNS: Hash of original values to pass to _restore_stdopts()
851 #
852 # This is used by methods which accept {verbose} etc. options
853 # which override what is in the object for the duration of that method call.
854 647 ▶sub _set_stdopts {
Subroutine _set_stdopts: called CC=3 SCAR=11.0
855 647 my ($self, $opthash) = @_;
856 647 my $previous = {};
857 647 foreach my $key (qw/verbose debug silent/) {
858 1941 ▶ if (exists $opthash->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$opthash{$key}) 123 1818 1941
859 123 $previous->{$key} = $$self->{$key};
860 123 $$self->{$key} = delete($opthash->{$key});
861 }
862 }
863 $previous
864 647 }
865 647 ▶sub _restore_stdopts {
Subroutine _restore_stdopts: called CC=1 SCAR=0.0
866 647 my $self = shift;
867 647 my $saved = shift;
868 647 @$$self{keys %$saved} = values %$saved;
869 }
870  
871 87 ▶sub _validate_ident($) {
Subroutine _validate_ident: called CC=4 SCAR=14.9
872 87 ▶ croak "identifier is undef!" unless defined $_[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 87 87
873 87 ▶ croak "identifier is empty" unless $_[0] ne "";
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] ne "" 0 87 87
874 87 ▶ croak ivisq '"$_[0]" is not a valid identifier\n'
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] eq &title2ident($_[0]) 0 87 87
875 unless $_[0] eq title2ident($_[0]);
876 87 $_[0]
877 }
878  
879 # Check that an option hash has only valid keys
880 0 ▶sub __validate_4pthash($$;$) {
Subroutine __validate_4pthash: not called CC=5 SCAR=34.0
881 0 my ($opthash, $valid_keys, $optdesc) = @_;
882 0 ▶ return unless defined $opthash; # silently accept undef
Branch: 1 branch
BranchTrueFalseTotal
unless defined $opthash 0 0 0
883 0 foreach my $k (keys %$opthash) {
884 croak "Unrecognized ",($optdesc//"option")," '$k'"
885 0 ▶ unless first{$_ eq $k} @$valid_keys;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless &first(sub { $_ eq $k; } , @$valid_keys) 0 0 0
Condition0%0/2
$optdesc // "option"
l!l
00
MC/DC0%0/1
$optdesc // "option"
$optdesc
Truth table0%0/2
A
A= $optdesc
Aresult
11
00
886 }
887 $opthash
888 0 }
889  
890 342 ▶sub __validate_nonnegi($;$) {
Subroutine __validate_nonnegi: called CC=4 SCAR=19.1
891 342 ▶ croak(($_[1]//"argument")." must be a non-negative integer",
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] and "$_[0]" =~ /^\d+$/u 0 342 342
Condition20%1/5
defined $_[0] and "$_[0]" =~ /^\d+$/u
!ll&&!rl&&r
00342
$_[1] // "argument"
l!l
00
MC/DC0%0/2
defined $_[0] and "$_[0]" =~ /^\d+$/u
defined $_[0] "$_[0]" =~ /^\d+$/u
MC/DC0%0/1
$_[1] // "argument"
$_[1]
Truth table33%1/3
A and B
A= defined $_[0]B= "$_[0]" =~ /^\d+$/u
ABresult
0X0
100
111
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
892 " (not ".u($_[0]).")")
893 unless defined($_[0]) && "$_[0]" =~ /^\d+$/;
894 342 $_[0]
895 }
896 23 ▶sub __validate_nonnegi_or_undef($;$) {
Subroutine __validate_nonnegi_or_undef: called CC=4 SCAR=16.9
897 23 ▶ croak(($_[1]//"argument")." must be a non-negative integer or undef",
Branch: 1 branch
BranchTrueFalseTotal
unless not defined $_[0] or "$_[0]" =~ /^\d+$/u 0 23 23
Condition40%2/5
not defined $_[0] or "$_[0]" =~ /^\d+$/u
l!l&&r!l&&!r
13100
$_[1] // "argument"
l!l
00
MC/DC0%0/2
not defined $_[0] or "$_[0]" =~ /^\d+$/u
not defined $_[0] "$_[0]" =~ /^\d+$/u
MC/DC0%0/1
$_[1] // "argument"
$_[1]
Truth table66%2/3
not(A) or B
A= not defined $_[0]B= "$_[0]" =~ /^\d+$/u
ABresult
1X1
011
000
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
898 " (not ".u($_[0]).")")
899 unless !defined($_[0]) || "$_[0]" =~ /^\d+$/;
900 23 $_[0]
901 }
902  
903 226 ▶sub __validate_pairs(@) {
Subroutine __validate_pairs: called CC=5 SCAR=20.1
904 226 ▶ unless ((scalar(@_) % 2) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar(@_) % 2 == 0) 0 226 226
905 0 ▶ croak __methname," does not accept an {OPTIONS} hash here"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 0 0
906 if (ref($_[0]) eq "HASH");
907 0 confess "In call to ",__methname,
908 " : uneven arg count, expecting key => value pairs"
909 }
910 226 foreach (pairs @_) {
911 135 my $key = $_->[0];
912 135 ▶ confess "In call to ",__methname," the key '$key' looks suspicious"
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^\w+$/u 0 135 135
913 unless $key =~ /^\w+$/;
914 }
915 @_
916 226 }
917  
918 10 ▶sub _check_rx {
Subroutine _check_rx: called CC=5 SCAR=17.5
919 10 my ($self, $rx, $one_past_end_ok) = @_;
920 10 ▶ confess __methname.": Illegal rx ",vis($rx),"\n"
Branch: 1 branch
BranchTrueFalseTotal
unless ($rx // "") =~ /^\d+$/u 0 10 10
Condition50%1/2
$rx // ""
l!l
100
MC/DC0%0/1
$rx // ""
$rx
Truth table50%1/2
A
A= $rx
Aresult
11
00
921 unless ($rx//"") =~ /^\d+$/; # non-negative integer
922 10 my $maxrx = $#{$$self->{rows}};
923 10 ▶ confess __methname.": rx ".vis($rx)." is beyond the last row\n"
Branch: 2 branches
BranchTrueFalseTotal
if $rx > ($one_past_end_ok ? $maxrx + 1 : $maxrx) 0 10 10
$one_past_end_ok ? : 10 0 10
924 .dvis(' $$self')
925 if $rx > ($one_past_end_ok ? ($maxrx+1) : $maxrx);
926 }
927  
928 # Diagnose scalar context if there are no results.
929 292 ▶sub __first_ifnot_wantarray(@) {
Subroutine __first_ifnot_wantarray: called CC=4 SCAR=16.1
930 292 my $wantarray = (caller(1))[5];
931 292 ▶ return @_ if $wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $wantarray 3 289 292
932 289 ▶ return $_[0] if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 289 0 289
933 0 ▶ croak __methname, " called in scalar context but that method does not return a result.\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $wantarray 0 0 0
934 if defined($wantarray);
935 }
936 1 ▶sub __validate_not_scalar_context(@) {
Subroutine __validate_not_scalar_context: called CC=3 SCAR=12.5
937 1 my $wantarray = (caller(1))[5];
938 1 ▶ croak __methname, " returns an array, not a scalar"
Branch: 1 branch
BranchTrueFalseTotal
unless $wantarray or not defined $wantarray 0 1 1
Condition33%1/3
$wantarray or not defined $wantarray
l!l&&r!l&&!r
010
MC/DC0%0/2
$wantarray or not defined $wantarray
$wantarray not defined $wantarray
Truth table33%1/3
A or not(B)
A= $wantarrayB= not defined $wantarray
ABresult
1X1
011
000
939 unless $wantarray || !defined($wantarray);
940 @_
941 1 }
942  
943 0 ▶sub _carponce { # if not silent
Subroutine _carponce: not called CC=5 SCAR=34.0
944 0 my $self = shift;
945 0 my $msg = join "",@_;
946 0 ▶ return if $$self->{_carponce}->{$msg}++;
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"_carponce"}{$msg}++ 0 0 0
947 0 ▶ $msg .= "\n" unless $msg =~ /\n\z/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg =~ /\n\z/su 0 0 0
948 0 ▶ unless ($$self->{silent}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$$self{"silent"}) 0 0 0
949 0 ▶ if ($$self->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$$self{'debug'}) { } 0 0 0
950 0 Carp::cluck($msg)
951 } else {
952 0 carp($msg)
953 }
954 } # else never appears even if silent is later unset!
955 }
956  
957 ###################### METHODS/FUNCTIONS #######################
958  
959 # Unlike other methods, new() takes key => value pair arguments.
960 # For consistency with other methods an initial {OPTIONS} hash is
961 # also allowed, but it is not special in any way and is merged
962 # with any linear args (linear args override {OPTIONS}).
963  
964 103 ▶sub new { # Strictly OO, this does not affect caller's "current sheet".
Subroutine new: called CC=13 SCAR=26.5
Pod documented
965 # The corresponding functional API is new_sheet() which explicitly
966 # creates a new sheet and makes it the 'current sheet'.
967 103 my $classname = shift;
968 103 ▶ croak "Invalid/missing CLASSNAME (i.e. \"this\") arg"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $classname and $classname =~ /^$packname_re$/u 0 103 103
Condition33%1/3
defined $classname and $classname =~ /^$packname_re$/u
!ll&&!rl&&r
00103
MC/DC0%0/2
defined $classname and $classname =~ /^$packname_re$/u
defined $classname $classname =~ /^$packname_re$/u
Truth table33%1/3
A and B
A= defined $classnameB= $classname =~ /^$packname_re$/u
ABresult
0X0
100
111
969 unless defined($classname) && $classname =~ /^$packname_re$/;
970  
971 103 my $opthash = &__opthash;
972 # Special handling of {cmd_nesting) since there was no object to begin with:
973 # Internal callers may pass this as a "user" option in {OPTARGS};
974 # we won't log it, but we plant it into the object below.
975 # **THE CALLER MUST DECREMENT IT LATER IF NEEDED*
976 103 ▶ my $cmd_nesting = delete($opthash->{cmd_nesting}) // 0;
Condition50%1/2
delete $$opthash{"cmd_nesting"} // 0
l!l
0103
MC/DC0%0/1
delete $$opthash{"cmd_nesting"} // 0
delete $$opthash{"cmd_nesting"}
Truth table50%1/2
A
A= delete $$opthash{"cmd_nesting"}
Aresult
11
00
977  
978 103 my %opts = (verbose => $Verbose, debug => $Debug, silent => $Silent,
979 %$opthash,
980 __validate_pairs(@_));
981  
982 103 my $self;
983 103 ▶ if (my $clonee = delete $opts{clone}) { # untested as of 2/12/14
Branch: 1 branch
BranchTrueFalseTotal
if (my $clonee = delete $opts{'clone'}) { } 2 101 103
984 2 delete @opts{qw/verbose debug silent/};
985 2 ▶ croak "Other options not allowed with 'clone': ",hvis(%opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 2 2
986 2 require Clone;
987 2 $self = Clone::clone($clonee); # in all its glory
988 $$self->{data_source} = (delete $opts{data_source})
989 2 ▶ // "cloned from $$self->{data_source}";
Condition50%1/2
delete $opts{"data_source"} // qq[cloned from $$$self{"data_source"}]
l!l
02
MC/DC0%0/1
delete $opts{"data_source"} // qq[cloned from $$$self{"data_source"}]
delete $opts{"data_source"}
Truth table50%1/2
A
A= delete $opts{"data_source"}
Aresult
11
00
990 } else {
991 my $hash = {
992 attributes => delete $opts{attributes} // {},
993 linenums => delete $opts{linenums} // [],
994 meta_info => delete $opts{meta_info} // [], ##### ???? obsolete ???
995 data_source => delete $opts{data_source}, # // "(none)",
996 num_cols => delete $opts{num_cols}, # possibly undef
997  
998 # %colx maps titles, aliases (automatic and user-defined), and
999 # spreadsheet column lettercodes to the corresponding column indicies.
1000 101 ▶ colx => {},
Condition66%4/6
delete $opts{"attributes"} // {}
l!l
0101
delete $opts{"linenums"} // []
l!l
299
delete $opts{"meta_info"} // []
l!l
0101
MC/DC0%0/1
delete $opts{"attributes"} // {}
delete $opts{"attributes"}
MC/DC100%1/1
delete $opts{"linenums"} // []
delete $opts{"linenums"}
MC/DC0%0/1
delete $opts{"meta_info"} // []
delete $opts{"meta_info"}
Truth table50%1/2
A
A= delete $opts{"attributes"}
Aresult
11
00
Truth table100%2/2
A
A= delete $opts{"linenums"}
Aresult
11
00
Truth table50%1/2
A
A= delete $opts{"meta_info"}
Aresult
11
00
1001 colx_desc => {}, # for use in error messages
1002 useraliases => {}, # key exists for user-defined alias names
1003  
1004 title_rx => undef,
1005 first_data_rx => undef,
1006 last_data_rx => undef,
1007 current_rx => undef, # valid during apply()
1008  
1009 pkg2tiedvarnames => {},
1010 pkg2tieall => {},
1011 };
1012  
1013 # We can not use $hash directly as the object representation because %{}
1014 # is overloaded, so we use a scalar ref (pointing to the hashref)
1015 # as the object.
1016 101 $self = bless \$hash, $classname;
1017  
1018 # Create a tied virtual array which creates Magicrows when assigned to.
1019 101 my @rows; tie @rows, 'Spreadsheet::Edit::RowsTie', $self;
1020 101 $hash->{rows} = \@rows;
1021  
1022 101 ▶ if (my $newdata = delete $opts{rows}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $newdata = delete $opts{"rows"}) 88 13 101
1023 88 foreach (@$newdata) {
1024 632 push @rows, $_;
1025 }
1026 }
1027 }# not cloning
1028  
1029 103 $$self->{cmd_nesting} = $cmd_nesting;
1030  
1031 103 $self->_set_verbose_debug_silent(%opts); # croaks if other keys remain
1032  
1033 # Validate data, default num_cols, pad rows, etc.
1034 103 $self->_rows_replaced();
1035  
1036 log_call [$opthash,@_], [\fmt_sheet($self)]
1037 103 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 103 103
1038  
1039 103 $self
1040 }#new
1041  
1042 use overload
1043 # As an ARRAYref, a sheet acts like \@rows which is (a ref to) a
1044 # virtual array of Magicrow objects, each of which is a dual array/hash
1045 # ref to cells in a given row (via RowsTie).
1046 4049 ▶ '@{}' => sub { my $hash = ${ shift() }; $hash->{rows}; },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
1047  
1048 # As a HASHref, a sheet acts like \%crow which is (a ref to)
1049 # the hash view of the current row during 'apply'
1050 4015 ▶ '%{}' => sub { my $self = shift;
Subroutine __ANON__: called CC=1 SCAR=0.0
1051 # probably less efficient but avoids repeating code
1052 4015 \%{ $self->crow() };
1053 },
1054 #'""' => sub { shift },
1055 #'0+' => sub { shift },
1056 #'==' => sub { my ($self, $other, $swap) = @_; $self == $other },
1057 #'eq' => sub { my ($self, $other, $swap) = @_; "$self" eq "$other" },
1058 11 fallback => 1,
1059 11 ;
1060  
1061 172 ▶sub _rows_replaced { # completely new or replaced rows, linenums, etc.
Subroutine _rows_replaced: called CC=18 SCAR=30.6
1062 172 my ($self) = @_;
1063 172 my $hash = $$self;
1064  
1065 my ($rows, $linenums, $num_cols, $current_rx)
1066 172 = @$hash{qw/rows linenums num_cols current_rx/};
1067  
1068 172 ▶ croak "Can not replace sheet content during an apply!\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $current_rx 0 172 172
1069 if defined $current_rx;
1070 172 for my $rx (0..$#$rows) {
1071 1224 my $row = $rows->[$rx];
1072 1224 ▶ croak "rows must contain refs to arrays of cells (row $rx is $row)"
Branch: 1 branch
BranchTrueFalseTotal
unless &__looks_like_aref($row) 0 1224 1224
1073 unless __looks_like_aref($row);
1074 1224 for my $cx (0..$#$row) {
1075 5645 ▶ croak "New cell at Row ",$rx+1," Column ",cx2let($cx)," contains a ref"
Branch: 1 branch
BranchTrueFalseTotal
if ref $$row[$cx] 0 5645 5645
1076 if ref($row->[$cx]);
1077 }
1078 }
1079 croak '\'linenums\' if present must be (a ref to) an array of numbers, ',
1080 ' "" or "???"', ivis('\nnot $linenums\n')
1081 unless ref($linenums) eq "ARRAY"
1082 172 ▶ && all{ defined() and looks_like_number($_) || !/[^\?]/ } @$linenums;
Subroutine __ANON__: called CC=3 SCAR=13.6
Branch: 2 branches
BranchTrueFalseTotal
unless ref $linenums eq "ARRAY" and &all(sub { defined $_ and &looks_like_number($_) || !/[^\?]/u; } , @$linenums) 0 172 172
defined $_ and &looks_like_number($_) || !/[^\?]/u 603 0 603
Condition44%4/9
ref $linenums eq "ARRAY" and &all(sub { defined $_ and &looks_like_number($_) || !/[^\?]/u; } , @$linenums)
!ll&&!rl&&r
00172
defined $_ && (&looks_like_number($_) || !/[^\?]/u)
!ll&&!rl&&r
00603
&looks_like_number($_) or not /[^\?]/u
l!l&&r!l&&!r
588150
MC/DC0%0/2
ref $linenums eq "ARRAY" and &all(sub { defined $_ and &looks_like_number($_) || !/[^\?]/u; } , @$linenums)
ref $linenums eq "ARRAY" &all(sub { defined $_ and &looks_like_number($_) || !/[^\?]/u; } , @$linenums)
MC/DC0%0/3
defined $_ && (&looks_like_number($_) || !/[^\?]/u)
defined $_ &looks_like_number($_) not /[^\?]/u
Truth table33%1/3
A and B
A= ref $linenums eq "ARRAY"B= &all(sub { defined $_ and &looks_like_number($_) || !/[^\?]/u; } , @$linenums)
ABresult
0X0
100
111
Truth table50%2/4
A && B or not(C)
A= defined $_B= &looks_like_number($_)C= not /[^\?]/u
ABCresult
0XX0
1000
11X1
1011
1083  
1084 172 ▶ if (@$rows) { # Determine num_cols and pad short rows
Branch: 1 branch
BranchTrueFalseTotal
if (@$rows) { } 144 28 172
1085 144 my $nc = 0;
1086 144 ▶ foreach (@$rows) { $nc = @$_ if @$_ > $nc }
Branch: 1 branch
BranchTrueFalseTotal
if @$_ > $nc 217 1007 1224
1087 144 ▶ if ($num_cols && $num_cols != $nc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($num_cols and $num_cols != $nc) { } 0 144 144
Condition66%2/3
$num_cols and $num_cols != $nc
!ll&&!rl&&r
14220
MC/DC0%0/2
$num_cols and $num_cols != $nc
$num_cols $num_cols != $nc
Truth table66%2/3
A and B
A= $num_colsB= $num_cols != $nc
ABresult
0X0
100
111
1088 0 croak "num_cols=$num_cols was specified along with initial data, but\n",
1089 "the value doesn't match the data (which has up to $nc columns)\n"
1090 } else {
1091 144 $hash->{num_cols} = $num_cols = $nc;
1092 }
1093 # Pad short rows with empty fields
1094 144 foreach my $row (@$rows) {
1095 1224 push @$row, (("") x ($num_cols - @$row));
1096 }
1097 144 $#$linenums = $#$rows;
1098 144 ▶ foreach (@$linenums) { $_ //= '???' };
Condition100%2/2
$_ //= "???"
l!l
603621
MC/DC100%1/1
$_ //= "???"
$_
Truth table100%2/2
A
A= $_
Aresult
11
00
1099 } else {
1100 # There is no data. Default num_cols to zero, but leave any
1101 # user-supplied value so a subsequent insert_rows() will know how
1102 # many columns to create.
1103 28 ▶ $hash->{num_cols} //= 0;
Condition100%2/2
$$hash{"num_cols"} //= 0
l!l
325
MC/DC100%1/1
$$hash{"num_cols"} //= 0
$$hash{"num_cols"}
Truth table100%2/2
A
A= $$hash{"num_cols"}
Aresult
11
00
1104 }
1105 #oops unless $hash->{data_source};
1106 172 ▶ croak "#linenums ($#$linenums) != #rows ($#$rows)\n",
Branch: 1 branch
BranchTrueFalseTotal
unless @$linenums == @$rows 0 172 172
1107 dvis '$hash->{linenums}\n$hash->{rows}'
1108 unless @$linenums == @$rows;
1109  
1110 172 $hash->{title_rx} = undef;
1111 172 $hash->{first_data_rx} = undef;
1112 172 $hash->{last_data_rx} = undef;
1113 172 $hash->{useraliases} = {};
1114 172 $self->_rebuild_colx; # Set up colx colx_desc
1115 172 $self
1116 }#_rows_replaced
1117  
1118 #########################################################
1119 # Combination FUNCTION/METHOD
1120 # These are declared with signatures for use as functional-API FUNCTIONs
1121 # which use the caller's "current sheet" as the implicit object.
1122 #
1123 # However they may also be called as METHODs with an explicit object.
1124 #########################################################
1125  
1126 # Allow user to find out names of tied variables
1127 0 ▶sub tied_varnames(;@) {
Subroutine tied_varnames: not called CC=3 SCAR=24.8
Pod undocumented
1128 0 my ($self, $opts) = &__selfmust_opthash;
1129 0 ▶ my $pkg = $opts->{package} // __callerpkg();
Condition0%0/3
$$opts{"package"} // &__callerpkg()
l!l&&r!l&&!r
000
MC/DC0%0/2
$$opts{"package"} // &__callerpkg()
$$opts{"package"} &__callerpkg()
Truth table0%0/3
A // B
A= $$opts{"package"}B= &__callerpkg()
ABresult
1X1
011
000
1130 0 ▶ my $h = $$self->{pkg2tiedvarnames}->{$pkg} //= {};
Condition0%0/2
$$$self{"pkg2tiedvarnames"}{$pkg} //= {}
l!l
00
MC/DC0%0/1
$$$self{"pkg2tiedvarnames"}{$pkg} //= {}
$$$self{"pkg2tiedvarnames"}{$pkg}
Truth table0%0/2
A
A= $$$self{"pkg2tiedvarnames"}{$pkg}
Aresult
11
00
1131 0 return keys %$h;
1132 }
1133  
1134 # Internal: Tie specified variables into a package if not already tied.
1135 # Returns:
1136 sub __TCV_REDUNDANT() { 1 } # if all were already tied
1137 sub __TCV_OK() { 2 } # otherwise (some tied, or no idents specified)
1138 #
1139 86 ▶sub _tie_col_vars {
Subroutine _tie_col_vars: called CC=13 SCAR=25.7
1140 86 my $self = shift;
1141 86 my $pkg = shift;
1142 86 my $parms = shift;
1143 # Remaining arguments are idents
1144  
1145 86 my ($file, $lno) = @$parms;
1146 86 ▶ my @safecheck_pkgs = $pkg eq "main" ? ($pkg) : ($pkg, "main");
Branch: 1 branch
BranchTrueFalseTotal
$pkg eq 'main' ? : 83 3 86
1147  
1148 my ($colx, $colx_desc, $debug, $silent)
1149 86 = @$$self{qw/colx colx_desc debug silent/};
1150  
1151 # FIXME: BUG/ISSUE ...
1152 # Why is it correct to keep tiedvarnames PER-SHEET ?
1153 # Isn't this a global property of each package?
1154  
1155 86 ▶ my $tiedvarnames = ($$self->{pkg2tiedvarnames}->{$pkg} //= {});
Condition100%2/2
$$$self{"pkg2tiedvarnames"}{$pkg} //= {}
l!l
788
MC/DC100%1/1
$$$self{"pkg2tiedvarnames"}{$pkg} //= {}
$$$self{"pkg2tiedvarnames"}{$pkg}
Truth table100%2/2
A
A= $$$self{"pkg2tiedvarnames"}{$pkg}
Aresult
11
00
1156  
1157 86 ▶ if (@_ > 0 && keys %$tiedvarnames) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0 and keys %$tiedvarnames) 77 9 86
Condition100%3/3
@_ > 0 and keys %$tiedvarnames
!ll&&!rl&&r
1877
MC/DC100%2/2
@_ > 0 and keys %$tiedvarnames
@_ > 0 keys %$tiedvarnames
Truth table100%3/3
A and B
A= @_ > 0B= keys %$tiedvarnames
ABresult
0X0
100
111
1158 SHORTCUT: {
1159 77 foreach (@_) {
1160 2044 ▶ last SHORTCUT unless exists $tiedvarnames->{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$tiedvarnames{$_} 38 2006 2044
1161 }
1162 39 return __TCV_REDUNDANT;
1163 }
1164 }
1165  
1166 VAR:
1167 47 foreach (sort {$a->[0] <=> $b->[0]} # sort for ease of debugging
1168 map {
1169 1360 my $cx = $colx->{$_};
1170 defined($cx)
1171 1360 ▶ ? [ $cx, $_, $colx_desc->{$_} ]
Branch: 1 branch
BranchTrueFalseTotal
defined $cx ? : 1351 9 1360
1172 : [ 999999, $_, "(currently NOT DEFINED)" ];
1173 } @_
1174 )
1175 {
1176 1360 my ($cx, $ident, $desc) = @$_;
1177 1360 ▶ oops(dvis '$cx $ident $desc') unless $ident =~ /^$identifier_re$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $ident =~ /^$identifier_re$/u 0 1360 1360
1178  
1179 1360 ▶ if (exists $tiedvarnames->{$ident}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$tiedvarnames{$ident}) 1228 132 1360
1180 1228 ▶ $self->_log(" Previously tied: \$${pkg}::${ident}\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1228 1228
1181 next
1182 1228 }
1183  
1184 132 ▶ $self->_log("tie \$${pkg}::${ident} to $desc\n") if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 132 132
1185  
1186 132 $tiedvarnames->{$ident} = 1;
1187  
1188 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1189 132 *{"$pkg\::$ident"} = \${ *{gensym()} };
1190  
1191 132 tie ${"${pkg}::$ident"}, 'Tie::Indirect::Scalar',
1192 \&_tiecell_helper, $pkg, $ident;
1193 }
1194 47 return __TCV_OK;
1195 }
1196 2394 ▶sub _tiecell_helper {
Subroutine _tiecell_helper: called CC=2 SCAR=6.9
1197 2394 my($mutating, $pkg, $ident) = @_;
1198 2394 ▶ my $sheet = $pkg2currsheet{$pkg}
Condition100%2/2
$Spreadsheet::Edit::pkg2currsheet{$pkg} // croak("No sheet is currently valid for package $pkg\n")
l!l
23931
MC/DC100%1/1
$Spreadsheet::Edit::pkg2currsheet{$pkg} // croak("No sheet is currently valid for package $pkg\n")
$Spreadsheet::Edit::pkg2currsheet{$pkg}
Truth table100%2/2
A
A= $Spreadsheet::Edit::pkg2currsheet{$pkg}
Aresult
11
00
1199 // croak "No sheet is currently valid for package $pkg\n";
1200 2393 $sheet->_onlyinapply("tied variable \$$ident");
1201  
1202 # WRONG... it croaks bc sheet->{rows}->[rx] is a rowhash which doesn't like undef keys
1203 # WRONG: This returns \undef if $ident is not currently valid
1204 2390 \( $$sheet->{rows}->[$$sheet->{current_rx}]->{$ident} )
1205 }
1206  
1207 82 ▶sub _all_valid_idents {
Subroutine _all_valid_idents: called CC=3 SCAR=11.4
1208 82 my $self = shift;
1209 82 my $num_cols = $$self->{num_cols};
1210 82 my %valid_idents;
1211 82 foreach (keys %{ $$self->{colx} }) {
1212 3314 ▶ if (__is_unindexed_title($_, $num_cols)) {
Branch: 1 branch
BranchTrueFalseTotal
if (&__is_unindexed_title($_, $num_cols)) 0 3314 3314
1213 0 $self->_carponce("WARNING: Column key ",visq($_)," conflicts with a Perl built-in; variable will not be tied.\n");
1214 0 next;
1215 }
1216 3314 $valid_idents{ title2ident($_) } = 1;
1217 }
1218 82 return keys %valid_idents;
1219 }
1220  
1221 9 ▶sub tie_column_vars(;@) {
Subroutine tie_column_vars: called CC=14 SCAR=27.9
Pod documented
1222 9 my ($self, $opts) = &__self_opthash;
1223 # Any remaining args are :all or variable names matching
1224 # alias names, either user-defined or automatic.
1225  
1226 9 ▶ local $$self->{silent} = $opts->{silent} // $$self->{silent};
Condition66%2/3
$$opts{"silent"} // $$$self{"silent"}
l!l&&r!l&&!r
027
MC/DC50%1/2
$$opts{"silent"} // $$$self{"silent"}
$$opts{"silent"} $$$self{"silent"}
Truth table66%2/3
A // B
A= $$opts{"silent"}B= $$$self{"silent"}
ABresult
1X1
011
000
1227 9 ▶ local $$self->{verbose} = $opts->{verbose} // $$self->{verbose};
Condition33%1/3
$$opts{"verbose"} // $$$self{"verbose"}
l!l&&r!l&&!r
009
MC/DC0%0/2
$$opts{"verbose"} // $$$self{"verbose"}
$$opts{"verbose"} $$$self{"verbose"}
Truth table33%1/3
A // B
A= $$opts{"verbose"}B= $$$self{"verbose"}
ABresult
1X1
011
000
1228 9 ▶ local $$self->{debug} = $opts->{debug} // $$self->{debug};
Condition33%1/3
$$opts{"debug"} // $$$self{"debug"}
l!l&&r!l&&!r
009
MC/DC0%0/2
$$opts{"debug"} // $$$self{"debug"}
$$opts{"debug"} $$$self{"debug"}
Truth table33%1/3
A // B
A= $$opts{"debug"}B= $$$self{"debug"}
ABresult
1X1
011
000
1229  
1230 9 ▶ my $pkg = $opts->{package} // __callerpkg();
Condition66%2/3
$$opts{"package"} // &__callerpkg()
l!l&&r!l&&!r
270
MC/DC0%0/2
$$opts{"package"} // &__callerpkg()
$$opts{"package"} &__callerpkg()
Truth table66%2/3
A // B
A= $$opts{"package"}B= &__callerpkg()
ABresult
1X1
011
000
1231  
1232 9 my (%tokens, @varnames);
1233 9 ▶ foreach (@_) { if (/:/) { $tokens{$_} = 1 } else { push @varnames, $_ } }
Branch: 1 branch
BranchTrueFalseTotal
if (/:/u) { } 5 12 17
1234 9 foreach (@varnames) {
1235 12 s/^\$//;
1236 12 ▶ croak "Invalid variable name '$_'\n" unless /^$identifier_re$/;
Branch: 1 branch
BranchTrueFalseTotal
unless /^$identifier_re$/u 0 12 12
1237 }
1238  
1239 # With ':all' tie all possible variables, now and in the future.
1240 9 my ($file, $lno) = __fn_ln_methname();
1241 9 my $parms = [$file, $lno];
1242  
1243 # Why? Obsolete? Only for :all?? [note added Dec22]
1244 # FIXME TODO: is this bogus?
1245 9 ▶ $self->title_rx($opts->{title_rx}) if exists $opts->{title_rx};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$opts{"title_rx"} 0 9 9
1246  
1247 9 ▶ if (delete $tokens{':all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (delete $tokens{":all"}) 5 4 9
1248 # Remember parameters for tie operations which might occur later
1249 5 $$self->{pkg2tieall}->{$pkg} = $parms;
1250  
1251 5 push @varnames, sort $self->_all_valid_idents;
1252 }
1253 9 ▶ croak "Unrecognized token in arguments: ",avis(keys %tokens) if %tokens;
Branch: 1 branch
BranchTrueFalseTotal
if %tokens 0 9 9
1254  
1255 9 my $r = $self->_tie_col_vars($pkg, $parms, @varnames);
1256  
1257 9 ▶ my $pfx = ($r == __TCV_REDUNDANT ? "[ALL REDUNDANT] " : "");
Branch: 1 branch
BranchTrueFalseTotal
$r == 1 ? : 0 9 9
1258 log_methcall $self, [\$pfx, \__fmt_uqarraywithqw(keys %tokens, @varnames),
1259 \" in package $pkg"]
1260 9 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 9 9
1261 }#tie_column_vars
1262  
1263 #
1264 # Accessors for misc. sheet data
1265 #
1266  
1267 328 ▶sub attributes(@) { ${&__selfonly}->{attributes} }
Subroutine attributes: called CC=1 SCAR=0.0
Pod documented
1268 4016 ▶sub colx() { ${&__selfmustonly}->{colx} }
Subroutine colx: called CC=1 SCAR=0.0
Pod documented
1269 0 ▶sub colx_desc() { ${&__selfmustonly}->{colx_desc} }
Subroutine colx_desc: not called CC=1 SCAR=6.9
Pod documented
1270 5 ▶sub data_source(;$) {
Subroutine data_source: called CC=5 SCAR=24.1
Pod documented
1271 5 my $self = &__selfmust;
1272 5 ▶ if (@_ == 0) { # 'get' request
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 5 0 5
1273 5 ▶ log_methcall $self, [], [$$self->{data_source}] if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 5 5
1274 return $$self->{data_source}
1275 5 }
1276 0 ▶ log_methcall $self, [@_] if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 0 0
1277 0 ▶ croak "Too many args" unless @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 0 0 0
1278 0 $$self->{data_source} = $_[0];
1279 0 $self
1280 }
1281 1 ▶sub linenums() { ${&__selfmustonly}->{linenums} }
Subroutine linenums: called CC=1 SCAR=0.0
Pod documented
1282 1 ▶sub num_cols() { ${&__selfmustonly}->{num_cols} }
Subroutine num_cols: called CC=1 SCAR=0.0
Pod documented
1283 4204 ▶sub rows() { ${&__selfmustonly}->{rows} }
Subroutine rows: called CC=1 SCAR=0.0
Pod documented
1284 0 ▶sub sheetname() { ${&__selfmustonly}->{sheetname} }
Subroutine sheetname: not called CC=1 SCAR=6.9
Pod documented
1285  
1286 0 ▶sub iolayers() { ${&__selfmustonly}->{iolayers} }
Subroutine iolayers: not called CC=1 SCAR=6.9
Pod undocumented
1287 0 ▶sub meta_info() {${&__selfmustonly}->{meta_info} }
Subroutine meta_info: not called CC=1 SCAR=6.9
Pod undocumented
1288 1 ▶sub input_encoding() {
Subroutine input_encoding: called CC=3 SCAR=13.5
Pod undocumented
1289 # Emulate old API. We actually store input_iolayers instead now,
1290 # so as to include :crlf if necessary.
1291 1 my $self = &__selfmustonly;
1292 1 local $_;
1293 return undef unless
1294 1 exists(${$self}->{input_iolayers})
1295 0 ▶ && ${$self}->{input_iolayers} =~ /encoding\(([^()]*)\)/;
Branch: 1 branch
BranchTrueFalseTotal
unless exists $${$self;}{"input_iolayers"} and $${$self;}{"input_iolayers"} =~ /encoding\(([^()]*)\)/u 1 0 1
Condition33%1/3
exists $${$self;}{"input_iolayers"} and $${$self;}{"input_iolayers"} =~ /encoding\(([^()]*)\)/u
!ll&&!rl&&r
100
MC/DC0%0/2
exists $${$self;}{"input_iolayers"} and $${$self;}{"input_iolayers"} =~ /encoding\(([^()]*)\)/u
exists $${$self;}{"input_iolayers"} $${$self;}{"input_iolayers"} =~ /encoding\(([^()]*)\)/u
Truth table33%1/3
A and B
A= exists $${$self;}{"input_iolayers"}B= $${$self;}{"input_iolayers"} =~ /encoding\(([^()]*)\)/u
ABresult
0X0
100
111
1296 0 return $1;
1297 }
1298  
1299 # See below for title_rx()
1300 13 ▶sub title_row() {
Subroutine title_row: called CC=3 SCAR=11.3
Pod documented
1301 13 my $self = &__selfmustonly;
1302 13 my $title_rx = $$self->{title_rx};
1303 13 ▶ my $r = defined($title_rx) ? $$self->{rows}->[$title_rx] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $title_rx ? : 13 0 13
1304 13 ▶ log_methcall $self, [], [$r] if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 13 13
1305 13 $r
1306 }
1307 138 ▶sub rx() { ${ &__selfmustonly }->{current_rx} }
Subroutine rx: called CC=1 SCAR=0.0
Pod documented
1308 4071 ▶sub crow() {
Subroutine crow: called CC=1 SCAR=0.0
Pod documented
1309 4071 my $self = &__selfmustonly;
1310 4071 ${ $self->_onlyinapply("crow() method") }->{rows}->[$$self->{current_rx}]
1311 }
1312 0 ▶sub linenum() {
Subroutine linenum: not called CC=2 SCAR=17.9
Pod documented
1313 0 my $self = &__selfmustonly;
1314 0 ▶ my $current_rx = $$self->{current_rx} // return(undef);
Condition0%0/2
$$$self{"current_rx"} // (return undef)
l!l
00
MC/DC0%0/1
$$$self{"current_rx"} // (return undef)
$$$self{"current_rx"}
Truth table0%0/2
A
A= $$$self{"current_rx"}
Aresult
11
00
1315 0 $$self->{linenums}->[$current_rx];
1316 }
1317 0 ▶sub _getref {
Subroutine _getref: not called CC=6 SCAR=37.4
1318 0 my ($self, $rx, $ident) = @_;
1319 0 my ($rows, $colx) = @$$self{qw/rows colx/};
1320 0 ▶ croak "get/set: rx $rx is out of range" if $rx < 0 || $rx > $#$rows;
Branch: 1 branch
BranchTrueFalseTotal
if $rx < 0 or $rx > $rows->$#* 0 0 0
Condition0%0/3
$rx < 0 or $rx > $rows->$#*
l!l&&r!l&&!r
000
MC/DC0%0/2
$rx < 0 or $rx > $rows->$#*
$rx < 0 $rx > $rows->$#*
Truth table0%0/3
A or B
A= $rx < 0B= $rx > $rows->$#*
ABresult
1X1
011
000
1321 0 my $row = $$self->{rows}->[$rx];
1322 0 my $cx = $colx->{$ident};
1323 0 ▶ oops("Invalid cx ".vis($cx)) if ! defined($cx) || $cx < 0 || $cx > $#$row;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $cx or $cx < 0 or $cx > $row->$#* 0 0 0
Condition0%0/6
not defined $cx or $cx < 0 or $cx > $row->$#*
l!l&&r!l&&!r
000
not defined $cx or $cx < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $cx or $cx < 0 or $cx > $row->$#*
not defined $cx $cx < 0 $cx > $row->$#*
Truth table0%0/4
not(A) or B or C
A= not defined $cxB= $cx < 0C= $cx > $row->$#*
ABCresult
1XX1
01X1
0011
0000
1324 0 \$row->[$cx];
1325 }
1326 # get/set a cell given by (rx,COLSPEC)
1327 0 ▶sub get($$) {
Subroutine get: not called CC=1 SCAR=6.9
Pod documented
1328 0 my $self = &__selfmust;
1329 0 my $ref = $self->_getref(@_);
1330 0 $$ref;
1331 }
1332 0 ▶sub set($$$) {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
1333 0 my $self = &__selfmust;
1334 0 my ($rx, $colspec, $newval) = @_;
1335 0 my $ref = $self->_getref($rx, $colspec);
1336 0 $$ref = $newval;
1337 0 $self
1338 }
1339  
1340 23 ▶sub __validate_sheet_arg($) {
Subroutine __validate_sheet_arg: called CC=4 SCAR=16.4
1341 23 my $sheet = shift;
1342 0 ▶ croak "Argument '${\u($sheet)}' is not a Spreadsheet::Edit sheet object"
Branch: 1 branch
BranchTrueFalseTotal
if defined $sheet and not builtin::blessed($sheet) && $sheet->isa("Spreadsheet::Edit") 0 23 23
Condition50%3/6
defined $sheet and not builtin::blessed($sheet) && $sheet->isa("Spreadsheet::Edit")
!ll&&!rl&&r
7160
builtin::blessed($sheet) && $sheet->isa('Spreadsheet::Edit')
!ll&&!rl&&r
0016
MC/DC0%0/3
defined $sheet and not builtin::blessed($sheet) && $sheet->isa("Spreadsheet::Edit")
defined $sheet builtin::blessed($sheet) $sheet->isa('Spreadsheet::Edit')
Truth table50%2/4
A and not(B && C)
A= defined $sheetB= builtin::blessed($sheet)C= $sheet->isa('Spreadsheet::Edit')
ABCresult
0XX0
1110
10X1
1101
1343 if defined($sheet) and
1344 !blessed($sheet) || !$sheet->isa("Spreadsheet::Edit");
1345 23 $sheet;
1346 }
1347  
1348 #---- logging ----------------------------------
1349  
1350 # Print segmented log messages:
1351 # Join args together, prefixing with "> " or ">> " etc.
1352 # unless the previous call did not end with newline.
1353 # Maintains internal state. A final call with an ending \n must occur.
1354 0 ▶sub _log {
Subroutine _log: not called CC=3 SCAR=24.8
1355 0 my $self = shift;
1356 0 state $in_midst;
1357 print STDERR join "",
1358 ($in_midst ? "" : (">" x ($$self->{cmd_nesting}||1))),
1359 0 ▶ map{u} @_;
Branch: 1 branch
BranchTrueFalseTotal
$in_midst ? : 0 0 0
Condition0%0/2
$$$self{'cmd_nesting'} || 1
l!l
00
MC/DC0%0/1
$$$self{'cmd_nesting'} || 1
$$$self{'cmd_nesting'}
Truth table0%0/2
A
A= $$$self{'cmd_nesting'}
Aresult
11
00
1360 0 $in_midst = ($_[$#_] !~ /\n\z/s);
1361 }
1362  
1363 11 my $trunclen = 200;
1364 25 ▶sub fmt_sheet($) {
Subroutine fmt_sheet: called CC=5 SCAR=18.1
Pod undocumented
1365 25 my $sheet = shift;
1366 25 ▶ return "undef" unless defined($sheet);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sheet 0 25 25
1367 #oops unless ref($sheet) ne "" && $sheet->isa(__PACKAGE__);
1368 25 local $$sheet->{verbose} = 0;
1369 25 ▶ my $desc = $$sheet->{data_source} || $$sheet->{sheetname} || "no data_source";
Condition40%2/5
$$$sheet{'data_source'} || $$$sheet{'sheetname'} || 'no data_source'
l!l
250
$$$sheet{"data_source"} or $$$sheet{"sheetname"}
l!l&&r!l&&!r
2500
MC/DC0%0/2
$$$sheet{'data_source'} || $$$sheet{'sheetname'} || 'no data_source'
$$$sheet{"data_source"} $$$sheet{"sheetname"}
Truth table33%1/3
A or B
A= $$$sheet{"data_source"}B= $$$sheet{"sheetname"}
ABresult
1X1
011
000
1370 0 ▶ if (length($desc) > $trunclen) { $desc = substr($desc,($trunclen-3))."..." }
Branch: 1 branch
BranchTrueFalseTotal
if (length $desc > $trunclen) 0 25 25
1371 25 my $r = addrvis($sheet)."($desc)";
1372 25 $r
1373 }
1374  
1375 111 ▶sub _logmethifv { # args: just linearized INPUT items
Subroutine _logmethifv: called CC=2 SCAR=9.2
1376 111 ▶ return unless ${$_[0]}->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $${$_[0];}{"verbose"} 111 0 111
1377 0 my $self = shift;
1378 0 log_methcall $self, [@_];
1379 }
1380  
1381 757 ▶sub _logmethretifv { # args: [INPUTS], [OUTPUTS]
Subroutine _logmethretifv: called CC=3 SCAR=11.0
1382 757 ▶ oops if @_ > 3; # (self, items, retvals)
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 3 0 757 757
1383 757 ▶ return unless ${$_[0]}->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $${$_[0];}{"verbose"} 749 8 757
1384 8 goto &log_methcall
1385 }
1386 #-----------------------------------------------
1387  
1388 971 ▶sub _call_usercode($$$) {
Subroutine _call_usercode: called CC=5 SCAR=19.4
1389 971 my ($self, $code, $cxlist) = @_;
1390  
1391 971 ▶ if (@$cxlist) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cxlist) { } 56 915 971
1392 56 my $row = $self->crow();
1393 56 for ($row->[$cxlist->[0]]) { # bind $_ to the first-specified column
1394 56 eval{ &$code(@$row[@$cxlist]) };
1395 }
1396 } else {
1397 915 eval{ $code->() };
1398 }
1399 971 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 971 971
1400 0 local $_ = $@;
1401 0 ▶ unless ($$self->{debug} #or "TEMP DEBUG"
Branch: 1 branch
BranchTrueFalseTotal
unless ($$$self{"debug"}) 0 0 0
1402 ) {
1403 # Simplify a backtrace to omit our internal frames between the user's
1404 # call to apply* (or join_cols) and the call to the user's callback.
1405 # A typical Carp::confess traceback looks like this:
1406 # user message lines
1407 # ...user frame(s) inside the apply callback
1408 # users_callback_function called at lib/Spreadsheet/Edit.pm line nnn
1409 #
1410 # <user_subname() or pkg::__ANON__() called from ...Spreadsheet/Edit.pm
1411 #
1412 # ### BEGIN portion replaced by '[apply internals]'
1413 #
1414 # eval {...} called at ...Spreadsheet/Edit.pm line... [the above eval]
1415 # Spreadsheet::Edit::_call_usercode(...) called at .../Spreadsheet/Edit.pm line...
1416 # Spreadsheet::Edit::_apply_to_rows(...) called at .../Spreadsheet/Edit.pm line...
1417 # ...possible other internal frame(s)...
1418 #
1419 # ### END portion replaced by '[apply internals]'
1420 #
1421 # Spreadsheet::Edit::public_method(...) called at userfilename line nnn
1422 # [where public_method is apply*, join_cols, etc.]
1423 # outer user frame(s)...
1424 #
1425 # The last internal frame will be the eval{...} above.
1426 0 state $__FILE__ = __FILE__;
1427 0 state $__PACKAGE__ = __PACKAGE__;
1428 #warn "### MMM before: $_\n--{end}--\n";
1429  
1430 #s/^(\h*)eval \{.*?\} called at $__FILE__ line .*?\R(\h*${__PACKAGE__}::[a-z])/${1}[apply internals]\n$2/msg
1431  
1432 0 s/^(?<prespace>\h*)(?<usercallback>\w[\w:]*\([^()]*\)) called at $__FILE__ line .*?\R\h*eval \{.*?\} called at $__FILE__ line (?:.|\R)*?\R(?<outermethod>\h*${__PACKAGE__}::[a-z])/$+{prespace}$+{usercallback} called from [apply internals]\n$+{outermethod}/msg;
1433  
1434 # or warn "!!!\n","(*_call_usercode REGEX DID NOT MATCH*)\n(__FILE__=$__FILE__)\n(__PACKAGE__=$__PACKAGE__)\n$@\n----------\n"
1435 ;
1436 #warn "### MM2 after : $_\n--{end}--\n";
1437 }
1438 0 die "$_\n";
1439 }
1440 }
1441  
1442 # Do apply, handling COLSPEC args.
1443 # If $rxlists or $rxfirst & $rxlast are undef, visit all rows.
1444 565 ▶sub _apply_to_rows($$$;$$$) {
Subroutine _apply_to_rows: called CC=15 SCAR=27.7
1445 565 my ($self, $code, $cxlist, $rxlist, $rxfirst, $rxlast) = @_;
1446 565 my $hash = $$self;
1447 565 my ($linenums,$rows,$num_cols,$cl) = @$hash{qw/linenums rows num_cols/};
1448  
1449 565 ▶ croak $self->_methmsg("Missing or incorrect {code} argument")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code eq "CODE" 0 565 565
1450 unless ref($code) eq "CODE";
1451 565 foreach (@$cxlist) {
1452 35 ▶ if ($_ < 0 || $_ >= $num_cols) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ < 0 or $_ >= $num_cols) 0 35 35
Condition33%1/3
$_ < 0 or $_ >= $num_cols
l!l&&r!l&&!r
0035
MC/DC0%0/2
$_ < 0 or $_ >= $num_cols
$_ < 0 $_ >= $num_cols
Truth table33%1/3
A or B
A= $_ < 0B= $_ >= $num_cols
ABresult
1X1
011
000
1453 0 croak $self->_methmsg(__fmt_cx($_)." is out of range")
1454 }
1455 }
1456  
1457 { # Temp save "current_rx" from an enclosing apply
1458 565 local $hash->{current_rx} = undef;
1459  
1460 # Temp update "current apply sheet" for logmsg()
1461 565 local $_inner_apply_sheet = $self;
1462  
1463 565 ▶ if (defined $rxlist) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rxlist) { } 492 73 565
1464 492 foreach my $rx (@$rxlist) {
1465 638 ▶ croak "rx $rx is out of range"
Branch: 1 branch
BranchTrueFalseTotal
if $rx < 0 or $rx > $rows->$#* 10 628 638
Condition100%3/3
$rx < 0 or $rx > $rows->$#*
l!l&&r!l&&!r
55628
MC/DC100%2/2
$rx < 0 or $rx > $rows->$#*
$rx < 0 $rx > $rows->$#*
Truth table100%3/3
A or B
A= $rx < 0B= $rx > $rows->$#*
ABresult
1X1
011
000
1466 if $rx < 0 || $rx > $#$rows;
1467 628 $hash->{current_rx} = $rx;
1468 628 _call_usercode($self,$code,$cxlist);
1469 }
1470 } else {
1471 # Do not cache $#$rows so user can call insert_rows() or delete_rows()
1472 73 ▶ for (my $rx = $rxfirst // 0;
Branch: 1 branch
BranchTrueFalseTotal
if ($rx <= $rows->$#* and !defined($rxlast) || $rx <= $rxlast) 343 73 416
Condition100%8/8
$rxfirst // 0
l!l
703
$rx <= $rows->$#* and !defined($rxlast) || $rx <= $rxlast
!ll&&!rl&&r
703343
not defined $rxlast or $rx <= $rxlast
l!l&&r!l&&!r
33763
MC/DC100%1/1
$rxfirst // 0
$rxfirst
MC/DC100%3/3
$rx <= $rows->$#* and !defined($rxlast) || $rx <= $rxlast
$rx <= $rows->$#* not defined $rxlast $rx <= $rxlast
Truth table100%2/2
A
A= $rxfirst
Aresult
11
00
Truth table100%4/4
A and not(B) or C
A= $rx <= $rows->$#*B= not defined $rxlastC= $rx <= $rxlast
ABCresult
0XX0
1000
11X1
1011
1473 $rx <= $#$rows && (!defined($rxlast) || $rx <= $rxlast);
1474 $rx++)
1475 {
1476 343 $hash->{current_rx} = $rx;
1477 343 _call_usercode($self,$code,$cxlist);
1478 343 $rx = $hash->{current_rx}; # might have been changed by delete_rows()
1479 }
1480 }
1481 }
1482  
1483 croak "After completing apply, an enclosing apply was resumed, but",
1484 " current_rx=",$hash->{current_rx}," now points beyond the last row!\n"
1485 555 ▶ if defined($hash->{current_rx}) && $hash->{current_rx} > $#$rows;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$hash{"current_rx"} and $$hash{"current_rx"} > $rows->$#* 0 555 555
Condition33%1/3
defined $$hash{"current_rx"} and $$hash{"current_rx"} > $rows->$#*
!ll&&!rl&&r
55500
MC/DC0%0/2
defined $$hash{"current_rx"} and $$hash{"current_rx"} > $rows->$#*
defined $$hash{"current_rx"} $$hash{"current_rx"} > $rows->$#*
Truth table33%1/3
A and B
A= defined $$hash{"current_rx"}B= $$hash{"current_rx"} > $rows->$#*
ABresult
0X0
100
111
1486 555 $self
1487 }#_apply_to_rows
1488  
1489 # Rebuild %colx and %colx_desc, and tie any required new variables.
1490 #
1491 # User-defined column aliases must already be valid in %colx;
1492 # all other entries are deleted and re-created.
1493 #
1494 # Note: the special '^', '$' and numieric cx values (only in 0..num_cols)
1495 # are handled algorithmically in _specs2cxdesclist() before consulting %colx.
1496 #
1497 # When building %colx, conflicts are resolved using these priorities:
1498 #
1499 # User-defined aliases (ALWAYS valid)
1500 # Titles (if unique)
1501 # Trimmed titles (with leading & trailing spaces removed)
1502 # Automatic aliases
1503 # ABC letter-codes
1504 #
1505 # Warnings are issued once for each conflict.
1506 721 ▶sub _rebuild_colx {
Subroutine _rebuild_colx: called CC=12 SCAR=24.9
1507 721 my $self = shift;
1508 721 my $notie = $_[0]; # true during autodetect probing
1509  
1510 my ($silent, $colx, $colx_desc, $useraliases, $num_cols, $title_rx,
1511 $rows, $debug, $pkg2tieall)
1512 721 = @$$self{qw/silent colx colx_desc useraliases num_cols title_rx
1513 rows debug pkg2tieall/};
1514  
1515 # Save user-defined Aliases before wiping %colx
1516 721 my %useralias;
1517 721 foreach my $alias (keys %$useraliases) {
1518 1307 my $cx = $colx->{$alias};
1519 # cx may be undef if referenced column was deleted and/or if
1520 # an alias was created with {optional => TRUE} with non-matching regex.
1521 #next if !defined($cx); # the referenced column was deleted
1522 1307 $useralias{$alias} = [$cx, $colx_desc->{$alias}];
1523 }
1524  
1525 # Now re-generate
1526 721 %$colx = ();
1527 721 %$colx_desc = ();
1528  
1529 9736 ▶ my sub __putback($$$;$) {
Subroutine __putback: called CC=6 SCAR=18.2
1530 9736 my ($key, $cx, $desc, $nomasking) = @_;
1531 9736 ▶ if (defined (my $ocx = $colx->{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $ocx = $$colx{$key})) { } 1651 8085 9736
1532 1651 ▶ if ($cx != $ocx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cx != $ocx) 72 1579 1651
1533 $self->_carponce("Warning: ", visq($key), " ($desc) is MASKED BY (",
1534 72 ▶ $colx_desc->{$key}, ")") unless $silent;
Branch: 1 branch
BranchTrueFalseTotal
unless $silent 0 72 72
1535 72 ▶ oops(dvis '$key $ocx $cx $desc $colx_desc->{$key} $silent $nomasking')
Branch: 1 branch
BranchTrueFalseTotal
if $nomasking 0 72 72
1536 if $nomasking; # _get_usable_titles should have screen out
1537 }
1538 } else {
1539 8085 ▶ oops if exists $colx->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$colx{$key} 0 8085 8085
1540 8085 $colx->{$key} = $cx;
1541 8085 $colx_desc->{$key} = $desc;
1542 }
1543 }
1544  
1545 # Put back the user aliases
1546 721 ▶ while (my ($alias,$aref) = each %useralias) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %useralias) 1307 721 2028
1547 1307 my ($cx, $desc) = @$aref;
1548 1307 __putback($alias, $cx, $desc);
1549 }
1550  
1551 721 ▶ if (defined $title_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title_rx) 531 190 721
1552 # Add non-conflicting titles
1553 531 my ($normal_titles, $unindexed_titles) = $self->_get_usable_titles;
1554 531 foreach (@$normal_titles) {
1555 2319 my ($title, $cx) = @$_;
1556 2319 __putback($title, $cx, __fmt_cx($cx).": Title", 1); # nomasking==1
1557 }
1558 # Titles with leading & trailing spaces trimmed off
1559 531 foreach (@$normal_titles) {
1560 2319 my ($title, $cx) = @$_;
1561 2319 my $key = $title;
1562 2319 $key =~ s/\A\s+//s; $key =~ s/\s+\z//s;
1563 2319 ▶ if ($key ne $title) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key ne $title) 121 2198 2319
1564 # "nomasking" arg is false in case the trimmed title clashes with a
1565 # full Title in another column
1566 121 __putback($key, $cx, __fmt_cx($cx).": Title sans lead/trailing spaces",0);
1567 }
1568 }
1569 # Automatic aliases
1570 # N.B. These come from all titles, not just "normal" ones
1571 531 foreach (@$normal_titles, @$unindexed_titles) {
1572 2481 my ($title, $cx) = @$_;
1573 2481 my $ident = title2ident($title);
1574 2481 __putback($ident, $cx, __fmt_cx($cx).": Automatic alias for title");
1575 }
1576 }
1577 721 my %abc;
1578 721 foreach my $cx ( 0..$num_cols-1 ) {
1579 3508 my $ABC = cx2let($cx);
1580 3508 __putback($ABC, $cx, "cx $cx: Standard letter-code");
1581 }
1582  
1583 721 ▶ unless ($notie) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($notie) 497 224 721
1584 # export and tie newly-defined magic variables to packages which want that.
1585 497 ▶ if (my @pkglist = grep {defined $pkg2tieall->{$_}} keys %$pkg2tieall) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pkglist) = grep({defined $$pkg2tieall{$_};} keys %$pkg2tieall)) 77 420 497
1586 77 my @idents = $self->_all_valid_idents;
1587 77 foreach my $pkg (@pkglist) {
1588 77 $self->_tie_col_vars($pkg, $pkg2tieall->{$pkg}, @idents);
1589 }
1590 }
1591 }
1592 } # _rebuild_colx
1593  
1594 # Move and/or delete column positions. The argument is a ref to an array
1595 # containing the old column indicies of current (i.e. surviving) columns,
1596 # or undefs for new columns which did not exist previously.
1597 23 ▶sub _adjust_colx {
Subroutine _adjust_colx: called CC=9 SCAR=22.2
1598 23 my ($self, $old_colxs) = @_;
1599 my ($colx, $colx_desc, $num_cols, $useraliases, $debug)
1600 23 = @$$self{qw/colx colx_desc num_cols useraliases debug/};
1601 23 ▶ oops unless @$old_colxs == $num_cols;
Branch: 1 branch
BranchTrueFalseTotal
unless @$old_colxs == $num_cols 0 23 23
1602 23 my %old2new;
1603 23 foreach my $new_cx (0..$#$old_colxs) {
1604 307 my $old_cx = $old_colxs->[$new_cx];
1605 307 ▶ $old2new{$old_cx} = $new_cx if defined $old_cx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $old_cx 295 12 307
1606 }
1607 # User-defined aliases are for arbitrary columns, so fix them manually
1608 23 foreach my $alias (keys %$useraliases) {
1609 439 my $cx = $colx->{$alias};
1610 439 ▶ next unless defined $cx; # e.g. non-unique title; see _rebuild_colx()
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cx 0 439 439
1611 439 ▶ if (defined (my $new_cx = $old2new{$cx})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $new_cx = $old2new{$cx})) { } 433 6 439
1612 433 ▶ warn ">adjusting colx{$alias} : $colx->{$alias} -> $new_cx\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 433 433
1613 433 $colx->{$alias} = $new_cx;
1614 } else {
1615 6 ▶ warn ">deleting colx{$alias} (was $colx->{$alias})\n" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 6 6
1616 6 delete $colx->{$alias};
1617 6 delete $colx_desc->{$alias};
1618 6 delete $useraliases->{$alias};
1619 }
1620 }
1621 # Everything else is derived from actual titles
1622 23 $self->_rebuild_colx();
1623 }
1624  
1625 # Translate list of COLSPECs to a list of [cx,desc].
1626 # Regexes may match multiple columns.
1627 # THROWS if a spec does not indicate any existing column.
1628 270 ▶sub _specs2cxdesclist {
Subroutine _specs2cxdesclist: called CC=16 SCAR=27.8
1629 270 my $self = shift;
1630 270 my ($colx, $colx_desc, $num_cols) = @$$self{qw/colx colx_desc num_cols/};
1631 270 my @results;
1632 270 foreach my $spec (@_) {
1633 293 ▶ croak "Column specifier is undef!" unless defined $spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec 0 293 293
1634 293 ▶ if ($spec eq '^') {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "^") 4 289 293
1635 4 push @results, [0, "Special '^' specifier for first col"];
1636 next
1637 4 }
1638 289 ▶ if ($spec eq '$') {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec eq "\$") 10 279 289
1639 10 push @results, [$num_cols-1, "Special '\$' specifier for last col"];
1640 next
1641 10 }
1642 279 ▶ if (($spec =~ /^[1-9]\d*$/ || $spec eq "0")
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /^[1-9]\d*$/u || $spec eq 0 and $spec <= $num_cols) 43 236 279
Condition100%6/6
$spec =~ /^[1-9]\d*$/u || $spec eq 0 and $spec <= $num_cols
!ll&&!rl&&r
235143
$spec =~ /^[1-9]\d*$/u or $spec eq 0
l!l&&r!l&&!r
2618235
MC/DC100%3/3
$spec =~ /^[1-9]\d*$/u || $spec eq 0 and $spec <= $num_cols
$spec =~ /^[1-9]\d*$/u $spec eq 0 $spec <= $num_cols
Truth table80%4/5
A or B and C
A= $spec =~ /^[1-9]\d*$/uB= $spec eq 0C= $spec <= $num_cols
ABCresult
00X0
1X00
0100
1X11
0111
1643 && $spec <= $num_cols) { # allow one-past-end
1644 43 push @results, [$spec, "Numeric column-index"];
1645 next
1646 43 }
1647 236 ▶ if (defined (my $cx = $colx->{$spec})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cx = $$colx{$spec})) 117 119 236
1648 117 push @results, [$cx, $colx_desc->{$spec}];
1649 next
1650 117 }
1651 119 ▶ if (ref($spec) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec eq "Regexp") 88 31 119
1652 88 my ($title_rx, $rows) = @$$self{qw/title_rx rows/};
1653 88 ▶ croak "Can not use regex: No title-row is defined!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title_rx 0 88 88
1654 unless defined $title_rx;
1655 88 ▶ my $title_row = $rows->[$title_rx] // oops;
Condition33%1/3
$$rows[$title_rx] // &oops()
l!l&&r!l&&!r
8800
MC/DC0%0/2
$$rows[$title_rx] // &oops()
$$rows[$title_rx] &oops()
Truth table33%1/3
A // B
A= $$rows[$title_rx]B= &oops()
ABresult
1X1
011
000
1656 88 my $matched;
1657 88 for my $cx (0..$#$title_row) {
1658 342 my $title = $title_row->[$cx];
1659 # Note: We can't use /s here! The regex compiler has already
1660 # encapsulated /s or lack thereof in the compiled regex
1661 342 ▶ if ($title =~ /$spec/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($title =~ /$spec/u) 91 251 342
1662 91 push @results, [$cx, __fmt_cx($cx).": regex matched title '$title'"];
1663 91 $matched++;
1664 }
1665 }
1666 88 ▶ if (! $matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 5 83 88
1667 5 croak "\n--- Title Row (rx $title_rx) ---\n",
1668 # Force evaluation of magicrow as an array ref
1669 vis([ @$title_row ]),"\n-----------------\n",
1670 "Regex $spec\n",
1671 "does not match any of the titles (see above) in '",
1672 fmt_sheet($self),"'\n"
1673 # N.B. check for "does not match" in alias()
1674 }
1675 next
1676 83 }
1677 31 croak "'${spec}' matches no column in $$self->{data_source}\n",
1678 "num_cols=$num_cols. Valid keys are:\n",
1679 $self->_fmt_colx;
1680 }
1681 234 ▶ oops unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 234 234
1682 @results
1683 234 }#_specs2cxdesclist
1684 197 ▶sub _spec2cx { # return $cx or ($cx, $desc); throws if spec is invalid
Subroutine _spec2cx: called CC=2 SCAR=7.6
1685 197 my ($self, $spec) = @_;
1686 197 my @list = $self->_specs2cxdesclist($spec);
1687 194 ▶ if (@list > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list > 1) 0 194 194
1688 croak ivis("Regexpr $spec matches multiple titles:\n "),
1689 0 join("\n ",map{ vis $_->[1] } @list), "\n";
1690 }
1691 194 __first_ifnot_wantarray( @{$list[0]} ) # cx or (cx,desc)
1692 }
1693  
1694 17 ▶sub _colspec2cx {
Subroutine _colspec2cx: called CC=2 SCAR=7.1
1695 17 my ($self, $colspec) = @_;
1696 17 ▶ croak "COLSPEC may not be a regex" if ref($colspec) eq 'Regexp';
Branch: 1 branch
BranchTrueFalseTotal
if ref $colspec eq "Regexp" 0 17 17
1697 17 goto &_spec2cx
1698 }
1699  
1700 # The user-callable API
1701 # THROWS if a spec does not indicate any existing column.
1702 # Can return multiple results due to multple args and/or Regexp multimatch
1703 # In scalar context returns the first result.
1704 # TODO: Change API to return undef for unmatches regex ??
1705 # (so no need to filter exceptions to still die on real errors)
1706 29 ▶sub spectocx(@) { # the user-callable API
Subroutine spectocx: called CC=1 SCAR=0.0
Pod documented
1707 29 my $self = &__selfmust;
1708 29 my @list = $self->_specs2cxdesclist(@_);
1709 4 __first_ifnot_wantarray( map{ $_->[0] } @list )
1710 }
1711  
1712 # Translate a possibly-relative column specification which
1713 # indicate 1 off the end.
1714 #
1715 # The specification may be
1716 # >something (the column after 'something')
1717 # or
1718 # an absolute column indicator (cx or ABC), possibly 1 off the end
1719 # or
1720 # refer to an existing column
1721 #
1722 17 ▶sub _relspec2cx {
Subroutine _relspec2cx: called CC=2 SCAR=6.9
1723 17 my ($self, $spec) = @_;
1724 17 my $colx = $$self->{colx};
1725 17 ▶ if ($spec =~ /^>(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /^>(.*)/u) 13 4 17
1726 13 my $cx = $self->_colspec2cx($1); # croaks if not an existing column
1727 13 return $cx + 1
1728 }
1729 4 $self->_colspec2cx($spec); # croaks if not an existing column
1730 }
1731  
1732 87 ▶sub alias(@) {
Subroutine alias: called CC=11 SCAR=24.7
Pod documented
1733 87 my $self = &__selfmust;
1734 87 ▶ my $opthash = ref($_[0]) eq 'HASH' ? shift() : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 2 85 87
1735 87 ▶ if ($opthash) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opthash) 87 0 87
1736 87 __validate_opthash($opthash, [qw(optional)],
1737 desc => "alias option");
1738 }
1739 87 ▶ croak "'alias' expects an even number of arguments\n"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ % 2 == 0 0 87 87
1740 unless scalar(@_ % 2)==0;
1741  
1742 my ($colx, $colx_desc, $num_cols, $useraliases, $rows, $silent, $debug)
1743 87 = @$$self{qw/colx colx_desc num_cols useraliases rows silent debug/};
1744  
1745 87 my ($file, $lno) = __fn_ln_methname();
1746  
1747 87 my @cxlist;
1748 87 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 87 85 172
1749 87 my $ident = _validate_ident( shift @_ );
1750 87 my $spec = shift @_;
1751  
1752 87 my $cx = eval{ $self->_spec2cx($spec) };
1753 87 ▶ unless(defined $cx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cx) 3 84 87
1754 3 ▶ oops unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 3 3
1755 3 ▶ die $@ unless $opthash->{optional} && $@ =~ /does not match/is;
Branch: 1 branch
BranchTrueFalseTotal
unless $$opthash{"optional"} and $@ =~ /does not match/isu 2 1 3
Condition66%2/3
$$opthash{"optional"} and $@ =~ /does not match/isu
!ll&&!rl&&r
201
MC/DC50%1/2
$$opthash{"optional"} and $@ =~ /does not match/isu
$$opthash{"optional"} $@ =~ /does not match/isu
Truth table66%2/3
A and B
A= $$opthash{"optional"}B= $@ =~ /does not match/isu
ABresult
0X0
100
111
1756 # Always throw on other errors, e.g. regex matches more than one title
1757 };
1758  
1759 85 ▶ if (my $wheredefined = $useraliases->{$ident}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $wheredefined = $$useraliases{$ident}) 0 85 85
1760 0 my $ocx = scalar($self->_spec2cx($ident));
1761 0 ▶ croak "'$ident' is already a user-defined alias",
Branch: 1 branch
BranchTrueFalseTotal
unless $ocx == $cx 0 0 0
1762 " for cx $ocx defined at $wheredefined\n"
1763 unless $ocx==$cx; # allow redundant alias definitions
1764 }
1765  
1766 # Log each pair individually
1767 85 $self->_logmethifv($opthash, \", $ident => ",\__fmt_colspec_cx($spec,$cx));
1768  
1769 85 $colx->{$ident} = $cx;
1770 85 $colx_desc->{$ident} = "alias for ".__fmt_cx($cx)." (".quotekey($spec).")";
1771 85 $useraliases->{$ident} = "${file}:$lno";
1772 85 push @cxlist, $cx;
1773 }
1774 85 $self->_rebuild_colx();
1775  
1776 85 __first_ifnot_wantarray( @cxlist )
1777 }#alias
1778  
1779 3 ▶sub unalias(@) {
Subroutine unalias: called CC=6 SCAR=19.4
Pod documented
1780 3 my $self = &__selfmust;
1781 3 ▶ croak __methname, " does not accept an {OPTIONS} hash\n"
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "HASH" 0 3 3
1782 if ref($_[0]) eq 'HASH';
1783  
1784 my ($colx, $colx_desc, $useraliases)
1785 3 = @$$self{qw/colx colx_desc useraliases/};
1786  
1787 3 foreach (@_) {
1788 3 ▶ delete $useraliases->{$_} // croak "unalias: '$_' is not a column alias\n";
Condition50%1/2
delete $$useraliases{$_} // croak("unalias: '${_}' is not a column alias\n")
l!l
30
MC/DC0%0/1
delete $$useraliases{$_} // croak("unalias: '${_}' is not a column alias\n")
delete $$useraliases{$_}
Truth table50%1/2
A
A= delete $$useraliases{$_}
Aresult
11
00
1789 3 $self->_logmethifv(\" Removing alias $_ => ", \$colx_desc->{$_});
1790 3 ▶ delete $colx->{$_} // oops;
Condition33%1/3
delete $$colx{$_} // &oops()
l!l&&r!l&&!r
300
MC/DC0%0/2
delete $$colx{$_} // &oops()
delete $$colx{$_} &oops()
Truth table33%1/3
A // B
A= delete $$colx{$_}B= &oops()
ABresult
1X1
011
000
1791 3 ▶ delete $colx_desc->{$_} // oops;
Condition33%1/3
delete $$colx_desc{$_} // &oops()
l!l&&r!l&&!r
300
MC/DC0%0/2
delete $$colx_desc{$_} // &oops()
delete $$colx_desc{$_} &oops()
Truth table33%1/3
A // B
A= delete $$colx_desc{$_}B= &oops()
ABresult
1X1
011
000
1792 }
1793 3 $self->_rebuild_colx();
1794 3 $self
1795 }
1796  
1797 # title_rx: Get/set the title row index
1798 #
1799 # $rx = title_rx ; # Retrieve
1800 #
1801 # title_rx undef # Set to no titles
1802 #
1803 # title_rx ROWINDEX # Set to specified rx
1804 #
1805 # title_rx 'auto' # Auto-detect the title row; an exception is thrown
1806 # # if a plausible row is not found.
1807 #
1808 # {OPTARGS} may contain
1809 # verbose, silent, debug (temporarily override the object's settings)
1810 #
1811 # Auto-detect options:
1812 # required => [COLSPEC, ...] # required titles
1813 # min_rx, max_rx => NUM # range of rows to examine
1814 # first_cx => NUM # first column ix which must contain a valid title
1815 # last_cx => NUM # last column ix which must contain a valid title
1816 #
1817 # Note: This is called internally by read_spreadsheet(), passing 'auto'
1818 # by default. Therefore users only need to call title_rx explicitly
1819 # to change title row or if read_spreadsheet() was not used to
1820 # construct the sheet.
1821 #
1822 577 ▶sub title_rx(;$@) {
Subroutine title_rx: called CC=9 SCAR=22.2
Pod documented
1823 577 my ($self, $opthash_arg) = &__selfmust_opthash;
1824 577 my $opthash = { %$opthash_arg }; # make copy so we can modify it
1825 577 my @orig_args = @_;
1826  
1827 577 my $saved_stdopts = $self->_set_stdopts($opthash);
1828 577 ▶ scope_guard{ $self->_restore_stdopts($saved_stdopts) };
Subroutine __ANON__: called CC=1 SCAR=0.0
1829 577 __validate_opthash( $opthash,
1830 [qw(required min_rx max_rx first_cx last_cx)],
1831 desc => "autodetect option",
1832 undef_ok_only => [qw/verbose silent debug/] );
1833 577 my $rx = -999;
1834 577 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) { } 121 456 577
1835 # A return value was requested
1836 121 ▶ croak '{OPTARGS} passed to title_rx with no operator (get request?)'
Branch: 1 branch
BranchTrueFalseTotal
if keys %$opthash 0 121 121
1837 if keys %$opthash;
1838 121 $rx = $$self->{title_rx};
1839 121 $self->_logmethretifv([], [$rx]);
1840 } else {
1841 456 $rx = shift; # N.B. undef means there are no titles
1842 456 ▶ my $notie; $notie = shift() if u($_[0]) eq "_notie"; # during auto-detect probes
Branch: 1 branch
BranchTrueFalseTotal
if &u($_[0]) eq "_notie" 224 232 456
1843 456 ▶ croak "Extraneous argument(s) to title_rx: ".avis(@_) if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 456 456
1844  
1845 456 ▶ if (defined $rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rx) 451 5 456
1846 451 ▶ if ($rx eq 'auto') {
Branch: 3 branches
BranchTrueFalseTotal
if ($rx eq 'auto') { } 96 355 451
elsif (not $rx =~ /^\d+$/u) { } 0 355 355
elsif ($rx > $#{$$$self{'rows'};}) { } 0 355 355
1847 96 $rx = $self->_autodetect_title_rx($opthash);
1848 }
1849 elsif ($rx !~ /^\d+$/) {
1850 0 croak "Invalid title_rx argument: ", visq($rx);
1851 }
1852 355 elsif ($rx > $#{ $$self->{rows} }) {
1853 0 croak "Rx $rx is beyond the end of the data", visq($rx);
1854 }
1855 }
1856 436 $$self->{title_rx} = $rx;
1857 436 $self->_logmethretifv([$opthash_arg, @orig_args], [$rx]);
1858 436 $self->_rebuild_colx($notie);
1859 }
1860 557 $rx;
1861 }#title_rx
1862  
1863 96 ▶sub _autodetect_title_rx {
Subroutine _autodetect_title_rx: called CC=32 SCAR=35.5
1864 96 my ($self, $opthash) = @_;
1865  
1866 my ($title_rx, $rows, $colx, $num_cols, $verbose, $debug) =
1867 96 @$$self{qw(title_rx rows colx num_cols verbose debug)};
1868  
1869  
1870 96 ▶ if ($#$rows == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rows->$#* == -1) 13 83 96
1871 13 return undef; # completely empty
1872 }
1873  
1874 # Filter out titles which can not be used as a COLSPEC
1875 my @required_specs = $opthash->{required}
1876 83 ▶ ? to_array($opthash->{required}) : ();
Branch: 1 branch
BranchTrueFalseTotal
$$opthash{'required'} ? : 38 45 83
1877 83 ▶ croak "undef value in {required}" if grep{! defined} @required_specs;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not defined $_;} @required_specs 0 83 83
1878 83 @required_specs = grep{ !__is_unindexed_title($_, $num_cols) } @required_specs;
1879  
1880 83 ▶ my $min_rx = __validate_nonnegi($opthash->{min_rx}//0, "min_rx");
Condition100%2/2
$$opthash{"min_rx"} // 0
l!l
1469
MC/DC100%1/1
$$opthash{"min_rx"} // 0
$$opthash{"min_rx"}
Truth table100%2/2
A
A= $$opthash{"min_rx"}
Aresult
11
00
1881 83 ▶ my $max_rx = __validate_nonnegi($opthash->{max_rx}//$min_rx+3, "max_rx");
Condition100%2/2
$$opthash{"max_rx"} // $min_rx + 3
l!l
1073
MC/DC100%1/1
$$opthash{"max_rx"} // $min_rx + 3
$$opthash{"max_rx"}
Truth table100%2/2
A
A= $$opthash{"max_rx"}
Aresult
11
00
1882 83 ▶ $max_rx = $#$rows if $max_rx > $#$rows;
Branch: 1 branch
BranchTrueFalseTotal
if $max_rx > $rows->$#* 1 82 83
1883  
1884 83 ▶ my $first_cx = __validate_nonnegi($opthash->{first_cx}//0, "first_cx");
Condition100%2/2
$$opthash{"first_cx"} // 0
l!l
3251
MC/DC100%1/1
$$opthash{"first_cx"} // 0
$$opthash{"first_cx"}
Truth table100%2/2
A
A= $$opthash{"first_cx"}
Aresult
11
00
1885 83 ▶ my $last_cx = __validate_nonnegi($opthash->{last_cx}//INT_MAX, "last_cx");
Condition100%2/2
$$opthash{"last_cx"} // 2147483647
l!l
2063
MC/DC100%1/1
$$opthash{"last_cx"} // 2147483647
$$opthash{"last_cx"}
Truth table100%2/2
A
A= $$opthash{"last_cx"}
Aresult
11
00
1886 83 ▶ $last_cx = $num_cols-1 if $last_cx >= $num_cols;
Branch: 1 branch
BranchTrueFalseTotal
if $last_cx >= $num_cols 63 20 83
1887  
1888 83 my @nd_reasons;
1889 83 ▶ if ($min_rx > $#$rows) {
Branch: 2 branches
BranchTrueFalseTotal
if ($min_rx > $rows->$#*) { } 0 83 83
elsif ($min_rx > $max_rx) { } 2 81 83
1890 0 push @nd_reasons, "min_rx ($min_rx) is out of range";
1891 }
1892 elsif ($min_rx > $max_rx) {
1893 2 push @nd_reasons,
1894 "min_rx ($min_rx) is greater than max_rx ($max_rx)"
1895 }
1896 83 ▶ if ($first_cx >= $num_cols) {
Branch: 2 branches
BranchTrueFalseTotal
if ($first_cx >= $num_cols) { } 2 81 83
elsif ($first_cx > $last_cx) { } 2 79 81
1897 2 push @nd_reasons, "first_cx ($first_cx) is out of range"
1898 }
1899 elsif ($first_cx > $last_cx) {
1900 2 push @nd_reasons,
1901 "first_cx ($first_cx) is less than last_cx ($last_cx)"
1902 }
1903  
1904 83 my $detected;
1905 83 ▶ unless (@nd_reasons) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@nd_reasons) 77 6 83
1906 # no logging during trial and error (except with debug)
1907 77 local $$self->{verbose} = $debug;
1908 77 local $$self->{silent} = !$debug;
1909 77 RX: for my $rx ($min_rx .. $max_rx) {
1910 224 ▶ warn "# ",$nd_reasons[-1],"\n" if $debug && @nd_reasons;
Branch: 1 branch
BranchTrueFalseTotal
if $debug and @nd_reasons 0 224 224
Condition33%1/3
$debug and @nd_reasons
!ll&&!rl&&r
22400
MC/DC0%0/2
$debug and @nd_reasons
$debug @nd_reasons
Truth table33%1/3
A and B
A= $debugB= @nd_reasons
ABresult
0X0
100
111
1911 224 ▶ warn ivis '#autodetect: Trying RX $rx ...\n' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 224 224
1912  
1913 # Make $rx the title_rx so __specs2cxdesclist() can be used
1914 # e.g. to handle regex COLSPECS. Pass special option to not tie
1915 # user variables yet.
1916 224 $self->title_rx($rx, "_notie");
1917 224 ▶ oops unless $rx == $$self->{title_rx};
Branch: 1 branch
BranchTrueFalseTotal
unless $rx == $$$self{"title_rx"} 0 224 224
1918  
1919 224 my $row = $rows->[$rx];
1920 224 for my $cx ($first_cx .. $last_cx) {
1921 667 ▶ if ($row->[$cx] eq "") {
Branch: 1 branch
BranchTrueFalseTotal
if ($$row[$cx] eq "") 139 528 667
1922 139 push @nd_reasons, "rx $rx: col ".__fmt_cx($cx)." is empty";
1923 139 next RX;
1924 }
1925 }
1926 85 foreach my $spec (@required_specs) {
1927 44 my @list; # A regex might match multiple titles
1928 44 eval { @list = $self->_specs2cxdesclist($spec) };
1929 44 ▶ warn ivis ' found $spec in @list\n' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 44 44
1930 44 ▶ if (@list == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@list == 0) 8 36 44
1931 8 push @nd_reasons, ivis 'rx $rx: Required column \'$spec\' not found';
1932 next RX
1933 8 }
1934 36 ▶ my @shortlist = grep{ $_->[0] >= $first_cx && $_->[0] <= $last_cx }
Condition100%3/3
$$_[0] >= $first_cx && $$_[0] <= $last_cx
!ll&&!rl&&r
14622
MC/DC100%2/2
$$_[0] >= $first_cx && $$_[0] <= $last_cx
$$_[0] >= $first_cx $$_[0] <= $last_cx
Truth table100%3/3
A && B
A= $$_[0] >= $first_cxB= $$_[0] <= $last_cx
ABresult
0X0
100
111
1935 @list;
1936 36 ▶ if (@shortlist == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@shortlist == 0) 14 22 36
1937 14 push @nd_reasons, ivis 'rx $rx: Matched \'$spec\' but in unacceptable cx '.avisl(map{$_->[0]} @list);
1938 next RX
1939 14 }
1940 22 ▶ if (! grep{ $_->[1] =~ /title/i } @shortlist) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$$_[1] =~ /title/iu;} @shortlist) 0 22 22
1941 ### ??? Can this actually happen ???
1942 0 push @nd_reasons, ivis 'rx $rx: \'$spec\' resolved to something other than a title: '.__fmt_pairs(@shortlist);
1943 0 next RX;
1944 }
1945 22 ▶ warn ivis ' <<cx is within $first_cx .. $last_cx>>\n' if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 22 22
1946 }
1947 63 $detected = $rx;
1948 last
1949 63 }
1950 77 $$self->{title_rx} = undef; # will re-do below
1951 }
1952 83 ▶ if (defined $detected) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $detected) { } 63 20 83
1953 63 ▶ if ($verbose) { # should be $debug ??
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 63 63
1954 0 my ($fn, $lno, $methname) = __fn_ln_methname();
1955 0 print STDERR "[Auto-detected title_rx = $detected at ${fn}:$lno]\n";
1956 }
1957 63 local $$self->{cmd_nesting} = $$self->{cmd_nesting} + 1;
1958 63 local $$self->{verbose} = 0; # suppress normal logging
1959 63 $self->title_rx($detected); # shows collision warnings unless {silent}
1960 63 return $detected;
1961 } else {
1962 20 ▶ if (@nd_reasons == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nd_reasons == 0) 0 20 20
1963 0 push @nd_reasons, ivis '(BUG?) No rows checked! num_cols=$num_cols rows=$$self->{rows}'.dvis '\n##($min_rx $max_rx $first_cx $last_cx)' ;
1964 }
1965 20 croak("In ",qsh(fmt_sheet($self))," ...\n",
1966 " Auto-detect of title_rx with options ",vis($opthash),
1967 dvis ' @required_specs\n',
1968 " failed because:\n ", join("\n ",@nd_reasons),
1969 "\n"
1970 );
1971 }
1972 }
1973  
1974 15 ▶sub first_data_rx(;$) {
Subroutine first_data_rx: called CC=3 SCAR=11.0
Pod documented
1975 15 my $self = &__self;
1976 15 my $first_data_rx = $$self->{first_data_rx};
1977 15 ▶ if (@_ == 0) { # 'get' request
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 4 11 15
1978 4 $self->_logmethretifv([], [$first_data_rx]);
1979 4 return $first_data_rx;
1980 }
1981 11 my $rx = __validate_nonnegi_or_undef( shift() );
1982 11 $self->_logmethretifv([$rx]);
1983 # Okay if this points to one past the end
1984 11 ▶ $self->_check_rx($rx, 1) if defined $rx; # one_past_end_ok=1
Branch: 1 branch
BranchTrueFalseTotal
if defined $rx 5 6 11
1985 11 $$self->{first_data_rx} = $rx;
1986 11 $self
1987 }
1988 15 ▶sub last_data_rx(;$) {
Subroutine last_data_rx: called CC=5 SCAR=16.1
Pod documented
1989 15 my $self = &__self;
1990 15 my $last_data_rx = $$self->{last_data_rx};
1991 15 ▶ if (@_ == 0) { # 'get' request
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 3 12 15
1992 3 $self->_logmethretifv([], [$last_data_rx]);
1993 3 return $last_data_rx;
1994 }
1995 12 my $rx = __validate_nonnegi_or_undef( shift() );
1996 12 $self->_logmethretifv([$rx]);
1997 12 ▶ if (defined $rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rx) 5 7 12
1998 5 $self->_check_rx($rx, 1); # one_past_end_ok=1
1999 confess "last_data_rx must be >= first_data_rx"
2000 5 ▶ unless $rx >= ($$self->{first_data_rx}//0);
Branch: 1 branch
BranchTrueFalseTotal
unless $rx >= ($$$self{"first_data_rx"} // 0) 0 5 5
Condition100%2/2
$$$self{"first_data_rx"} // 0
l!l
23
MC/DC100%1/1
$$$self{"first_data_rx"} // 0
$$$self{"first_data_rx"}
Truth table100%2/2
A
A= $$$self{"first_data_rx"}
Aresult
11
00
2001 }
2002 12 $$self->{last_data_rx} = $rx;
2003 12 $self
2004 }
2005  
2006 # move_cols ">COLSPEC",source cols...
2007 # move_cols "absolute-position",source cols...
2008 6 ▶sub move_cols($@) {
Subroutine move_cols: called CC=4 SCAR=13.9
Pod documented
2009 6 my $self = &__selfmust;
2010 6 my ($posn, @sources) = @_;
2011  
2012 6 my ($num_cols, $rows) = @$$self{qw/num_cols rows/};
2013  
2014 6 my $to_cx = $self->_relspec2cx($posn);
2015  
2016 6 my @source_cxs = map { scalar $self->_spec2cx($_) } @sources;
2017 6 my @source_cxs_before = grep { $_ < $to_cx } @source_cxs;
2018 6 my $insert_offset = $to_cx - scalar(@source_cxs_before);
2019 0 my @rsorted_source_cxs = sort { $b <=> $a } @source_cxs;
2020  
2021 $self->_logmethifv(\__fmt_colspec_cx($posn,$to_cx), \" <-- ",
2022 6 \join(" ",map{"$source_cxs[$_]\[$_\]"} 0..$#source_cxs));
2023  
2024 6 ▶ croak "move destination is too far to the right\n"
Branch: 1 branch
BranchTrueFalseTotal
if $to_cx + @sources - @source_cxs_before > $num_cols 0 6 6
2025 if $to_cx + @sources - @source_cxs_before > $num_cols;
2026  
2027 6 my @old_cxs = (0..$num_cols-1);
2028  
2029 6 foreach my $row (@$rows, \@old_cxs) {
2030 48 my @moving_cells = @$row[@source_cxs]; # save
2031 48 splice @$row, $_, 1 foreach (@rsorted_source_cxs); # delete
2032 48 splice @$row, $insert_offset, 0, @moving_cells; # put back
2033 };
2034  
2035 6 $self->_adjust_colx(\@old_cxs);
2036 6 $self
2037 }
2038 0 ▶sub move_col($$) { goto &move_cols; }
Subroutine move_col: not called CC=1 SCAR=6.9
Pod documented
2039  
2040 # insert_cols ">COLSPEC",new titles (or ""s or undefs if no title row)
2041 # insert_cols "absolute-position",...
2042 # RETURNS: The new colum indicies, or in scalar context the first cx
2043 11 ▶sub insert_cols($@) {
Subroutine insert_cols: called CC=10 SCAR=23.1
Pod documented
2044 11 my $self = &__selfmust;
2045 11 my ($posn, @new_titles) = @_;
2046 my ($num_cols, $colx, $rows, $title_rx)
2047 11 = @$$self{qw/num_cols colx rows title_rx/};
2048  
2049 11 my $to_cx = $self->_relspec2cx($posn);
2050  
2051 11 $self->_logmethifv(\__fmt_colspec_cx($posn,$to_cx), \" <-- ", \avis(@new_titles));
2052  
2053 11 ▶ @new_titles = map { $_ // "" } @new_titles; # change undef to ""
Condition50%1/2
$_ // ""
l!l
150
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
Aresult
11
00
2054 11 ▶ if (first { $_ ne "" } @new_titles) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&first(sub { $_ ne ''; } , @new_titles)) { } 10 1 11
2055 10 ▶ croak "insert_cols: Can not specify titles unless title_rx is defined\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title_rx 0 10 10
2056 unless defined $title_rx;
2057 10 my $title_row = $rows->[$title_rx];
2058 10 my %seen;
2059 10 foreach my $ntitle (@new_titles) {
2060 14 ▶ croak "New title '$ntitle' specified more than once!" if $seen{$ntitle}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$ntitle}++ 1 13 14
2061 13 my $ex_rx = $colx->{$ntitle}; # for now can't clash with ABC codes
2062 13 ▶ croak "insert_cols: New title '$ntitle' clashes with rx $ex_rx"
Branch: 1 branch
BranchTrueFalseTotal
if defined $ex_rx 1 12 13
2063 if defined $ex_rx;
2064 }
2065 } else {
2066 # NO. Allow new columns without a title
2067 #croak "You must specify non-undef titles if title_rx is defined\n"
2068 # if defined $title_rx;
2069 }
2070 9 my $num_insert_cols = @new_titles;
2071  
2072 9 foreach my $row (@$rows) {
2073 57 ▶ if (defined $title_rx && $row == $rows->[$title_rx]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title_rx and $row == $$rows[$title_rx]) { } 9 48 57
Condition66%2/3
defined $title_rx and $row == $$rows[$title_rx]
!ll&&!rl&&r
0489
MC/DC50%1/2
defined $title_rx and $row == $$rows[$title_rx]
defined $title_rx $row == $$rows[$title_rx]
Truth table66%2/3
A and B
A= defined $title_rxB= $row == $$rows[$title_rx]
ABresult
0X0
100
111
2074 9 splice @$row, $to_cx, 0, @new_titles;
2075 } else {
2076 48 splice @$row, $to_cx, 0, (("") x $num_insert_cols);
2077 }
2078 }
2079 9 $$self->{num_cols} += $num_insert_cols;
2080  
2081 9 $self->_adjust_colx(
2082 [ 0..$to_cx-1, ((undef) x $num_insert_cols), $to_cx..$num_cols-1 ]
2083 ); #calls _rebuild_colx();
2084  
2085 9 __first_ifnot_wantarray( $to_cx .. $to_cx+$num_insert_cols-1 )
2086 }
2087 0 ▶sub insert_col($$) { goto &insert_cols }
Subroutine insert_col: not called CC=1 SCAR=6.9
Pod documented
2088  
2089 # Internal function which looks back two levels to get caller's package
2090 # Returns [list of row indicies] but does not actually change anything.
2091 # In list context, returns ( [list of row indicies], $first_rx, $last_rx )
2092 1 ▶sub _internal_sort_indicies {
Subroutine _internal_sort_indicies: called CC=11 SCAR=25.7
2093 1 my $self = shift;
2094 1 ▶ croak "bad args" unless @_ == 1 or @_ == 3;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 or @_ == 3 0 1 1
Condition33%1/3
@_ == 1 or @_ == 3
l!l&&r!l&&!r
100
MC/DC0%0/2
@_ == 1 or @_ == 3
@_ == 1 @_ == 3
Truth table33%1/3
A or B
A= @_ == 1B= @_ == 3
ABresult
1X1
011
000
2095 1 my ($cmpfunc, $first_rx, $last_rx) = @_;
2096  
2097 my ($rows, $title_rx, $first_data_rx, $last_data_rx)
2098 1 = @$$self{qw/rows title_rx first_data_rx last_data_rx/};
2099  
2100 1 ▶ $first_rx //= $first_data_rx
Branch: 1 branch
BranchTrueFalseTotal
defined $title_rx ? : 1 0 1
Condition50%2/4
$first_rx //= $first_data_rx // (defined $title_rx ? $title_rx + 1 : 0)
l!l
01
$first_data_rx // (defined $title_rx ? $title_rx + 1 : 0)
l!l
01
MC/DC0%0/1
$first_rx //= $first_data_rx // (defined $title_rx ? $title_rx + 1 : 0)
$first_rx
Truth table50%1/2
A
A= $first_rx
Aresult
11
00
2101 // (defined($title_rx) ? $title_rx+1 : 0);
2102 1 ▶ $last_rx //= $last_data_rx // $#$rows;
Condition50%2/4
$last_rx //= $last_data_rx // $rows->$#*
l!l
01
$last_data_rx // $rows->$#*
l!l
01
MC/DC0%0/1
$last_rx //= $last_data_rx // $rows->$#*
$last_rx
Truth table50%1/2
A
A= $last_rx
Aresult
11
00
2103  
2104 1 $self->_logmethifv(\"(sorting rx ${first_rx}..${last_rx})");
2105  
2106 1 ▶ oops unless defined($first_rx);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $first_rx 0 1 1
2107 1 ▶ oops unless defined($last_rx);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $last_rx 0 1 1
2108 1 my $pkg = (caller(1))[0];
2109 my @indicies = sort {
2110 1 my @row_indicies = ($a, $b);
2111 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
2112 3 local ${ "$pkg\::a" } = $rows->[$a]; # actual row objects
2113 3 local ${ "$pkg\::b" } = $rows->[$b];
2114 3 $cmpfunc->(@row_indicies)
2115 } ($first_rx..$last_rx);
2116 1 ▶ oops unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 1 1
2117 1 return( \@indicies, $first_rx, $last_rx );
2118 }
2119  
2120 # sort_row_indicies {compare function}
2121 # sort_row_indicies {compare function} $first_rx, $last_rx
2122 #
2123 # Returns [list of row indicies] but does not actually change anything.
2124 # In list context, returns ( [list of row indicies], $first_rx, $last_rx )
2125 0 ▶sub sort_indicies(&;$$) {
Subroutine sort_indicies: not called CC=3 SCAR=24.8
Pod documented
2126 0 my $self = &__selfmust;
2127 0 my ($indicies, $first_rx, $last_rx) = $self->_internal_sort_indicies(@_);
2128 0 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
elsif (defined wantarray) { } 0 0 0
2129 0 return( $indicies, $first_rx, $last_rx );
2130 }
2131 elsif (defined wantarray) {
2132 0 return $indicies;
2133 }
2134 0 else { croak __methname, " returns [rx list], first_rx, last_rx"; }
2135 }
2136  
2137  
2138 # sort_rows {compare function}
2139 # sort_rows {compare function} $first_rx, $last_rx
2140 1 ▶sub sort_rows(&;$$) {
Subroutine sort_rows: called CC=1 SCAR=0.0
Pod documented
2141 1 my $self = &__selfmust;
2142 1 my ($indicies, $first_rx, $last_rx) = $self->_internal_sort_indicies(@_);
2143  
2144 1 my ($rows, $linenums) = @$$self{qw/rows linenums/};
2145 1 @$rows[$first_rx..$#$rows] = @$rows[@$indicies];
2146 1 @$linenums[$first_rx..$#$rows] = @$linenums[@$indicies];
2147  
2148 1 __validate_not_scalar_context(0..$first_rx-1, @$indicies, $last_rx+1..$#$rows)
2149 }
2150  
2151 3 ▶sub delete_cols(@) {
Subroutine delete_cols: called CC=4 SCAR=13.9
Pod documented
2152 3 my $self = &__selfmust;
2153 3 my (@cols) = @_;
2154 3 my ($num_cols, $rows) = @$$self{qw/num_cols rows/};
2155  
2156 3 my @cxlist = $self->_colspecs_to_cxs_ckunique(\@cols);
2157  
2158 3 my @reverse_cxs = sort { $b <=> $a } @cxlist;
2159  
2160 3 $self->_logmethifv(reverse @reverse_cxs);
2161 3 my @old_cxs = (0..$num_cols-1);
2162 3 for my $row (@$rows, \@old_cxs) {
2163 24 foreach my $cx (@reverse_cxs) {
2164 56 ▶ oops if $cx > $#$row;
Branch: 1 branch
BranchTrueFalseTotal
if $cx > $row->$#* 0 56 56
2165 56 splice @$row, $cx, 1, ();
2166 }
2167 }
2168 3 $$self->{num_cols} -= @reverse_cxs;
2169 3 $self->_adjust_colx(\@old_cxs);
2170 3 $self
2171 }
2172 0 ▶sub delete_col($) { goto &delete_cols; }
Subroutine delete_col: not called CC=1 SCAR=6.9
Pod documented
2173  
2174 # Logic which forces verbose on when debug is on, etc.
2175 # Used by new() and options()
2176 324 ▶sub _set_verbose_debug_silent(@) {
Subroutine _set_verbose_debug_silent: called CC=8 SCAR=20.8
2177 324 my $self = shift;
2178 324 foreach (pairs @_) {
2179 430 my ($key, $val) = @$_;
2180 430 my $oldval = $$self->{$key};
2181 next
2182 430 ▶ unless !!$oldval != !!$val;
Branch: 1 branch
BranchTrueFalseTotal
unless !!$oldval != !!$val 413 17 430
2183 17 ▶ if ($key eq "silent") {
Branch: 3 branches
BranchTrueFalseTotal
if ($key eq 'silent') { } 9 8 17
elsif ($key eq 'verbose') { } 4 4 8
elsif ($key eq 'debug') { } 4 0 4
2184 9 $$self->{$key} = $val;
2185 }
2186 elsif ($key eq "verbose") {
2187 4 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 2 2 4
2188 2 $$self->{saved_silent} = $$self->{silent};
2189 2 $$self->{silent} = 0; #?? might still want to suppress warnings
2190 } else {
2191 2 $$self->{silent} = delete $$self->{saved_silent};
2192 }
2193 }
2194 elsif ($key eq "debug") {
2195 4 ▶ if ($val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val) { } 2 2 4
2196 2 $$self->{saved_verbose} = $$self->{verbose};
2197 2 $$self->{saved_silent} = $$self->{silent};
2198 2 $$self->{silent} = 0;
2199 2 $$self->{verbose} = "forced by {debug}";
2200 } else {
2201 2 $$self->{verbose} = delete $$self->{saved_verbose};
2202 2 $$self->{silent} = delete $$self->{saved_silent};
2203 }
2204 }
2205 0 else { confess "options: Unknown option key '$key'\n"; }
2206 17 $$self->{$key} = $val;
2207 }
2208 }
2209  
2210 # Get or set option(s).
2211 # New settings may be in an {OPTIONS} hash and/or linear args.
2212 # Always returns the old options (key => value pairs).
2213 170 ▶sub options(@) {
Subroutine options: called CC=4 SCAR=13.9
Pod documented
2214 170 my $self = &__self; # auto-create sheet if necessary
2215 170 ▶ my @old = map{ exists($$self->{$_}) ? ($_ => $$self->{$_}) : () }
Branch: 1 branch
BranchTrueFalseTotal
exists $$$self{$_} ? : 342 168 510
2216 qw/verbose debug silent/;
2217  
2218 170 my %eff_args;
2219 170 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) { } 47 123 170
2220 47 ▶ croak "(list) returned but called in scalar or void context"
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 47 47
2221 unless wantarray;
2222 } else {
2223 123 my $opthash = &__opthash; # shift off 1st arg iff it is a hashref
2224 123 %eff_args = (%$opthash, &__validate_pairs);
2225 123 $self->_set_verbose_debug_silent(%eff_args);
2226 }
2227 170 $self->_logmethretifv([\__fmt_pairlist(%eff_args)], [\hvis(@old)]);
2228 170 @old;
2229 }
2230  
2231 6 ▶sub _colspecs_to_cxs_ckunique {
Subroutine _colspecs_to_cxs_ckunique: called CC=4 SCAR=14.2
2232 6 ▶ my ($self, $colspecs) = @_; oops unless @_==2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 2 0 6 6
2233 6 my @cxlist;
2234 my %seen;
2235 6 foreach (@$colspecs) {
2236 50 my $cx = $self->_spec2cx($_); # auto-detects title_rx if needed
2237 50 ▶ if ($seen{$cx}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$cx}) 0 50 50
2238 0 croak "cx $cx is specified by multiple COLSPECs: ", vis($_)," and ",vis($seen{$cx}),"\n";
2239 }
2240 50 $seen{ $cx } = $_;
2241 50 push @cxlist, $cx;
2242 }
2243 @cxlist
2244 6 }
2245  
2246 3 ▶sub only_cols(@) {
Subroutine only_cols: called CC=2 SCAR=6.9
Pod documented
2247 3 my $self = &__selfmust;
2248 3 my @cols = @_;
2249 3 my $rows = $self->rows;
2250  
2251 # Replace each row with just the surviving columns, in the order specified
2252 3 my @cxlist = $self->_colspecs_to_cxs_ckunique(\@cols);
2253 3 for my $row (@$rows) {
2254 21 @$row = map{ $row->[$_] } @cxlist;
2255 }
2256 3 $$self->{num_cols} = scalar(@cxlist);
2257 3 $self->_adjust_colx(\@cxlist);
2258 3 $self
2259 }
2260  
2261 # obj->join_cols separator_or_coderef, colspecs...
2262 # If coderef:
2263 # $_ is bound to the first-named column, and is the destination
2264 # @_ is bound to all named columns, in the order named.
2265 0 ▶sub join_cols(&@) {
Subroutine join_cols: not called CC=3 SCAR=24.8
Pod documented
2266 0 my $self = &__selfmust;
2267 0 my ($separator, @sources) = @_;
2268 0 my $hash = $$self;
2269  
2270 0 my ($num_cols, $rows) = @$hash{qw/num_cols rows/};
2271  
2272 0 my @source_cxs = map { scalar $self->_spec2cx($_) } @sources;
2273 $self->_logmethifv(\"'$separator' ",
2274 0 \join(" ",map{"$source_cxs[$_]\[$_\]"} 0..$#source_cxs));
2275  
2276 0 my $saved_v = $hash->{verbose}; $hash->{verbose} = 0;
2277  
2278 # Merge the content into the first column. N.B. EXCLUDES title row.
2279 my $code = ref($separator) eq 'CODE'
2280 ? $separator
2281 0 ▶ : sub{ $_ = join $separator, @_ } ;
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $separator eq 'CODE' ? : 0 0 0
2282  
2283 # Note first/last_data_rx are ignored
2284 0 ▶ { my $first_rx = ($hash->{title_rx} // -1)+1;
Condition0%0/2
$$hash{"title_rx"} // -1
l!l
00
MC/DC0%0/1
$$hash{"title_rx"} // -1
$$hash{"title_rx"}
Truth table0%0/2
A
A= $$hash{"title_rx"}
Aresult
11
00
2285 0 _apply_to_rows($self, $code, \@source_cxs, undef, $first_rx, undef);
2286 }
2287  
2288 # Delete the other columns
2289 0 $self->delete_cols(@source_cxs[1..$#source_cxs]);
2290  
2291 0 $$self->{verbose} = $saved_v;
2292 0 $self
2293 }
2294 0 ▶sub join_cols_sep($@) { goto &join_cols } # to match the functional API
Subroutine join_cols_sep: not called CC=1 SCAR=6.9
Pod documented
2295  
2296 2 ▶sub rename_cols(@) {
Subroutine rename_cols: called CC=5 SCAR=16.2
Pod documented
2297 2 my $self = &__selfmust;
2298 2 ▶ croak "rename_cols expects an even number of arguments\n"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ % 2 == 0 0 2 2
2299 unless scalar(@_ % 2)==0;
2300  
2301 2 my ($num_cols, $rows, $title_rx) = @$$self{qw/num_cols rows title_rx/};
2302  
2303 2 ▶ croak "rename_cols: No title_rx is defined!\n"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $title_rx 0 2 2
2304 unless defined $title_rx;
2305  
2306 2 my $title_row = $rows->[$title_rx];
2307  
2308 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 2 4
2309 2 my $old_title = shift @_;
2310 2 my $new_title = shift @_;
2311 2 my $cx = $self->_spec2cx($old_title);
2312 2 $self->_logmethifv($old_title, \" -> ", $new_title, \" [cx $cx]");
2313 2 ▶ croak "rename_cols: Column $old_title is too large\n"
Branch: 1 branch
BranchTrueFalseTotal
if $cx > $title_row->$#* 0 2 2
2314 if $cx > $#$title_row; # it must have been an absolute form
2315 2 $title_row->[$cx] = $new_title;
2316  
2317 # N.B. aliases remain pointing to the same columns regardless of names
2318 }
2319 2 $self->_rebuild_colx();
2320 2 $self
2321 }
2322  
2323 # apply {code}, colspec*
2324 # @_ are bound to the columns in the order specified (if any)
2325 # $_ is bound to the first such column
2326 # Only visit rows bounded by first_data_rx and/or last_data_rx,
2327 # starting with title_rx+1 if a title row is defined.
2328 70 ▶sub apply(&;@) {
Subroutine apply: called CC=3 SCAR=11.0
Pod documented
2329 70 my $self = &__selfmust;
2330 70 my ($code, @cols) = @_;
2331 70 my $hash = $$self;
2332 0 my @cxs = map { scalar $self->_spec2cx($_) } @cols;
2333  
2334 70 ▶ my $first_rx = max(($hash->{title_rx} // -1)+1, $hash->{first_data_rx}//0);
Condition100%4/4
$$hash{"title_rx"} // -1
l!l
673
$$hash{"first_data_rx"} // 0
l!l
367
MC/DC100%1/1
$$hash{"title_rx"} // -1
$$hash{"title_rx"}
MC/DC100%1/1
$$hash{"first_data_rx"} // 0
$$hash{"first_data_rx"}
Truth table100%2/2
A
A= $$hash{"title_rx"}
Aresult
11
00
Truth table100%2/2
A
A= $$hash{"first_data_rx"}
Aresult
11
00
2335  
2336 70 _apply_to_rows($self, $code, \@cxs, undef, $first_rx, $hash->{last_data_rx});
2337 }
2338  
2339 # apply_all {code}, colspec*
2340 # Like apply, but ALL rows are visited, inluding the title row if any
2341 3 ▶sub apply_all(&;@) {
Subroutine apply_all: called CC=3 SCAR=13.0
Pod documented
2342 3 my $self = &__selfmust;
2343 3 my ($code, @cols) = @_;
2344 3 my $hash = $$self;
2345 0 my @cxs = map { scalar $self->_spec2cx($_) } @cols;
2346 0 ▶ log_methcall $self, [\"rx 0..",$#{$hash->{rows}},
Branch: 1 branch
BranchTrueFalseTotal
@cxs > 0 ? : 0 0 0
2347 @cxs > 0 ? \(" cxs=".avis(@cxs)) : ()]
2348 3 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 3 3
2349 3 _apply_to_rows($self, $code, \@cxs);
2350 }
2351  
2352 502 ▶sub __arrify_checknotempty($) {
Subroutine __arrify_checknotempty: called CC=5 SCAR=17.8
2353 502 local $_ = shift;
2354 502 ▶ my $result = ref($_) eq 'ARRAY' ? $_ : [ $_ ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 45 457 502
2355 croak "Invalid argument ",vis($_)," (expecting [array ref] or single value)\n"
2356 502 ▶ unless @$result > 0 && !grep{ref($_) || $_ eq ""} @$result;
Branch: 1 branch
BranchTrueFalseTotal
unless @$result > 0 and not grep({ref $_ or $_ eq "";} @$result) 0 502 502
Condition33%2/6
@$result > 0 and not grep({ref $_ or $_ eq "";} @$result)
!ll&&!rl&&r
00502
ref $_ || $_ eq ''
l!l&&r!l&&!r
00660
MC/DC0%0/2
@$result > 0 and not grep({ref $_ or $_ eq "";} @$result)
@$result > 0 not grep({ref $_ or $_ eq "";} @$result)
MC/DC0%0/2
ref $_ || $_ eq ''
ref $_ $_ eq ''
Truth table33%1/3
A and not(B)
A= @$result > 0B= not grep({ref $_ or $_ eq "";} @$result)
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= ref $_B= $_ eq ''
ABresult
1X1
011
000
2357 502 $result
2358 }
2359  
2360 # apply_torx {code} rx, colspec*
2361 # apply_torx {code} [rx list], colspec*
2362 # Only the specified row(s) are visited
2363 # first/last_data_rx are ignored.
2364 469 ▶sub apply_torx(&$;@) {
Subroutine apply_torx: called CC=4 SCAR=14.8
Pod documented
2365 469 my $self = &__selfmust;
2366 469 my ($code, $rxlist_arg, @cols) = @_;
2367 469 ▶ croak "Missing rx (or [list of rx]) argument\n" unless defined $rxlist_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rxlist_arg 0 469 469
2368 469 my $rxlist = __arrify_checknotempty($rxlist_arg);
2369 469 my @cxs = map { scalar $self->_spec2cx($_) } @cols;
2370 log_methcall $self, [\vis($rxlist_arg),
2371 @cxs > 0 ? \(" cxs=".avis(@cxs)) : ()]
2372 469 ▶ if $$self->{verbose};
Branch: 2 branches
BranchTrueFalseTotal
if $$$self{"verbose"} 0 469 469
@cxs > 0 ? : 0 0 0
2373 469 _apply_to_rows($self, $code, \@cxs, $rxlist);
2374 }
2375  
2376 # apply_exceptrx {code} [rx list], colspec*
2377 # All rows EXCEPT the specified rows are visited
2378 33 ▶sub apply_exceptrx(&$;@) {
Subroutine apply_exceptrx: called CC=7 SCAR=19.7
Pod documented
2379 33 my $self = &__selfmust;
2380 33 my ($code, $exrxlist_arg, @cols) = @_;
2381 33 ▶ croak "Missing rx (or [list of rx]) argument\n" unless defined $exrxlist_arg;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exrxlist_arg 0 33 33
2382 33 my $exrxlist = __arrify_checknotempty($exrxlist_arg);
2383 33 my @cxs = map { scalar $self->_spec2cx($_) } @cols;
2384 log_methcall $self, [\vis($exrxlist_arg),
2385 @cxs > 0 ? \(" cxs=".avis(@cxs)) : ()]
2386 33 ▶ if $$self->{verbose};
Branch: 2 branches
BranchTrueFalseTotal
if $$$self{"verbose"} 0 33 33
@cxs > 0 ? : 0 0 0
2387 33 my $hash = $$self;
2388 33 my $max_rx = $#{ $hash->{rows} };
2389 33 foreach (@$exrxlist) {
2390 79 ▶ croak "rx $_ is out of range\n" if $_ < 0 || $_ > $max_rx;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 or $_ > $max_rx 10 69 79
Condition100%3/3
$_ < 0 or $_ > $max_rx
l!l&&r!l&&!r
5569
MC/DC100%2/2
$_ < 0 or $_ > $max_rx
$_ < 0 $_ > $max_rx
Truth table100%3/3
A or B
A= $_ < 0B= $_ > $max_rx
ABresult
1X1
011
000
2391 }
2392 23 my %exrxlist = map{ $_ => 1 } @$exrxlist;
2393 23 my $rxlist = [ grep{ ! exists $exrxlist{$_} } 0..$max_rx ];
2394 23 _apply_to_rows($self, $code, \@cxs, $rxlist);
2395 }
2396  
2397 # split_col {code} oldcol, newcol_start_position, new titles...
2398 # {code} is called for each row with $_ bound to <oldcol>
2399 # and @_ bound to the new column(s).
2400 # The old column is left as-is (not deleted).
2401 0 ▶sub split_col(&$$$@) {
Subroutine split_col: not called CC=3 SCAR=24.8
Pod documented
2402 0 my $self = &__selfmust;
2403 0 my ($code, $oldcol_posn, $newcols_posn, @new_titles) = @_;
2404  
2405 0 my $num_insert_cols = @new_titles;
2406 0 my $old_cx = $self->_spec2cx($oldcol_posn);
2407 0 my $newcols_first_cx = $self->_relspec2cx($newcols_posn);
2408  
2409 log_methcall $self, [\"... $oldcol_posn\[$old_cx] -> [$newcols_first_cx]",
2410 avis(@new_titles)]
2411 0 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 0 0
2412  
2413 0 local $$self->{verbose} = 0;
2414  
2415 0 $self->insert_cols($newcols_first_cx, @new_titles);
2416  
2417 0 ▶ $old_cx += $num_insert_cols if $old_cx >= $newcols_first_cx;
Branch: 1 branch
BranchTrueFalseTotal
if $old_cx >= $newcols_first_cx 0 0 0
2418  
2419 0 $self->apply($code,
2420 $old_cx, $newcols_first_cx..$newcols_first_cx+$num_insert_cols-1);
2421  
2422 0 $self
2423 }
2424  
2425 2 ▶sub reverse_cols() {
Subroutine reverse_cols: called CC=3 SCAR=11.0
Pod documented
2426 2 my $self = &__selfmust;
2427 2 my ($rows, $num_cols) = @$$self{qw/rows num_cols/};
2428 2 ▶ log_methcall $self, [] if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 2 2
2429 2 for my $row (@$rows) {
2430 14 @$row = reverse @$row;
2431 }
2432 2 $self->_adjust_colx([reverse 0..$num_cols-1]);
2433 2 $self
2434 }
2435  
2436 2 ▶sub transpose() {
Subroutine transpose: called CC=7 SCAR=19.5
Pod documented
2437 2 my $self = &__selfmust;
2438 2 ▶ log_methcall $self, [] if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 2 2
2439  
2440 2 my ($rows, $old_num_cols, $linenums) = @$$self{qw/rows num_cols linenums/};
2441  
2442 2 $$self->{useraliases} = {};
2443 2 $$self->{title_rx} = undef;
2444 2 $$self->{first_data_rx} = undef;
2445 2 $$self->{last_data_rx} = undef;
2446  
2447 # Save a copy of the data
2448 2 my @old_rows = ( map{ [ @$_ ] } @$rows );
2449  
2450 # Rebuild the spreadsheet
2451 2 @$rows = ();
2452 2 $$self->{num_cols} = scalar @old_rows;
2453  
2454 2 ▶ for (my $ocx=0; $ocx < $old_num_cols; ++$ocx) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ocx < $old_num_cols) 22 2 24
2455 22 my @nrow;
2456 22 for my $row (@old_rows) {
2457 210 ▶ push @nrow, $row->[$ocx] // "";
Condition50%1/2
$$row[$ocx] // ""
l!l
2100
MC/DC0%0/1
$$row[$ocx] // ""
$$row[$ocx]
Truth table50%1/2
A
A= $$row[$ocx]
Aresult
11
00
2458 }
2459 22 push @$rows, \@nrow;
2460 }
2461 2 ▶ if ($$self->{saved_linenums}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$$self{'saved_linenums'}) { } 1 1 2
2462 1 @$linenums = @{ $$self->{saved_linenums} };
2463 1 delete $$self->{saved_linenums};
2464 } else {
2465 1 $$self->{saved_linenums} = [ @$linenums ];
2466 1 @$linenums = ("?") x scalar @$rows;
2467 }
2468 2 ▶ $$self->{data_source} //= "";
Condition50%1/2
$$$self{"data_source"} //= ""
l!l
20
MC/DC0%0/1
$$$self{"data_source"} //= ""
$$$self{"data_source"}
Truth table50%1/2
A
A= $$$self{"data_source"}
Aresult
11
00
2469 2 $$self->{data_source} .= " transposed";
2470  
2471 2 $self->_rows_replaced;
2472 2 $self
2473 }#transpose
2474  
2475 # delete_rows rx ...
2476 # delete_rows 'LAST' ...
2477 # delete_rows '$' ...
2478 2 ▶sub delete_rows(@) {
Subroutine delete_rows: called CC=20 SCAR=41.8
Pod documented
2479 2 my $self = &__selfmust;
2480 2 my (@rowspecs) = @_;
2481  
2482 my ($rows, $linenums, $title_rx, $first_data_rx, $last_data_rx, $current_rx, $verbose)
2483 2 = @$$self{qw/rows linenums title_rx first_data_rx last_data_rx current_rx verbose/};
2484  
2485 2 foreach (@rowspecs) {
2486 7 ▶ $_ = $#$rows if /^(?:LAST|\$)$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^(?:LAST|\$)$/u 0 7 7
2487 7 __validate_nonnegi($_, "rx to delete");
2488 7 ▶ croak "Invalid row index '$_'\n" unless $_ <= $#$rows;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ <= $rows->$#* 0 7 7
2489 }
2490 2 my @rev_sorted_rxs = sort {$b <=> $a} @rowspecs;
2491 2 ▶ log_methcall $self, [reverse @rev_sorted_rxs] if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 2 2
2492  
2493 # Adjust if needed...
2494 2 ▶ if (defined $title_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title_rx) 2 0 2
2495 2 foreach (@rev_sorted_rxs) {
2496 7 ▶ if ($_ < $title_rx) { --$title_rx }
Branch: 2 branches
BranchTrueFalseTotal
if ($_ < $title_rx) { } 3 4 7
elsif ($_ == $title_rx) { } 0 4 4
2497 elsif ($_ == $title_rx) {
2498 $self->_log("Invalidating titles because rx $title_rx is being deleted\n")
2499 0 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 0 0
2500 0 $title_rx = undef;
2501 0 last;
2502 }
2503 }
2504 2 $$self->{title_rx} = $title_rx;
2505 }
2506 2 ▶ if (defined $first_data_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_data_rx) 0 2 2
2507 0 foreach (@rev_sorted_rxs) {
2508 0 ▶ if ($_ <= $first_data_rx) { --$first_data_rx }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ <= $first_data_rx) 0 0 0
2509 }
2510 0 $$self->{first_data_rx} = $first_data_rx;
2511 }
2512 2 ▶ if (defined $last_data_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_data_rx) 0 2 2
2513 0 foreach (@rev_sorted_rxs) {
2514 0 ▶ if ($_ <= $last_data_rx) { --$last_data_rx }
Branch: 1 branch
BranchTrueFalseTotal
if ($_ <= $last_data_rx) 0 0 0
2515 }
2516 0 $$self->{last_data_rx} = $last_data_rx;
2517 }
2518  
2519 # Back up $current_rx to account for deleted rows.
2520 # $current_rx is left set to one less than the index of the "next" row if
2521 # we are in an apply(). That is, current_rx will be left still pointing to
2522 # the same row as before, or if that row has been deleted then the row
2523 # before that (or -1 if row zero was deleted).
2524 2 ▶ if (defined $current_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $current_rx) 0 2 2
2525 0 foreach (@rev_sorted_rxs) {
2526 0 ▶ --$current_rx if ($_ <= $current_rx);
Branch: 1 branch
BranchTrueFalseTotal
if $_ <= $current_rx 0 0 0
2527 }
2528 0 $$self->{current_rx} = $current_rx;
2529 }
2530  
2531 #warn "### BEFORE delete_rows rx (@rev_sorted_rxs):\n",
2532 # map( { " [$_]=(".join(",",@{$rows->[$_]}).")\n" } 0..$#$rows);
2533  
2534 2 for my $rx (@rev_sorted_rxs) {
2535 7 splice @$rows, $rx, 1, ();
2536 7 splice @$linenums, $rx, 1, ();
2537 }
2538  
2539 #warn "### AFTER delete_rows:\n",
2540 # map( { " [$_]=(".join(",",@{$rows->[$_]}).")\n" } 0..$#$rows);
2541 $self
2542 2 }#delete_rows
2543 0 ▶sub delete_row($) { goto &delete_rows; }
Subroutine delete_row: not called CC=1 SCAR=6.9
Pod undocumented
2544  
2545 # $firstrx = insert_rows [rx [,count]]
2546 # $firstrx = insert_rows ['$'[,count]]
2547 3 ▶sub insert_rows(;$$) {
Subroutine insert_rows: called CC=12 SCAR=27.5
Pod documented
2548 3 my $self = &__selfmust;
2549 3 my ($rx, $count) = @_;
2550 3 ▶ $rx //= 'END';
Condition50%1/2
$rx //= "END"
l!l
30
MC/DC0%0/1
$rx //= "END"
$rx
Truth table50%1/2
A
A= $rx
Aresult
11
00
2551 3 ▶ $count //= 1;
Condition50%1/2
$count //= 1
l!l
30
MC/DC0%0/1
$count //= 1
$count
Truth table50%1/2
A
A= $count
Aresult
11
00
2552  
2553 my ($rows, $linenums, $num_cols, $title_rx, $first_data_rx, $last_data_rx)
2554 3 = @$$self{qw/rows linenums num_cols title_rx first_data_rx last_data_rx/};
2555  
2556 3 ▶ $rx = @$rows if $rx =~ /^(?:END|\$)$/;
Branch: 1 branch
BranchTrueFalseTotal
if $rx =~ /^(?:END|\$)$/u 0 3 3
2557  
2558 log_methcall $self, [\"at rx $rx (count $count)"], [$rx]
2559 3 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 3 3
2560  
2561 3 __validate_nonnegi($rx, "new rx");
2562  
2563 3 ▶ if (defined($title_rx) && $rx <= $title_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $title_rx and $rx <= $title_rx) 1 2 3
Condition100%3/3
defined $title_rx and $rx <= $title_rx
!ll&&!rl&&r
111
MC/DC100%2/2
defined $title_rx and $rx <= $title_rx
defined $title_rx $rx <= $title_rx
Truth table100%3/3
A and B
A= defined $title_rxB= $rx <= $title_rx
ABresult
0X0
100
111
2564 1 $$self->{title_rx} = ($title_rx += $count);
2565 }
2566 3 ▶ if (defined($first_data_rx) && $rx <= $first_data_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $first_data_rx and $rx <= $first_data_rx) 0 3 3
Condition33%1/3
defined $first_data_rx and $rx <= $first_data_rx
!ll&&!rl&&r
300
MC/DC0%0/2
defined $first_data_rx and $rx <= $first_data_rx
defined $first_data_rx $rx <= $first_data_rx
Truth table33%1/3
A and B
A= defined $first_data_rxB= $rx <= $first_data_rx
ABresult
0X0
100
111
2567 0 $$self->{first_data_rx} = ($first_data_rx += $count);
2568 }
2569 3 ▶ if (defined($last_data_rx) && $rx <= $last_data_rx) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $last_data_rx and $rx <= $last_data_rx) 0 3 3
Condition33%1/3
defined $last_data_rx and $rx <= $last_data_rx
!ll&&!rl&&r
300
MC/DC0%0/2
defined $last_data_rx and $rx <= $last_data_rx
defined $last_data_rx $rx <= $last_data_rx
Truth table33%1/3
A and B
A= defined $last_data_rxB= $rx <= $last_data_rx
ABresult
0X0
100
111
2570 0 $$self->{last_data_rx} = ($last_data_rx += $count);
2571 }
2572  
2573 3 for (1..$count) {
2574 14 splice @$rows, $rx, 0, [("") x $num_cols];
2575 14 splice @$linenums, $rx, 0, "??";
2576 }
2577  
2578 3 $rx;
2579 }
2580 0 ▶sub insert_row(;$) { goto &insert_rows; }
Subroutine insert_row: not called CC=1 SCAR=6.9
Pod documented
2581  
2582 # read_spreadsheet $inpath [Spreadsheet::Edit::IO::OpenAsCSV options...]
2583 # read_spreadsheet $inpath [,iolayers =>... or encoding =>...]
2584 # read_spreadsheet $inpath [,{iolayers =>... or encoding =>... }] #OLD API
2585 # read_spreadsheet [{iolayers =>... or encoding =>... }, ] $inpath #NEW API
2586 #
2587 # Titles are auto-detected by default, but this may be controlled
2588 # via {OPTIONS}:
2589 # title_rx => rx # Don't autodetect; set the specified title_rx
2590 # title_rx => undef # Don't autodetect; no titles
2591 # OR: autodetect options, passed thru to title_rx()
2592 #
2593 69 ▶sub read_spreadsheet($;@) {
Subroutine read_spreadsheet: called CC=19 SCAR=31.0
Pod documented
2594 69 my ($self, $opthash, $inpath) = &__self_opthash_1arg;
2595 69 my $orig_opthash = { %$opthash };
2596  
2597 # !! This removes verbose/debug/silent from %$opthash !!
2598 69 my $saved_stdopts = $self->_set_stdopts($opthash);
2599 69 ▶ scope_guard{ $self->_restore_stdopts($saved_stdopts) };
Subroutine __ANON__: called CC=1 SCAR=0.0
2600  
2601 69 my %csvopts = @sane_CSV_read_options;
2602 # Separate out Text::CSV options from %$opthash
2603 69 foreach my $key (Text::CSV::known_attributes()) {
2604 #$csvopts{$key} = delete $opthash{$key} if exists $opthash{$key};
2605 2208 ▶ $csvopts{$key} = $opthash->{$key} if defined $opthash->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$opthash{$key} 0 2208 2208
2606 2208 delete $opthash->{$key};
2607 }
2608 69 $csvopts{escape_char} = $csvopts{quote_char}; # " : """
2609  
2610 croak "Obsolete {sheet} key in options (use 'sheetname')"
2611 69 ▶ if exists $opthash->{sheet};
Branch: 1 branch
BranchTrueFalseTotal
if exists $$opthash{"sheet"} 0 69 69
2612  
2613 69 __validate_opthash( $opthash,
2614 [
2615 qw/data_source title_rx/,
2616 qw/iolayers encoding /,
2617 qw/tempdir use_gnumeric raw_values sheetname input_encoding/, # for OpenAsCsv
2618 qw/required min_rx max_rx first_cx last_cx/, # for title_rx
2619 ],
2620 desc => "read_spreadsheet option",
2621 undef_ok_only => [qw/data_source title_rx iolayers encoding
2622 use_gnumeric/] );
2623  
2624 # convert {encoding} to {iolayers}
2625 69 ▶ if (my $enc = delete $opthash->{encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = delete $$opthash{"encoding"}) 0 69 69
2626 #warn "Found OBSOLETE read_spreadsheet 'encoding' opt (use iolayers instead)\n";
2627 0 ▶ $opthash->{iolayers} = ($opthash->{iolayers}//"") . ":encoding($enc)";
Condition0%0/2
$$opthash{"iolayers"} // ""
l!l
00
MC/DC0%0/1
$$opthash{"iolayers"} // ""
$$opthash{"iolayers"}
Truth table0%0/2
A
A= $$opthash{"iolayers"}
Aresult
11
00
2628 }
2629 # Same as last-used, if any
2630 # N.B. If user says nothing, OpenAsCsv() defaults to UTF-8
2631 69 ▶ $opthash->{iolayers} //= $$self->{iolayers} // "";
Condition50%2/4
$$opthash{"iolayers"} //= $$$self{"iolayers"} // ""
l!l
069
$$$self{"iolayers"} // ""
l!l
069
MC/DC0%0/1
$$opthash{"iolayers"} //= $$$self{"iolayers"} // ""
$$opthash{"iolayers"}
Truth table50%1/2
A
A= $$opthash{"iolayers"}
Aresult
11
00
2632  
2633 my ($rows, $linenums, $meta_info, $verbose, $silent, $debug)
2634 69 = @$$self{qw/rows linenums meta_info verbose silent debug/};
2635  
2636 ##$self->_check_currsheet;
2637  
2638 69 my $hash;
2639 69 { local $$self->{verbose} = 0;
2640 69 ▶ $hash = OpenAsCsv(
Condition33%1/3
$verbose || $debug
l!l&&r!l&&!r
0069
MC/DC0%0/2
$verbose || $debug
$verbose $debug
Truth table33%1/3
A || B
A= $verboseB= $debug
ABresult
1X1
011
000
2641 inpath => $inpath,
2642 debug => $debug,
2643 silent => $silent,
2644 verbose => ($verbose || $debug),
2645 %$opthash, # all our opts are valid here
2646 );
2647 }
2648  
2649 ### TODO: Split off the following into a separate read_csvdata() method
2650 ### which takes a file handle? This might be useful so users
2651 ### can open arbitrary sources (even a pipe) and parse the data
2652 ### (e.g. /etc/passwd with : as the separator).
2653 ### ...but unclear how to handle encoding
2654  
2655 # Save possibly-defaulted iolayers for use in subsequent write_csv
2656 67 ▶ $$self->{iolayers} //= $hash->{iolayers};
Condition33%1/3
$$$self{"iolayers"} //= $$hash{"iolayers"}
l!l&&r!l&&!r
0067
MC/DC0%0/2
$$$self{"iolayers"} //= $$hash{"iolayers"}
$$$self{"iolayers"} $$hash{"iolayers"}
Truth table33%1/3
A //= B
A= $$$self{"iolayers"}B= $$hash{"iolayers"}
ABresult
1X1
011
000
2657  
2658 67 my $fh = $hash->{fh};
2659  
2660 67 $csvopts{keep_meta_info} = 1;
2661 67 ▶ my $csv = Text::CSV->new (\%csvopts)
Branch: 1 branch
BranchTrueFalseTotal
my $csv = "Text::CSV"->new(\%csvopts) or croak('read_spreadsheet: ' . "Text::CSV"->error_diag . &dvis("\\n## %csvopts\\n")) 67 0 67
2662 or croak "read_spreadsheet: ".Text::CSV->error_diag ()
2663 .dvis('\n## %csvopts\n');
2664  
2665 67 undef $$self->{num_cols};
2666 67 @$rows = ();
2667 67 @$linenums = ();
2668 67 my $lnum = 1;
2669 67 ▶ while (my $F = $csv->getline( $fh )) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $F = $csv->getline($fh)) 570 67 637
2670 570 push(@$linenums, $lnum);
2671 570 my @minfo = $csv->meta_info();
2672 # Force quoting of fields which look like negative numbers with an ascii
2673 # minus (\x{2D}) rather than Unicode math minus (\N{U+2212}).
2674 # This prevents conversion to the Unicode math minus when LibreOffice
2675 # reads the CSV. The assumption is that if the input, when converted
2676 # TO a csv, has an ascii minus then the original spreadsheet cell format
2677 # was "text" not numeric.
2678 570 for my $cx (0..$#$F) {
2679 #...TODO $minfo[$cx] |= 0x0001 if $F->[$cx] =~ /^-[\d.]+$/a;
2680 }
2681 570 push(@$meta_info, \@minfo);
2682  
2683 570 push(@$rows, $F);
2684  
2685 570 $lnum = $.+1;
2686 }
2687 67 ▶ close $fh || croak "Error reading $hash->{csvpath}: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak(qq[Error reading $$hash{"csvpath"}: $!\n]) 67 0 67
2688  
2689 $$self->{data_source} = $opthash->{data_source} ||
2690 67 ▶ form_spec_with_sheetname($hash->{inpath}, $hash->{sheetname});
Condition33%1/3
$$opthash{'data_source'} || &form_spec_with_sheetname($$hash{'inpath'}, $$hash{'sheetname'})
l!l&&r!l&&!r
0670
MC/DC0%0/2
$$opthash{'data_source'} || &form_spec_with_sheetname($$hash{'inpath'}, $$hash{'sheetname'})
$$opthash{'data_source'} &form_spec_with_sheetname($$hash{'inpath'}, $$hash{'sheetname'})
Truth table33%1/3
A || B
A= $$opthash{'data_source'}B= &form_spec_with_sheetname($$hash{'inpath'}, $$hash{'sheetname'})
ABresult
1X1
011
000
2691 67 $$self->{sheetname} = $hash->{sheetname}; # possibly undef
2692  
2693 67 $self->_rows_replaced;
2694  
2695 # Set title_rx, either to a value explicitly given in OPTIONS (possibly
2696 # undef, meaning no titles) or else auto-detect.
2697 67 my %autodetect_opts;
2698 67 foreach (qw/required min_rx max_rx first_cx last_cx/) {
2699 335 ▶ $autodetect_opts{$_} = $opthash->{$_} if exists($opthash->{$_});
Branch: 1 branch
BranchTrueFalseTotal
if exists $$opthash{$_} 57 278 335
2700 }
2701 67 ▶ my $arg = exists($opthash->{title_rx}) ? $opthash->{title_rx} : 'auto';
Branch: 1 branch
BranchTrueFalseTotal
exists $$opthash{'title_rx'} ? : 3 64 67
2702 67 { local $$self->{cmd_nesting} = $$self->{cmd_nesting} + 1;
2703 67 $autodetect_opts{verbose} = 0; # suppress logging
2704 67 $self->title_rx(\%autodetect_opts, $arg);
2705 }
2706  
2707 log_methcall $self, [$orig_opthash, $inpath,
2708 \" [title_rx set to ",vis($$self->{title_rx}),\"]"]
2709 57 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 57 57
2710  
2711 57 $self
2712 }#read_spreadsheet
2713  
2714 # write_csv {OPTHASH} "/path/to/output.csv"
2715 # Cells will be quoted if the input was quoted, i.e. if indicated by meta_info.
2716 1 ▶sub write_csv(*;@) {
Subroutine write_csv: called CC=47 SCAR=48.5
Pod documented
2717 1 my ($self, $opts) = &__selfmust_opthash;
2718 1 ▶ my $dest = shift // croak "Missing 'destination' arg";
Condition50%1/2
shift() // croak("Missing 'destination' arg")
l!l
10
MC/DC0%0/1
shift() // croak("Missing 'destination' arg")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
2719  
2720 # Instantiate and remove verbose/debug/silent from %$opts
2721 1 my $saved_stdopts = $self->_set_stdopts($opts);
2722 1 ▶ scope_guard{ $self->_restore_stdopts($saved_stdopts) };
Subroutine __ANON__: called CC=1 SCAR=0.0
2723  
2724 1 my %csvopts = ( @sane_CSV_write_options,
2725 quote_space => 0, # dont quote embedded spaces
2726 );
2727 # Separate out Text::CSV options from {OPTIONS}
2728 1 foreach my $key (Text::CSV::known_attributes()) {
2729 32 ▶ $csvopts{$key} = $opts->{$key} if defined $opts->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$opts{$key} 0 32 32
2730 32 delete $opts->{$key};
2731 }
2732  
2733 1 { my %notok = %$opts;
2734 1 delete $notok{$_} foreach (
2735 #removed above... Text::CSV::known_attributes(),
2736 qw/verbose silent debug/,
2737 );
2738 1 ▶ croak "Unrecognized OPTION(s): ",avislq(keys %notok) if %notok;
Branch: 1 branch
BranchTrueFalseTotal
if %notok 0 1 1
2739 }
2740  
2741 1 ▶ $opts->{iolayers} //= $$self->{iolayers} // "";
Condition50%2/4
$$opts{"iolayers"} //= $$$self{"iolayers"} // ""
l!l
01
$$$self{"iolayers"} // ""
l!l
01
MC/DC0%0/1
$$opts{"iolayers"} //= $$$self{"iolayers"} // ""
$$opts{"iolayers"}
Truth table50%1/2
A
A= $$opts{"iolayers"}
Aresult
11
00
2742 # New API: opts->{iolayers} may have all 'binmode' arguments.
2743 # If it does not include encoding(...) then insert default
2744 1 ▶ if ($opts->{iolayers} !~ /encoding\(|:utf8/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$opts{"iolayers"} =~ /encoding\(|:utf8/u) 1 0 1
2745 1 ▶ $opts->{iolayers} .= ":encoding(".
Condition50%1/2
$self->input_encoding || 'UTF-8'
l!l
01
MC/DC0%0/1
$self->input_encoding || 'UTF-8'
$self->input_encoding
Truth table50%1/2
A
A= $self->input_encoding
Aresult
11
00
2746 ($self->input_encoding() || DEFAULT_WRITE_ENCODING)
2747 .")";
2748 }
2749 1 ▶ if ($opts->{iolayers} !~ /:(?:crlf|raw)\b/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$opts{"iolayers"} =~ /:(?:crlf|raw)\b/u) 1 0 1
2750 # Use platform default
2751 #$opts->{iolayers} .= ":crlf";
2752 }
2753  
2754 my ($rows, $meta_info, $num_cols, $verbose, $debug)
2755 1 = @$$self{qw/rows meta_info num_cols verbose debug/};
2756  
2757 1 my $fh;
2758 1 ▶ if (openhandle($dest)) { # an already-open file handle?
Branch: 1 branch
BranchTrueFalseTotal
if (&openhandle($dest)) { } 0 1 1
2759 log_methcall $self, [$opts, "<file handle specified>"
2760 ." (".scalar(@$rows)." rows, $num_cols columns)"]
2761 0 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 0 0
2762 0 $fh = $dest;
2763 } else {
2764 log_methcall $self, [$opts, $dest
2765 ." (".scalar(@$rows)." rows, $num_cols columns)"]
2766 1 ▶ if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 1 1
2767 1 ▶ croak "Output path suffix must be *.csv, not\n ",qsh($dest),"\n"
Branch: 1 branch
BranchTrueFalseTotal
if $dest =~ /\.([a-z]*)$/u and lc $1 ne "csv" 0 1 1
Condition33%1/3
$dest =~ /\.([a-z]*)$/u and lc $1 ne "csv"
!ll&&!rl&&r
010
MC/DC0%0/2
$dest =~ /\.([a-z]*)$/u and lc $1 ne "csv"
$dest =~ /\.([a-z]*)$/u lc $1 ne "csv"
Truth table33%1/3
A and B
A= $dest =~ /\.([a-z]*)$/uB= lc $1 ne "csv"
ABresult
0X0
100
111
2768 if $dest =~ /\.([a-z]*)$/ && lc($1) ne "csv";
2769 1 ▶ open $fh,">$dest" or croak "$dest: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">$dest" or croak("${dest}: $!\n") 1 0 1
2770 }
2771  
2772 1 ▶ binmode $fh, $opts->{iolayers} or die "binmode:$!";
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh, $$opts{"iolayers"} or die "binmode:$!" 1 0 1
2773  
2774 # Arrgh. Although Text::CSV is huge and complex and implements a complicated
2775 # meta_info mechanism to capture quoting details on input, there is no way to
2776 # use the captured info to specify quoting of output fields!
2777 # So we implement writing CSVs by hand here.
2778 #my $csv = Text::CSV->new (\%csvopts)
2779 # or die "write_csv: ".Text::CSV->error_diag ();
2780 #foreach my $row (@$rows) {
2781 # oops "UNDEF row" unless defined $row; # did user modify @rows?
2782 # $csv->print ($fh, $row);
2783 #};
2784  
2785 # 5/2/22 FIXME: Maybe meta_info could be used when writing, albiet in
2786 # a grotesque way:
2787 # If keep_meta_info is set > 9, then the output quotation style is
2788 # "like it was used in the input of the the last parsed record"; so
2789 # we could "parse" a dummy record to set the quote style before writing
2790 # each record, like this (see perldoc Text::CSV_XS "keep_meta_info"):
2791 # my $csv = Text::CSV_XS->new({ binary=>1, keep_meta_info=>11,
2792 # quote_space => 0 });
2793 # apply_all {
2794 # my $minfo = $meta_info[$rx];
2795 # my @dummy = map{ '', 'x', '""' or '"x'' } @$minfo; # HOW?
2796 # $csv->parse(join ",", @dummy); # set saved meta_info
2797 # $csv->print(*OUTHANDLE, $row);
2798 # }
2799 #
2800  
2801 # Much of the option handling code was copied from Text::CSV_PP.pm
2802 # which depends on default values of options we don't specify explicitly.
2803 # So create a Text::CSV object just to get the effective option values...
2804 1 { my $o = Text::CSV->new( \%csvopts );
2805 1 foreach my $key (Text::CSV::known_attributes()) {
2806 32 $csvopts{$key} = $o->{$key};
2807 }
2808 }
2809  
2810 my $re_esc = ($csvopts{escape_char} ne '' and $csvopts{escape_char} ne "\0")
2811 1 ▶ ? ($csvopts{quote_char} ne '') ? qr/(\Q$csvopts{quote_char}\E|\Q$csvopts{escape_char}\E)/ : qr/(\Q$csvopts{escape_char}\E)/
Branch: 2 branches
BranchTrueFalseTotal
$csvopts{'escape_char'} ne '' && $csvopts{'escape_char'} ne "\000" ? : 1 0 1
$csvopts{'quote_char'} ne '' ? : 1 0 1
Condition33%1/3
$csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0"
!ll&&!rl&&r
001
MC/DC0%0/2
$csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0"
$csvopts{"escape_char"} ne "" $csvopts{"escape_char"} ne "\0"
Truth table33%1/3
A and B
A= $csvopts{"escape_char"} ne ""B= $csvopts{"escape_char"} ne "\0"
ABresult
0X0
100
111
2812 : qr/(*FAIL)/;
2813 1 for my $rx (0..$#$rows) {
2814 7 my $row = $rows->[$rx];
2815 7 my $minfo = $meta_info->[$rx];
2816 7 my @results;
2817 7 for my $cx (0..$num_cols-1) {
2818 84 my $value = $row->[$cx];
2819 84 ▶ confess "ERROR: rx $rx, cx $cx : undef cell value" unless defined($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 84 84
2820 84 my $mi = $minfo->[$cx]; # undef if input was missing columns in this row
2821 my $must_be_quoted = $csvopts{always_quote} ||
2822 84 ▶ (($mi//0) & 0x0001); # was quoted on input
Condition80%4/5
$csvopts{'always_quote'} || ($mi // 0) & 1
l!l&&r!l&&!r
0381
$mi // 0
l!l
7311
MC/DC50%1/2
$csvopts{'always_quote'} || ($mi // 0) & 1
$csvopts{'always_quote'} $mi
Truth table66%2/3
A || B
A= $csvopts{'always_quote'}B= $mi
ABresult
1X1
011
000
2823 84 ▶ unless ($must_be_quoted) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($must_be_quoted) 81 3 84
2824 81 ▶ if ($value eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq '') { } 12 69 81
2825 12 ▶ $must_be_quoted = 42 if $csvopts{quote_empty};
Branch: 1 branch
BranchTrueFalseTotal
if $csvopts{"quote_empty"} 0 12 12
2826 } else {
2827 69 ▶ if ($csvopts{quote_char} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($csvopts{"quote_char"} ne "") 69 0 69
2828 11 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
2829 $must_be_quoted=43 if
2830 ($value =~ /\Q$csvopts{quote_char}\E/) ||
2831 ($csvopts{sep_char} ne '' and $csvopts{sep_char} ne "\0" and $value =~ /\Q$csvopts{sep_char}\E/) ||
2832 ($csvopts{escape_char} ne '' and $csvopts{escape_char} ne "\0" and $value =~ /\Q$csvopts{escape_char}\E/) ||
2833 ($csvopts{quote_binary} && $value =~ /[\x00-\x1f\x7f-\xa0]/) ||
2834 69 ▶ ($csvopts{quote_space} && $value =~ /[\x09\x20]/);
Branch: 1 branch
BranchTrueFalseTotal
if $value =~ /\Q$csvopts{"quote_char"}\E/d or $csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0" and $value =~ /\Q$csvopts{"sep_char"}\E/d or $csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0" and $value =~ /\Q$csvopts{"escape_char"}\E/d or $csvopts{"quote_binary"} and $value =~ /[\x00-\x1f\x7f-\xa0]/d or $csvopts{"quote_space"} and $value =~ /[\x09\x20]/d 0 69 69
Condition33%10/30
$value =~ /\Q$csvopts{"quote_char"}\E/d or $csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0" and $value =~ /\Q$csvopts{"sep_char"}\E/d or $csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0" and $value =~ /\Q$csvopts{"escape_char"}\E/d or $csvopts{"quote_binary"} and $value =~ /[\x00-\x1f\x7f-\xa0]/d or $csvopts{"quote_space"} and $value =~ /[\x09\x20]/d
l!l&&r!l&&!r
0069
$value =~ /\Q$csvopts{"quote_char"}\E/d or $csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0" and $value =~ /\Q$csvopts{"sep_char"}\E/d or $csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0" and $value =~ /\Q$csvopts{"escape_char"}\E/d or $csvopts{"quote_binary"} and $value =~ /[\x00-\x1f\x7f-\xa0]/d
l!l&&r!l&&!r
0069
$value =~ /\Q$csvopts{"quote_char"}\E/d or $csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0" and $value =~ /\Q$csvopts{"sep_char"}\E/d or $csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0" and $value =~ /\Q$csvopts{"escape_char"}\E/d
l!l&&r!l&&!r
0069
$value =~ /\Q$csvopts{"quote_char"}\E/d or $csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0" and $value =~ /\Q$csvopts{"sep_char"}\E/d
l!l&&r!l&&!r
0069
$csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0" and $value =~ /\Q$csvopts{"sep_char"}\E/d
!ll&&!rl&&r
0690
$csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0"
!ll&&!rl&&r
0069
$csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0" and $value =~ /\Q$csvopts{"escape_char"}\E/d
!ll&&!rl&&r
0690
$csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0"
!ll&&!rl&&r
0069
$csvopts{"quote_binary"} and $value =~ /[\x00-\x1f\x7f-\xa0]/d
!ll&&!rl&&r
0690
$csvopts{"quote_space"} and $value =~ /[\x09\x20]/d
!ll&&!rl&&r
6900
MC/DC0%0/11
$value =~ /\Q$csvopts{"quote_char"}\E/d or $csvopts{"sep_char"} ne "" and $csvopts{"sep_char"} ne "\0" and $value =~ /\Q$csvopts{"sep_char"}\E/d or $csvopts{"escape_char"} ne "" and $csvopts{"escape_char"} ne "\0" and $value =~ /\Q$csvopts{"escape_char"}\E/d or $csvopts{"quote_binary"} and $value =~ /[\x00-\x1f\x7f-\xa0]/d or $csvopts{"quote_space"} and $value =~ /[\x09\x20]/d
$value =~ /\Q$csvopts{"quote_char"}\E/d $csvopts{"sep_char"} ne "" $csvopts{"sep_char"} ne "\0" $value =~ /\Q$csvopts{"sep_char"}\E/d $csvopts{"escape_char"} ne "" $csvopts{"escape_char"} ne "\0" $value =~ /\Q$csvopts{"escape_char"}\E/d $csvopts{"quote_binary"} $value =~ /[\x00-\x1f\x7f-\xa0]/d $csvopts{"quote_space"} $value =~ /[\x09\x20]/d
Truth table1%1/68
A or B and C and D or E and F and G or H and I or J and K
A= $value =~ /\Q$csvopts{"quote_char"}\E/dB= $csvopts{"sep_char"} ne ""C= $csvopts{"sep_char"} ne "\0"D= $value =~ /\Q$csvopts{"sep_char"}\E/dE= $csvopts{"escape_char"} ne ""F= $csvopts{"escape_char"} ne "\0"G= $value =~ /\Q$csvopts{"escape_char"}\E/dH= $csvopts{"quote_binary"}I= $value =~ /[\x00-\x1f\x7f-\xa0]/dJ= $csvopts{"quote_space"}K= $value =~ /[\x09\x20]/d
ABCDEFGHIJKresult
1XXXXXXXXXX1
0111XXXXXXX1
00XX111XXXX1
010X111XXXX1
0110111XXXX1
00XX0XX11XX1
00XX10X11XX1
00XX11011XX1
010X0XX11XX1
010X10X11XX1
010X11011XX1
01100XX11XX1
011010X11XX1
011011011XX1
00XX0XX0X111
00XX0XX10111
00XX10X0X111
00XX10X10111
00XX1100X111
00XX11010111
010X0XX0X111
010X0XX10111
010X10X0X111
010X10X10111
010X1100X111
010X11010111
01100XX0X111
01100XX10111
011010X0X111
011010X10111
01101100X111
011011010111
00XX0XX0X0X0
00XX0XX0X100
00XX0XX100X0
00XX0XX10100
00XX10X0X0X0
00XX10X0X100
00XX10X100X0
00XX10X10100
00XX1100X0X0
00XX1100X100
00XX110100X0
00XX11010100
010X0XX0X0X0
010X0XX0X100
010X0XX100X0
010X0XX10100
010X10X0X0X0
010X10X0X100
010X10X100X0
010X10X10100
010X1100X0X0
010X1100X100
010X110100X0
010X11010100
01100XX0X0X0
01100XX0X100
01100XX100X0
01100XX10100
011010X0X0X0
011010X0X100
011010X100X0
011010X10100
01101100X0X0
01101100X100
0110110100X0
011011010100
2835 }
2836 }
2837 }
2838 84 $value =~ s/($re_esc)/$csvopts{escape_char}$1/g;
2839 84 ▶ if ($csvopts{escape_null}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($csvopts{"escape_null"}) 84 0 84
2840 84 $value =~ s/\0/$csvopts{escape_char}0/g;
2841 }
2842 84 ▶ if ($must_be_quoted) {
Branch: 1 branch
BranchTrueFalseTotal
if ($must_be_quoted) 3 81 84
2843 3 $value = $csvopts{quote_char} . $value . $csvopts{quote_char};
2844 }
2845 84 ▶ $fh->print($csvopts{sep_char}) unless $cx==0;
Branch: 1 branch
BranchTrueFalseTotal
unless $cx == 0 77 7 84
2846 84 $fh->print($value);
2847 }
2848 7 $fh->print($csvopts{eol});
2849 }
2850  
2851 1 ▶ if (! openhandle $dest) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&openhandle($dest)) 1 0 1
2852 1 ▶ close $fh || croak "Error writing $dest : $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Error writing $dest : $!\n") 1 0 1
2853 }
2854 $self
2855 1 }#write_csv
2856  
2857 # Write spreadsheet with specified column formats
2858 # {col_formats} is required
2859 # Unless {sheetname} is specified, the sheet name is the outpath basename
2860 # sans any suffix
2861 #
2862 # If the specified output file is a csv this is the same as write_csv().
2863 #
2864 0 ▶sub write_spreadsheet(*;@) {
Subroutine write_spreadsheet: not called CC=10 SCAR=47.0
Pod documented
2865 0 my ($self, $opts, $outpath) = &__self_opthash_1arg;
2866  
2867 0 ▶ if ($outpath =~ /\.csv$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($outpath =~ /\.csv$/iu) 0 0 0
2868 0 return $self->write_csv($opts, $outpath);
2869 }
2870  
2871 # Instantiate and remove verbose/debug/silent from %$opts
2872 0 my $saved_stdopts = $self->_set_stdopts($opts);
2873 0 ▶ scope_guard{ $self->_restore_stdopts($saved_stdopts) };
Subroutine __ANON__: not called CC=1 SCAR=6.9
2874  
2875 0 my $colx = $$self->{colx};
2876  
2877 0 ▶ log_methcall $self, [$opts, $outpath] if $$self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $$$self{"verbose"} 0 0 0
2878  
2879 # {col_formats} may be [list of formats in column order]
2880 # or { COLSPEC => fmt, ..., __DEFAULT__ => fmt }
2881 # (we Transform the latter to the former...)
2882 0 my $cf = $opts->{col_formats};
2883 0 ▶ if ($cf) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cf) 0 0 0
2884 0 ▶ if (ref($cf) eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cf eq "HASH") 0 0 0
2885 0 my ($default, @ary);
2886 0 ▶ while (my ($key, $fmt) = each %$cf) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$cf) 0 0 0
2887 0 ▶ ($default = $fmt),next if $key eq "__DEFAULT__";
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "__DEFAULT__" 0 0 0
2888 0 ▶ my $cx = $colx->{$key} // croak("Invalid COLSPEC '$key' in col_formats");
Condition0%0/2
$$colx{$key} // croak("Invalid COLSPEC '${key}' in col_formats")
l!l
00
MC/DC0%0/1
$$colx{$key} // croak("Invalid COLSPEC '${key}' in col_formats")
$$colx{$key}
Truth table0%0/2
A
A= $$colx{$key}
Aresult
11
00
2889 0 $ary[$cx] = $fmt;
2890 }
2891 0 ▶ foreach (@ary) { $_ = $default if ! defined; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
2892 0 $cf = \@ary;
2893 }
2894 }
2895 0 local $opts->{col_formats} = $cf; # transformed form, or undef if not given
2896  
2897 # First convert to a temporary .csv named "<outputbasename>.csv"
2898 # so that the 'sheet name' in the spreadsheet will be <outputbasename> .
2899 #
2900 0 my $tdir = Path::Tiny->tempdir("_write_spreadsheet_tdir_XXXXX"); # auto-deleted when destroyed
2901 0 my $csvpath = $tdir->child( path($outpath)->basename(qr/\.\w+$/).".csv" );
2902 0 { local $$self->{verbose} = 0;
2903 0 $self->write_csv($csvpath->canonpath,
2904 silent => 1, iolayers => ':encoding(UTF-8)',
2905 @sane_CSV_write_options);
2906 }
2907  
2908 0 convert_spreadsheet($csvpath->canonpath,
2909 %$opts,
2910 iolayers => ':encoding(UTF-8)',
2911 cvt_from => "csv",
2912 outpath => $outpath,
2913 );
2914 0 $self
2915 }
2916  
2917 #====================================================================
2918 # These helpers are used by predefined magic sheet variables.
2919 # See code in Spreadsheet::Edit::import()
2920  
2921 # Return $self if during an apply, or if being examined by Data::Dumper ;
2922 # otherwise croak
2923 30675 ▶sub _onlyinapply {
Subroutine _onlyinapply: called CC=5 SCAR=16.4
2924 30675 my ($self, $accessor) = @_;
2925 30675 ▶ unless (defined $$self->{current_rx}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$$self{"current_rx"}) 23 30652 30675
2926 23 for (my $lvl=2; ;$lvl++) {
2927 112 ▶ my $pkg = (caller($lvl))[0] || last;
Condition100%2/2
(caller $lvl)[0] || (last)
l!l
8923
MC/DC100%1/1
(caller $lvl)[0] || (last)
(caller $lvl)[0]
Truth table100%2/2
A
A= (caller $lvl)[0]
Aresult
11
00
2928 89 ▶ return $self
Branch: 1 branch
BranchTrueFalseTotal
if defined $pkg and $pkg->isa("Data::Dumper") 0 89 89
Condition33%1/3
defined $pkg and $pkg->isa("Data::Dumper")
!ll&&!rl&&r
0890
MC/DC0%0/2
defined $pkg and $pkg->isa("Data::Dumper")
defined $pkg $pkg->isa("Data::Dumper")
Truth table33%1/3
A and B
A= defined $pkgB= $pkg->isa("Data::Dumper")
ABresult
0X0
100
111
2929 if defined($pkg) && $pkg->isa("Data::Dumper") # perldoc UNIVERSAL
2930 }
2931 23 croak "Can't use $accessor now: Not during apply*\n"
2932 #confess "Can't use $accessor now: Not during apply*\n"
2933 }
2934 $self
2935 30652 }
2936 47454 ▶sub __getsheet($$$$) {
Subroutine __getsheet: called CC=4 SCAR=13.9
2937 47454 my ($mutating, $pkg, $uvar, $onlyinapply) = @_;
2938 47454 my $sheet = $pkg2currsheet{$pkg};
2939 47454 ▶ croak("Modifying variable $uvar is not allowed\n")
Branch: 1 branch
BranchTrueFalseTotal
if $mutating 10 47444 47454
2940 if $mutating;
2941 47444 ▶ croak("Can not use $uvar: No sheet is currently valid for package $pkg\n")
Branch: 1 branch
BranchTrueFalseTotal
unless defined $sheet 1 47443 47444
2942 unless defined $sheet;
2943 47443 ▶ $onlyinapply ? _onlyinapply($sheet, $uvar) : $sheet
Branch: 1 branch
BranchTrueFalseTotal
$onlyinapply ? : 24211 23232 47443
2944 }
2945 17453 ▶sub _scal_tiehelper { # access a scalar sheet variable
Subroutine _scal_tiehelper: called CC=2 SCAR=7.0
2946 17453 my($mutating, $pkg, $uvar, $ident, $onlyinapply) = @_;
2947 17453 my $sheet = __getsheet($mutating, $pkg, $uvar, $onlyinapply);
2948 17442 ▶ confess avisq(@_) unless exists $$sheet->{$ident};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$$sheet{$ident} 0 17442 17442
2949 17442 return \$$sheet->{$ident}; # return ref to the scalar
2950 }
2951 13984 ▶sub _aryelem_tiehelper { # access an element of an array sheet variable
Subroutine _aryelem_tiehelper: called CC=6 SCAR=20.5
2952 13984 my($mutating, $pkg, $uvar, $index_ident, $array_ident, $onlyinapply) = @_;
2953 # E.g. for $title_row : index_ident="title_rx" and array_ident="rows"
2954 13984 my $sheet = __getsheet($mutating, $pkg, $uvar, $onlyinapply);
2955 13979 ▶ my $aref = $$sheet->{$array_ident} // oops dvisq '$array_ident @_'; # e.g. {rows}
Condition33%1/3
$$$sheet{$array_ident} // &oops(&dvisq("\$array_ident \@_"))
l!l&&r!l&&!r
1397900
MC/DC0%0/2
$$$sheet{$array_ident} // &oops(&dvisq("\$array_ident \@_"))
$$$sheet{$array_ident} &oops(&dvisq("\$array_ident \@_"))
Truth table33%1/3
A // B
A= $$$sheet{$array_ident}B= &oops(&dvisq("\$array_ident \@_"))
ABresult
1X1
011
000
2956 13979 ▶ my $index = $$sheet->{$index_ident} // do{
Condition66%2/3
$$$sheet{$index_ident} // do { if ($index_ident eq "current_rx" or $index_ident eq "title_rx") { return \(undef); } ; &oops(&dvis("\$array_ident \$index_ident")) }
l!l&&r!l&&!r
13904750
MC/DC0%0/2
$$$sheet{$index_ident} // do { if ($index_ident eq "current_rx" or $index_ident eq "title_rx") { return \(undef); } ; &oops(&dvis("\$array_ident \$index_ident")) }
$$$sheet{$index_ident} do { if ($index_ident eq "current_rx" or $index_ident eq "title_rx") { return \(undef); } ; &oops(&dvis("\$array_ident \$index_ident")) }
Truth table33%1/3
A // B
A= $$$sheet{$index_ident}B= do { if ($index_ident eq "current_rx" or $index_ident eq "title_rx") { return \(undef); } ; &oops(&dvis("\$array_ident \$index_ident")) }
ABresult
1X1
011
000
2957 75 ▶ if ($index_ident eq "current_rx" or $index_ident eq "title_rx") {
Branch: 1 branch
BranchTrueFalseTotal
if ($index_ident eq "current_rx" or $index_ident eq "title_rx") 75 0 75
Condition33%1/3
$index_ident eq "current_rx" or $index_ident eq "title_rx"
l!l&&r!l&&!r
0750
MC/DC0%0/2
$index_ident eq "current_rx" or $index_ident eq "title_rx"
$index_ident eq "current_rx" $index_ident eq "title_rx"
Truth table33%1/3
A or B
A= $index_ident eq "current_rx"B= $index_ident eq "title_rx"
ABresult
1X1
011
000
2958 return \undef # During Data::Dumper inspection of current_row?
2959 75 }
2960 0 oops dvis '$array_ident $index_ident'; # otherwise it's a bug
2961 };
2962 13904 ▶ oops(dvisq '@_ $index') if $index > $#$aref;
Branch: 1 branch
BranchTrueFalseTotal
if $index > $aref->$#* 0 13904 13904
2963 13904 return \$aref->[$index]; # return ref to scalar (the element in the array)
2964 }
2965 8060 ▶sub _refval_tiehelper { # access a sheet variable which is a ref of some kind
Subroutine _refval_tiehelper: called CC=2 SCAR=6.9
2966 8060 my($mutating, $pkg, $uvar, $field_ident, $onlyinapply, $mutable) = @_;
2967 8060 ▶ $mutating = 0 if $mutable;
Branch: 1 branch
BranchTrueFalseTotal
if $mutable 2741 5319 8060
2968 8060 my $sheet = __getsheet($mutating, $pkg, $uvar, $onlyinapply);
2969 8060 return $$sheet->{$field_ident}; # return the value, which is itself a ref
2970 }
2971  
2972 # Retrieve the sheet currently accessed by the functional API & tied globals
2973 # in the caller's package (or the specified package).
2974 # If an argument is passed, change the sheet to the specified sheet.
2975 #
2976 # Always returns the previous sheet (or undef)
2977 20399 ▶sub sheet(;$$) {
Subroutine sheet: called CC=15 SCAR=29.5
Pod documented
2978 20399 my $opthash = &__opthash;
2979 20399 ▶ my $pkg = $opthash->{package} // caller();
Condition66%2/3
$$opthash{"package"} // caller()
l!l&&r!l&&!r
33203660
MC/DC0%0/2
$$opthash{"package"} // caller()
$$opthash{"package"} caller()
Truth table66%2/3
A // B
A= $$opthash{"package"}B= caller()
ABresult
1X1
011
000
2980 20399 ▶ oops if index($pkg,__PACKAGE__) >= 0; # not us or sub-pkg
Branch: 1 branch
BranchTrueFalseTotal
if index($pkg, __PACKAGE__) != -1 0 20399 20399
2981 20399 ▶ my $pkgmsg = $opthash->{package} ? " [for pkg $pkg]" : "";
Branch: 1 branch
BranchTrueFalseTotal
$$opthash{'package'} ? : 33 20366 20399
2982 20399 ▶ my @opthash_unless_empty = ( keys(%$opthash) ? ($opthash) : () );
Branch: 1 branch
BranchTrueFalseTotal
keys %$opthash ? : 34 20365 20399
2983 20399 my $curr = $pkg2currsheet{$pkg};
2984 20399 ▶ my $verbose = $opthash->{verbose} || ($curr && $$curr->{verbose});
Condition50%3/6
$$opthash{'verbose'} || $curr && $$$curr{'verbose'}
l!l&&r!l&&!r
0020399
$curr and $$$curr{"verbose"}
!ll&&!rl&&r
19203800
MC/DC0%0/3
$$opthash{'verbose'} || $curr && $$$curr{'verbose'}
$$opthash{'verbose'} $curr $$$curr{"verbose"}
Truth table50%2/4
A || B and C
A= $$opthash{'verbose'}B= $currC= $$$curr{"verbose"}
ABCresult
1XX1
0111
00X0
0100
2985 20399 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 23 20376 20399
2986 23 my $new = __validate_sheet_arg(shift @_);
2987 23 ▶ croak "Extraneous argument(s) in call to sheet()" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 23 23
2988 23 ▶ if (defined $new) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new) 16 7 23
2989 16 ▶ oops if $$new->{cmd_nesting};
Branch: 1 branch
BranchTrueFalseTotal
if $$$new{"cmd_nesting"} 0 16 16
2990 16 ▶ $verbose ||= $$new->{verbose};
Condition33%1/3
$verbose ||= $$$new{"verbose"}
l!l&&r!l&&!r
0016
MC/DC0%0/2
$verbose ||= $$$new{"verbose"}
$verbose $$$new{"verbose"}
Truth table33%1/3
A ||= B
A= $verboseB= $$$new{"verbose"}
ABresult
1X1
011
000
2991 }
2992  
2993 23 ▶ log_call [@opthash_unless_empty,
Branch: 2 branches
BranchTrueFalseTotal
if $verbose 0 23 23
&u($curr) eq &u($new) ? : 0 0 0
2994 \(" ".fmt_sheet($new)),
2995 \(u($curr) eq u($new)
2996 ? " [no change]"
2997 : " [previous: ".fmt_sheet($curr)."]"),
2998 \$pkgmsg]
2999 if $verbose;
3000  
3001 23 $pkg2currsheet{$pkg} = $new;
3002 } else {
3003 20376 ▶ log_call [@opthash_unless_empty], [\fmt_sheet($curr), \$pkgmsg]
Branch: 1 branch
BranchTrueFalseTotal
if $verbose 0 20376 20376
3004 if $verbose;
3005 }
3006 20399 $curr
3007 }
3008  
3009 #====================================================================
3010 package
3011 Spreadsheet::Edit::RowsTie; # implements @rows and @$sheet
3012 11 ▶use parent 'Tie::Array';
Subroutine BEGIN: called CC=1 SCAR=0.0
3013  
3014 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3015 11 our @CARP_NOT = qw(Tie::Indirect Tie::Indirect::Array
3016 Tie::Indirect::Hash Tie::Indirect::Scalar
3017 Tie::Array Tie::Hash
3018 Spreadsheet::Edit Spreadsheet::Edit::Magicrow);
3019 11 ▶use Data::Dumper::Interp 6.009 qw/visnew
Subroutine BEGIN: called CC=1 SCAR=0.0
3020 vis viso avis alvis ivis dvis hvis hlvis
3021 visq visoq avisq alvisq ivisq dvisq hvisq hlvisq
3022 11 addrvis rvis rvisq u quotekey qsh qshlist qshpath/;
3023  
3024 11 ▶use Scalar::Util qw(looks_like_number weaken);
Subroutine BEGIN: called CC=1 SCAR=0.0
3025 0 ▶sub oops(@) { goto &Spreadsheet::Edit::oops }
Subroutine oops: not called CC=1 SCAR=6.9
3026  
3027 101 ▶sub TIEARRAY {
Subroutine TIEARRAY: called CC=1 SCAR=0.0
3028 101 my ($classname, $sheet) = @_;
3029 101 my $o = bless [ [], $sheet], $classname;
3030 101 weaken $o->[1];
3031 101 $o
3032 }
3033 42867 ▶sub FETCH {
Subroutine FETCH: called CC=3 SCAR=12.0
3034 42867 my ($this, $index) = @_;
3035 42867 my $aref = $this->[0];
3036 42867 ▶ croak "Row index ",u($index)," is invalid or out of range"
Branch: 1 branch
BranchTrueFalseTotal
unless $index >= 0 and $index <= $aref->$#* 0 42867 42867
Condition33%1/3
$index >= 0 and $index <= $aref->$#*
!ll&&!rl&&r
0042867
MC/DC0%0/2
$index >= 0 and $index <= $aref->$#*
$index >= 0 $index <= $aref->$#*
Truth table33%1/3
A and B
A= $index >= 0B= $index <= $aref->$#*
ABresult
0X0
100
111
3037 unless $index >= 0 && $index <= $#$aref;
3038 42867 $aref->[$index];
3039 }
3040 1354 ▶sub STORE {
Subroutine STORE: called CC=11 SCAR=28.7
3041 1354 my ($this, $index, $val) = @_;
3042 1354 my ($aref, $sheet) = @$this;
3043 1354 ▶ croak "Row index ",u($index)," is invalid or out of range"
Branch: 1 branch
BranchTrueFalseTotal
unless $index >= 0 and $index <= $aref->$#* + 1 0 1354 1354
Condition33%1/3
$index >= 0 and $index <= $aref->$#* + 1
!ll&&!rl&&r
001354
MC/DC0%0/2
$index >= 0 and $index <= $aref->$#* + 1
$index >= 0 $index <= $aref->$#* + 1
Truth table33%1/3
A and B
A= $index >= 0B= $index <= $aref->$#* + 1
ABresult
0X0
100
111
3044 unless $index >= 0 && $index <= $#$aref+1;
3045 1354 ▶ croak "Value must be a ref to array of cell values (not $val)"
Branch: 1 branch
BranchTrueFalseTotal
unless &Spreadsheet::Edit::__looks_like_aref($val) 0 1354 1354
3046 if ! Spreadsheet::Edit::__looks_like_aref($val);
3047 # I can't make CARP_NOT skip past Spreadsheet::Edit so doing confess
3048 1354 ▶ if (my $num_cols = $$sheet->{num_cols}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num_cols = $$$sheet{"num_cols"}) 152 1202 1354
3049 152 ▶ confess "New row must contain $num_cols cells (not ", $#$val+1, ")"
Branch: 1 branch
BranchTrueFalseTotal
if @$val != $num_cols 0 152 152
3050 if @$val != $num_cols;
3051 }
3052 1354 for my $cx (0..$#$val) {
3053 7283 local $_ = $val->[$cx];
3054 7283 my $bad;
3055 0 ▶ if (! defined $_) { $bad = "undef" }
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $_) { } 0 7283 7283
elsif (ref $_ and not &looks_like_number($_)) { } 0 14566 14566
3056 0 ▶ elsif (ref($_) && !looks_like_number($_)) { $bad = "strange object: $_" }
Condition33%1/3
ref $_ and not &looks_like_number($_)
!ll&&!rl&&r
728300
MC/DC0%0/2
ref $_ and not &looks_like_number($_)
ref $_ not &looks_like_number($_)
Truth table33%1/3
A and not(B)
A= ref $_B= not &looks_like_number($_)
ABresult
0X0
100
111
3057 7283 ▶ if ($bad) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bad) 0 7283 7283
3058 confess "Attempt to STORE a row at rx $index with $bad at cx $cx (",
3059 Spreadsheet::Edit::cx2let($cx).")\n ",
3060 0 join("\n ",map{ sprintf("[%d] %s: %s",$_,Spreadsheet::Edit::cx2let($_),u($val->[$_])) } 0..$#$val);
3061 #"(".join(",",map{u} @$val).")\n";
3062 }
3063 }
3064 # else (0 or undef) someone promises to set it later
3065  
3066 # Store a *copy* of the data to dispose of a Magicrow wrapper, if present
3067 1354 my $cells = [ @$val ];
3068 1354 $aref->[$index] = Spreadsheet::Edit::Magicrow->new($sheet, $cells);
3069 }
3070 20622 ▶sub FETCHSIZE { scalar @{ $_[0]->[0] } }
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
3071 76 ▶sub STORESIZE {
Subroutine STORESIZE: called CC=1 SCAR=0.0
3072 76 my ($this, $newlen) = @_;
3073 76 $#{ $this->[0] } = $newlen-1;
3074 }
3075 # End packageSpreadsheet::Edit::RowsTie
3076  
3077 #====================================================================
3078 package
3079 Spreadsheet::Edit::Magicrow;
3080  
3081 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3082 11 our @CARP_NOT = ('Spreadsheet::Edit');
3083 11 ▶use Scalar::Util qw(weaken blessed looks_like_number);
Subroutine BEGIN: called CC=1 SCAR=0.0
3084 0 ▶sub oops(@) { goto &Spreadsheet::Edit::oops }
Subroutine oops: not called CC=1 SCAR=6.9
3085 11 ▶use Data::Dumper::Interp;
Subroutine BEGIN: called CC=1 SCAR=0.0
3086  
3087 1354 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
3088 1354 my ($classname, $sheet, $cells) = @_;
3089 1354 my %hashview; tie %hashview, __PACKAGE__, $cells, $sheet;
3090 1354 bless \ [$cells, \%hashview], $classname;
3091 }
3092 34992 ▶use overload '@{}' => sub { ${ shift() }->[0] },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
3093 14511 ▶ '%{}' => sub { ${ shift() }->[1] },
Subroutine __ANON__: called CC=1 SCAR=0.0
3094 #'""' => sub { shift }, # defeats vis overload eval!
3095 #'0+' => sub { shift },
3096 #'==' => sub { my ($self, $other, $swap) = @_; $self == $other },
3097 #'eq' => sub { my ($self, $other, $swap) = @_; "$self" eq "$other" },
3098 11 fallback => 1, # for "" etc. FIXME: is this really ok?
3099 11 ;
3100  
3101 1354 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
3102 1354 my ($pkg, $cells, $sheet) = @_;
3103 1354 my $o = bless \ [$cells, $sheet], $pkg;
3104 1354 weaken $$o->[1];
3105 1354 $o
3106 }
3107 14546 ▶sub _cellref {
Subroutine _cellref: called CC=6 SCAR=17.9
3108 14546 my ($cells, $sheet) = @{ ${ shift() } }; # First arg is 'self'
3109 14546 my $key = shift; # Second arg is key
3110 14546 my $mutating = @_; # Third arg exists only for STORE
3111 14546 my $colx = $$sheet->{colx};
3112 14546 my $cx = $colx->{$key};
3113  
3114 14546 ▶ if (! defined $cx) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cx) 26 14520 14546
3115 # No cx is defined for this key:
3116 # If the identifier is completely unknown then an exception is thrown.
3117 #
3118 # If the ident is defined but has an undef cx then it is an 'optional'
3119 # alias not currently valid (creaded with alias({optional => TRUE},...)),
3120 # or any alias which became invalid because the column was deleted
3121 # or renamed; reading returns undef but writing throws.
3122 #
3123 26 my @carpargs;
3124 26 ▶ if (exists $colx->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$colx{$key}) { } 8 18 26
3125 8 ▶ return \undef unless $mutating; # Reading such a column returns undef
Branch: 1 branch
BranchTrueFalseTotal
unless $mutating 4 4 8
3126 4 @carpargs = ("Attempt to write to alias '$key' which is currently NOT DEFINED");
3127 } else {
3128 @carpargs = ("'$key' is an unknown COLSPEC in ", $$sheet->{data_source},
3129 18 "\nThe valid keys are:\n", $sheet->_fmt_colx());
3130 }
3131 22 ▶ if (@carpargs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@carpargs) 22 0 22
3132 22 croak(@carpargs);
3133 }
3134 }
3135 14520 ▶ $cx <= $#{$cells}
Condition50%1/2
$cx <= $#{$cells;} // croak("BUG?? key '${key}' maps to cx $cx which is out of range!")
l!l
145200
MC/DC0%0/1
$cx <= $#{$cells;} // croak("BUG?? key '${key}' maps to cx $cx which is out of range!")
$cx <= $#{$cells;}
Truth table50%1/2
A
A= $cx <= $#{$cells;}
Aresult
11
00
3136 // croak "BUG?? key '$key' maps to cx $cx which is out of range!";
3137 14520 \$cells->[$cx]
3138 }
3139 14503 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
3140 14503 ${ &_cellref }
3141 }
3142 43 ▶sub STORE {
Subroutine STORE: called CC=1 SCAR=0.0
3143 43 my $r = &_cellref;
3144 35 $$r = shift;
3145 }
3146 0 ▶sub NEXTKEY {
Subroutine NEXTKEY: not called CC=1 SCAR=6.9
3147 0 my (undef, $sheet) = @{ ${ shift() } };
3148 0 each %{ $$sheet->{colx} }
3149 }
3150 0 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: not called CC=1 SCAR=6.9
3151 0 my (undef, $sheet) = @{ ${ shift() } };
3152 0 my $colx = $$sheet->{colx};
3153 0 my $a = scalar keys %$colx; # reset iterator
3154 0 each %$colx;
3155 }
3156 0 ▶sub EXISTS {
Subroutine EXISTS: not called CC=1 SCAR=6.9
3157 0 my (undef, $sheet) = @{ ${ shift() } };
3158 0 my $key = shift;
3159 0 exists $$sheet->{colx}->{$key}
3160 }
3161 0 ▶sub SCALAR {
Subroutine SCALAR: not called CC=1 SCAR=6.9
3162 0 my (undef, $sheet) = @{ ${ shift() } };
3163 0 scalar %{ $$sheet->{colx} }
3164 }
3165 0 ▶sub DELETE { confess "DELETE not allowed for ".__PACKAGE__ }
Subroutine DELETE: not called CC=1 SCAR=6.9
3166 0 ▶sub CLEAR { confess "CLEAR not allowed for ".__PACKAGE__ }
Subroutine CLEAR: not called CC=1 SCAR=6.9
3167  
3168 # End package Spreadsheet::Edit::Magicrow;
3169 #====================================================================
3170  
3171 11 1;
3172 __END__
« Edit/Log.pm ↑ Summary Edit/Preload.pm »