Edit/Log.pm

filter: Statementstmt 82.4% 273 / 331 Branchbran 58.8% 93 / 158 Conditioncond 64.3% 47 / 73 MC/DCmcdc 34.0% 15 / 44 Subroutinesub 84.9% 45 / 53 Podpod 78.9% 15 / 19 totaltotal 71.9% 488 / 678 CC 111 SCAR 33.7
CC
111
Cov
73%
_btwN_str_back1
88.7
oops
42.0
_fmt_list
21.3
CRAP 29.1
« Edit/IO.pm ↑ Summary Edit.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 dot com) has waived all copyright and
4 # related or neighboring rights to this document. Attribution is requested
5 # but not required.
6 12 ▶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
7 12 ▶use feature qw(say state lexical_subs current_sub);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶no warnings qw(experimental::lexical_subs);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 package Spreadsheet::Edit::Log;
11  
12 # Allow "use <thismodule> VERSION ..." in development sandbox to not bomb
13 12 ▶{ no strict 'refs'; ${__PACKAGE__."::VER"."SION"} = 1999.999; }
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 our $VERSION = '1001.005'; # VERSION from Dist::Zilla::Plugin::OurPkgVersion
15 12 our $DATE = '2026-11-11'; # DATE from Dist::Zilla::Plugin::OurDate
16  
17 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 12 ▶use Scalar::Util qw/reftype refaddr blessed weaken openhandle looks_like_number/;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 12 ▶use List::Util qw/first any all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 12 ▶use Time::HiRes ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 12 ▶use File::Basename qw/dirname basename/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 12 ▶use Data::Dumper::Interp ':DEFAULT', qw/avisl addrvis u/;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 12 ▶use Exporter 5.57 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 12 our @EXPORT = qw/fmt_call log_call fmt_methcall log_methcall
26 nearest_call abbrev_call_fn_ln_subname/;
27  
28 12 our @EXPORT_OK = qw/btw btwN btwbt btwN_str btwpad btw_rjust_prefix
29 oops set_logdest get_logdest
30 colorize color_warn get_colorize_startend
31 ERROR_COLOR WARN_COLOR BOLD_COLOR SUCCESS_COLOR
32 _can_colorize/;
33  
34 my %backup_defaults = (
35 logdest => \*STDERR,
36 35 ▶ is_public_api => sub{ $_[1][3] =~ /(?:::|^)[a-z][^:]*$/ },
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 #fmt_object => sub{ addrvis($_[1]) },
39 # Just show the address, sans class::name. Note addrvis wraps it in <...>
40 3 ▶ fmt_object => sub{ addrvis(refaddr($_[1])) },
Subroutine __ANON__: called CC=1 SCAR=0.0
41 12 );
42  
43 0 ▶sub set_logdest(*) {
Subroutine set_logdest: not called CC=1 SCAR=6.9
Pod documented
44 #Carp::cluck(ivis '## set_logdest $_[0]\n');
45 0 $backup_defaults{logdest} = $_[0];
46 }
47 0 ▶sub get_logdest() {
Subroutine get_logdest: not called CC=1 SCAR=6.9
Pod documented
48 $backup_defaults{logdest}
49 0 }
50  
51 12 ▶use constant ERROR_COLOR => "error";
Subroutine BEGIN: called CC=1 SCAR=0.0
52 12 ▶use constant WARN_COLOR => "warn";
Subroutine BEGIN: called CC=1 SCAR=0.0
53 12 ▶use constant BOLD_COLOR => "norm";
Subroutine BEGIN: called CC=1 SCAR=0.0
54 12 ▶use constant SUCCESS_COLOR => "succ";
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 # Insert escapes to colorize text, provided the terminal supports ansi escapes
57 12 our $colorcodes;
58 12 ▶sub _getcode($) {
Subroutine _getcode: called CC=4 SCAR=14.1
59 12 my ($tput_args) = @_;
60 12 ▶ unless (exists $colorcodes->{$tput_args}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $$Spreadsheet::Edit::Log::colorcodes{$tput_args}) 3 9 12
61 3 $colorcodes->{$tput_args} = `tput $tput_args 2>/dev/null`;
62 3 ▶ $colorcodes->{$tput_args} = undef if $? != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $? != 0 0 3 3
63 }
64 12 ▶ return $colorcodes->{$tput_args} // die "FAILED: tput $tput_args (export NO_COLOR=1 to bypass)"; # trapped in eval below
Condition50%1/2
$$Spreadsheet::Edit::Log::colorcodes{$tput_args} // die("FAILED: tput $tput_args (export NO_COLOR=1 to bypass)")
l!l
120
MC/DC0%0/1
$$Spreadsheet::Edit::Log::colorcodes{$tput_args} // die("FAILED: tput $tput_args (export NO_COLOR=1 to bypass)")
$$Spreadsheet::Edit::Log::colorcodes{$tput_args}
Truth table50%1/2
A
A= $$Spreadsheet::Edit::Log::colorcodes{$tput_args}
Aresult
11
00
65 }
66 13 ▶sub get_colorize_startend($) {
Subroutine get_colorize_startend: called CC=9 SCAR=25.7
Pod undocumented
67 13 my ($colortype) = @_;
68 13 ▶ if (! exists $ENV{NO_COLOR}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ENV{"NO_COLOR"}) 0 13 13
69 0 $ENV{NO_COLOR} = ! -t &get_effective_logdest();
70 }
71 13 my ($color_start, $color_end) = ("", "");
72 13 ▶ unless ($ENV{NO_COLOR}) { # check every time
Branch: 1 branch
BranchTrueFalseTotal
unless ($ENV{"NO_COLOR"}) 6 7 13
73 6 eval {
74 # The basic colors 0-7 => black,red,green,yellow,blue,magenta,cyan,white
75 # ("white" is often really light grey)
76 6 ▶ $color_start =
Branch: 4 branches
BranchTrueFalseTotal
$colortype eq 'norm' ? : 0 6 6
$colortype eq 'warn' ? : 5 1 6
$colortype eq 'error' ? : 1 0 1
$colortype eq 'succ' ? : 0 0 0
77 $colortype eq BOLD_COLOR ? _getcode("bold") :
78 $colortype eq WARN_COLOR ? _getcode("setaf 3 bold") : # bold yellow
79 $colortype eq ERROR_COLOR ? _getcode("setaf 1 bold") : # bold red
80 $colortype eq SUCCESS_COLOR ? _getcode("setaf 2 bold") : # bold green
81 croak "unknown message type '$colortype'"
82 ;
83 6 $color_end = _getcode("sgr0");
84 };
85 6 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
86 0 ▶ die $@ unless $@ =~ /FAILED: tput/; # disabled or TERM does not support it.
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ /FAILED: tput/ 0 0 0
87 }
88 }
89 13 return ($color_start, $color_end);
90 }
91 13 ▶sub colorize($$) {
Subroutine colorize: called CC=2 SCAR=7.0
Pod documented
92 13 my ($str, $colortype) = @_;
93 13 my ($color_start, $color_end) = get_colorize_startend($colortype);
94 13 ▶ my @chunks = map{ $_ eq "" ? "" : $color_start.$_.$color_end }
Branch: 1 branch
BranchTrueFalseTotal
$_ eq '' ? : 0 14 14
95 split /\R/s, $str, -1;
96 13 return join "\n", @chunks;
97 }
98 1 ▶sub _can_colorize() { # used by test scripts
Subroutine _can_colorize: called CC=1 SCAR=0.0
99 1 state $yes_we_can = do{
100 1 local $ENV{NO_COLOR} = 0;
101 1 colorize("foo bar\nbaz", ERROR_COLOR) =~ /\033/;
102 };
103 }
104  
105 0 ▶sub color_warn(@) {
Subroutine color_warn: not called CC=1 SCAR=6.9
Pod undocumented
106 0 warn colorize(join(" ",@_), WARN_COLOR);
107 }
108 0 ▶sub oops(@) {
Subroutine oops: not called CC=6 SCAR=37.4
Pod documented
109 0 my @args = @_;
110 0 foreach (@args) { $_ = colorize($_, ERROR_COLOR); }
111 0 my $pkg = caller;
112 0 my $pfx = "\n".colorize("OOPS", ERROR_COLOR);
113 #$pfx .= " in pkg '$pkg'" unless $pkg eq 'main';
114 0 $pfx .= ": ";
115 0 ▶ if (defined(&Spreadsheet::Edit::logmsg)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &Spreadsheet::Edit::logmsg) { } 0 0 0
116 # Show current apply sheet & row if any.
117 0 @_=($pfx, &Spreadsheet::Edit::logmsg(@args));
118 } else {
119 0 @_=($pfx, @args);
120 }
121 0 ▶ push @_,"\n" unless $_[-1] =~ /\R\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $_[-1] =~ /\R\z/ 0 0 0
122 0 ▶ STDOUT->flush if openhandle(*STDOUT);
Branch: 1 branch
BranchTrueFalseTotal
if &openhandle(*STDOUT) 0 0 0
123 0 ▶ STDERR->flush if openhandle(*STDERR);
Branch: 1 branch
BranchTrueFalseTotal
if &openhandle(*STDERR) 0 0 0
124 0 goto &Carp::confess;
125 }
126  
127 12 our (%btw_importing_pkgs, $multiple_btw_importers, $start_time);
128 25 ▶sub import {
Subroutine import: called CC=9 SCAR=23.5
129 25 my $class = shift;
130 25 my $pkg = caller;
131 25 my @remaining_args;
132 25 foreach (@_) {
133 143 ▶ if (/^:btw\w*=/ or /^:(no)?color/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^:btw\w*=/ or /^:(no)?color/) 0 143 143
Condition33%1/3
/^:btw\w*=/ or /^:(no)?color/
l!l&&r!l&&!r
00143
MC/DC0%0/2
/^:btw\w*=/ or /^:(no)?color/
/^:btw\w*=/ /^:(no)?color/
Truth table33%1/3
A or B
A= /^:btw\w*=/B= /^:(no)?color/
ABresult
1X1
011
000
134 0 croak "Import tag '${_}' is no longer supported.\n"; # as of v1001.001
135 }
136 143 ▶ if (/btw/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/btw/) 25 118 143
137 $multiple_btw_importers = 1
138 25 ▶ if keys(%btw_importing_pkgs) && !$btw_importing_pkgs{$pkg};
Branch: 1 branch
BranchTrueFalseTotal
if keys %Spreadsheet::Edit::Log::btw_importing_pkgs and not $Spreadsheet::Edit::Log::btw_importing_pkgs{$pkg} 13 12 25
Condition66%2/3
keys %Spreadsheet::Edit::Log::btw_importing_pkgs and not $Spreadsheet::Edit::Log::btw_importing_pkgs{$pkg}
!ll&&!rl&&r
12013
MC/DC50%1/2
keys %Spreadsheet::Edit::Log::btw_importing_pkgs and not $Spreadsheet::Edit::Log::btw_importing_pkgs{$pkg}
keys %Spreadsheet::Edit::Log::btw_importing_pkgs not $Spreadsheet::Edit::Log::btw_importing_pkgs{$pkg}
Truth table66%2/3
A and not(B)
A= keys %Spreadsheet::Edit::Log::btw_importing_pkgsB= not $Spreadsheet::Edit::Log::btw_importing_pkgs{$pkg}
ABresult
0X0
100
111
139 25 $btw_importing_pkgs{$pkg}++;
140 25 ▶ if (/^:btw$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^:btw$/) 25 0 25
141 25 push @remaining_args, qw/btw btwN btwbt btwN_str
142 btwpad btw_rjust_prefix/;
143 25 next;
144 }
145 }
146 118 ▶ if (/^:time/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^:time/) 0 118 118
147 0 $start_time = Time::HiRes::time();
148 0 next;
149 }
150 118 push @remaining_args, $_;
151 }
152  
153 25 @_ = ($class, @remaining_args);
154 25 goto &Exporter::import
155 }#import
156  
157 12 our $btw_prefix = "";
158 12 ▶sub btwpad {
Subroutine btwpad: called CC=4 SCAR=23.2
Pod documented
159 # ??? 8/1/26: This used to update $btw_prefix but return its previous value,
160 # which now seems incorrect.
161 ##my $prev = $btw_prefix;
162 12 ▶ if (defined (my $new = $_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $new = $_[0])) 0 12 12
163 0 ▶ if ($new =~ /^([-+]?)\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new =~ /^([-+]?)\d+$/) { } 0 0 0
164 # NUMSPACES or ±INCR
165 0 ▶ $btw_prefix = " " x ($1 ? (length($btw_prefix) + $new) : $new);
Branch: 1 branch
BranchTrueFalseTotal
$1 ? : 0 0 0
166 } else {
167 0 $btw_prefix = $new; # literal string
168 }
169 }
170 ##return $prev;
171 12 return $btw_prefix;
172 }
173  
174 # Right-justify btw prefixes to widest seen?
175 # Optional argument is a boolean to enable/disable (no arg:enable)
176 12 our $keep_pfx_width = undef;
177 0 ▶if ($threads::shared) { &threads::shared::share(\$keep_pfx_width); }
Branch: 1 branch
BranchTrueFalseTotal
if ($threads::shared) 0 12 12
178 0 ▶sub btw_rjust_prefix {
Subroutine btw_rjust_prefix: not called CC=3 SCAR=24.8
Pod undocumented
179 0 ▶ my $enable = @_==0 ? 1 : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 0 0 0
180 0 ▶ $keep_pfx_width = $enable ? 0 : undef;
Branch: 1 branch
BranchTrueFalseTotal
$enable ? : 0 0 0
181 }
182  
183 12 ▶sub _btwN_str_back1($@) { # does not show top call frame
Subroutine _btwN_str_back1: called CC=40 SCAR=44.8
184 12 my ($N, @strings) = @_;
185 12 local ($@, $!, $^E, $., $,, $/, $\, $?, $^W);
186 12 my $nskip = 1;
187 12 my (@frames, $pkg_obvious);
188 #my $framesep = ">";
189 #my $framesep = " → ";
190 #my $framesep = " ⇢ ";
191 #my $framesep = " » "; # « exists in both Unicode and latin1
192 #my $framesep = " ⇒ ";
193 12 my $framesep = "»";
194 12 my ($firstN, $lastN, @custom_levels);
195 12 ▶ if (ref($N) eq "") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $N eq '') { } 11 1 12
elsif (ref $N eq 'SCALAR' and defined $$N) { } 1 0 1
elsif (ref $N eq 'ARRAY') { } 0 0 0
196 11 $firstN = $lastN = $N;
197 }
198 ▶ elsif (ref($N) eq 'SCALAR' && defined($$N)) {
Condition33%1/3
ref $N eq "SCALAR" and defined $$N
!ll&&!rl&&r
001
MC/DC0%0/2
ref $N eq "SCALAR" and defined $$N
ref $N eq "SCALAR" defined $$N
Truth table33%1/3
A and B
A= ref $N eq "SCALAR"B= defined $$N
ABresult
0X0
100
111
199 1 ▶ if ($$N < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$N < 0) { } 0 1 1
200 # mini-traceback excluding N most recent frames
201 0 $firstN = -$$N;
202 0 $lastN = 99999;
203 } else {
204 # mini-traceback of N most recent frames
205 1 $firstN = 0;
206 1 $lastN = $$N-1;
207 }
208 }
209 elsif (ref($N) eq 'ARRAY') {
210 0 @custom_levels = sort { $a <=> $b } @$N; # arbitrary list of levels
211 0 $firstN = $custom_levels[0];
212 0 $lastN = $custom_levels[-1];
213 0 ▶ $framesep = "," unless $#custom_levels == ($custom_levels[-1] - $custom_levels[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless $#custom_levels == $custom_levels[-1] - $custom_levels[0] 0 0 0
214 }
215 # $firstN & $lastN are now always set and possibly @custom_levels
216  
217 12 ▶ if (any{ !looks_like_number($_) or $_ < 0 } ($firstN, $lastN, @custom_levels)) {
Subroutine __ANON__: called CC=2 SCAR=7.6
Branch: 2 branches
BranchTrueFalseTotal
if (&any(sub { not &looks_like_number($_) or $_ < 0; } , $firstN, $lastN, @custom_levels)) 0 12 12
not &looks_like_number($_) or $_ < 0 0 24 24
218 0 carp "Invalid N arg to btwN: ${\vis($N)} ";
219 0 ($firstN, $lastN) = (0, 99999);
220 0 @custom_levels = ();
221 }
222 12 ▶ if (!caller($firstN + $nskip)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (caller $firstN + $nskip) 0 12 12
223 0 carp "Frame level $firstN starts too far back ";
224 0 ($firstN, $lastN) = (0, 99999);
225 0 @custom_levels = ();
226 }
227  
228 12 { my (%pkgtail2full, %fname2full, $prev_package);
229 12 my ($uniq_pkgtails, $uniq_fnames, $all_same_package) = (1, 1, 1);
230 12 for (my $ix=0; ;++$ix) {
231 33 ▶ my $lev = scalar(@custom_levels) ? $custom_levels[$ix] : $firstN+$ix;
Branch: 1 branch
BranchTrueFalseTotal
scalar @custom_levels ? : 0 33 33
232 33 ▶ last if !defined($lev); # exhausted @custom_levels;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $lev 0 33 33
233 33 ▶ last if $lev > $lastN; # exhausted range
Branch: 1 branch
BranchTrueFalseTotal
if $lev > $lastN 11 22 33
234 22 my $n = $lev + $nskip; # hide call to this helper func
235 22 my @c = caller($n);
236 22 my ($package, $path) = @c[0,1];
237 22 ▶ last if !defined $package;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $package 1 21 22
238 21 ▶ $all_same_package = 0 if $package ne ($prev_package //= $package);
Branch: 1 branch
BranchTrueFalseTotal
if $package ne ($prev_package //= $package) 8 13 21
Condition66%2/3
$prev_package //= $package
l!l&&r!l&&!r
9120
MC/DC0%0/2
$prev_package //= $package
$prev_package $package
Truth table66%2/3
A //= B
A= $prev_packageB= $package
ABresult
1X1
011
000
239 21 my $pkg = ($package =~ s/.*:://r); # abbreviated package
240 21 ▶ $uniq_pkgtails = 0 if ($pkgtail2full{$pkg} //= $package) ne $package;
Branch: 1 branch
BranchTrueFalseTotal
if ($pkgtail2full{$pkg} //= $package) ne $package 0 21 21
Condition66%2/3
$pkgtail2full{$pkg} //= $package
l!l&&r!l&&!r
5160
MC/DC0%0/2
$pkgtail2full{$pkg} //= $package
$pkgtail2full{$pkg} $package
Truth table66%2/3
A //= B
A= $pkgtail2full{$pkg}B= $package
ABresult
1X1
011
000
241 21 my $fname = ($path =~ s/.*[\\\/]//r);
242 21 ▶ $uniq_fnames = 0 if ($fname2full{$fname} //= $path) ne $path;
Branch: 1 branch
BranchTrueFalseTotal
if ($fname2full{$fname} //= $path) ne $path 0 21 21
Condition66%2/3
$fname2full{$fname} //= $path
l!l&&r!l&&!r
6150
MC/DC0%0/2
$fname2full{$fname} //= $path
$fname2full{$fname} $path
Truth table66%2/3
A //= B
A= $fname2full{$fname}B= $path
ABresult
1X1
011
000
243 21 push @frames, {n=>$n, caller => \@c, pkg => $pkg, fname => $fname};
244 }
245 #if ($all_same_package && $prev_package eq "main") {
246 # foreach (@frames) { $_->{pkg} = "" };
247 #}
248 12 ▶ if (! $uniq_pkgtails) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uniq_pkgtails) 0 12 12
249 0 foreach (@frames) { $_->{pkg} = $_->{caller}->[0] }; # pkg = package
250 }
251 12 ▶ if (! $uniq_fnames) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($uniq_fnames) 0 12 12
252 0 foreach (@frames) { $_->{fname} = $_->{caller}->[1]; } # fname = path
253 }
254 12 ▶ $pkg_obvious = ($all_same_package && !$multiple_btw_importers);
Condition66%2/3
$all_same_package && !$Spreadsheet::Edit::Log::multiple_btw_importers
!ll&&!rl&&r
1110
MC/DC0%0/2
$all_same_package && !$Spreadsheet::Edit::Log::multiple_btw_importers
$all_same_package !$Spreadsheet::Edit::Log::multiple_btw_importers
Truth table66%2/3
A && not(B)
A= $all_same_packageB= !$Spreadsheet::Edit::Log::multiple_btw_importers
ABresult
0X0
100
111
255 #warn dvis '### $pkg_obvious $all_same_package $multiple_btw_importers\n';
256 }
257  
258 #FIXME: Use only ASCII characters if terminal is not UTF enabled?
259 ##Cf DDI ## --or-- provide an environment variable?
260 ## (for when output must go through tools which barf on "wide" chars)
261  
262  
263 12 my $pfx = "";
264 12 my ($prev_pkg, $prev_n);
265 12 for (reverse @frames) { # show outer frame at the left
266 21 my ($n, $caller, $pkg, $fname) = @$_{qw/n caller pkg fname/};
267 21 my $lno = $caller->[2];
268 21 my $path = $caller->[1];
269 #warn dvis '#YY $n $pkg $fname @$caller[0..3]\n';
270 21 my $s;
271 # Show the filename if it does not correspond to the package
272 # e.g. if Foo.pm defines code in packages other than "Foo".
273 # Also eval...
274 21 ▶ if ($fname ne "${pkg}.pm" && $path ne $0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname ne "$pkg.pm" and $path ne $0) 1 20 21
Condition100%3/3
$fname ne "$pkg.pm" and $path ne $0
!ll&&!rl&&r
3171
MC/DC100%2/2
$fname ne "$pkg.pm" and $path ne $0
$fname ne "$pkg.pm" $path ne $0
Truth table100%3/3
A and B
A= $fname ne "$pkg.pm"B= $path ne $0
ABresult
0X0
100
111
275 1 $pkg .= "[$fname]";
276 }
277 #if (substr($fname,0,6) eq "(eval " && index($pkg,"/") < 0) {
278 # $pkg = "${pkg}$fname"; # "Modname(eval 42)"
279 #}
280 21 ▶ if ($pkg_obvious || (defined($prev_pkg) && $pkg eq $prev_pkg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pkg_obvious or defined $prev_pkg and $pkg eq $prev_pkg) { } 3 18 21
Condition83%5/6
$pkg_obvious or defined $prev_pkg and $pkg eq $prev_pkg
l!l&&r!l&&!r
0318
defined $prev_pkg and $pkg eq $prev_pkg
!ll&&!rl&&r
1263
MC/DC66%2/3
$pkg_obvious or defined $prev_pkg and $pkg eq $prev_pkg
$pkg_obvious defined $prev_pkg $pkg eq $prev_pkg
Truth table75%3/4
A or B and C
A= $pkg_obviousB= defined $prev_pkgC= $pkg eq $prev_pkg
ABCresult
1XX1
0111
00X0
0100
281 3 $s = $lno;
282 } else {
283 18 my $package = $caller->[0];
284 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
285 18 ▶ if (my $h = \%{"${package}::SpreadsheetEdit_Log_Options"}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $h = \%{"${package}::SpreadsheetEdit_Log_Options";}) 18 0 18
286 18 ▶ $pkg = $h->{subst_pkg} if $h->{subst_pkg};
Branch: 1 branch
BranchTrueFalseTotal
if $$h{"subst_pkg"} 0 18 18
287 }
288 18 ▶ $s = $s ? " ${pkg}:$lno" : "${pkg}:$lno"
Branch: 1 branch
BranchTrueFalseTotal
$s ? : 0 18 18
289 }
290 21 ▶ $pfx .= $framesep if $pfx;
Branch: 1 branch
BranchTrueFalseTotal
if $pfx 9 12 21
291 21 ▶ if (defined($prev_n)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_n) 9 12 21
292 9 $pfx .= "«" x (($prev_n-$n)-1); # n.b. reverse order (n high to low)
293 }
294 21 $pfx .= $s;
295 21 $prev_pkg = $pkg;
296 21 $prev_n = $n;
297 }
298 12 ▶ if ($start_time) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Spreadsheet::Edit::Log::start_time) 0 12 12
299 0 my $secs = Time::HiRes::time() - $start_time; # floating point
300 0 $pfx = "et=".sprintf("%-5.1f", $secs)." ".$pfx;
301 }
302 12 ▶ if (defined $keep_pfx_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Spreadsheet::Edit::Log::keep_pfx_width) 0 12 12
303 0 my $got = length($pfx);
304 0 ▶ if ((my $shortness = $keep_pfx_width - $got) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $shortness = $Spreadsheet::Edit::Log::keep_pfx_width - $got) > 0) { } 0 0 0
305 0 $pfx = (" " x $shortness).$pfx; # right-align
306 } else {
307 0 $keep_pfx_width = $got;
308 }
309 }
310  
311 12 ▶ foreach (@strings) { oops "undef arg to btw*" unless defined; }
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 22 22
312 12 my $s = join("", @strings);
313 12 my $pad = btwpad();
314 12 $s =~ s/\R\z//s;
315 12 $s =~ s/(\R)(?=.)/${1}${pad}/sg;
316 12 $s = colorize($s, WARN_COLOR);
317  
318 12 "${pad}${pfx}> ${s}";
319 }#_btwN_str_back1
320  
321 0 ▶sub btwN_str($@) { # Just the message string, sans final newline
Subroutine btwN_str: not called CC=1 SCAR=6.9
Pod undocumented
322 0 my ($N, @strings) = @_;
323 0 _btwN_str_back1($N, @strings);
324 }
325 12 ▶sub btwN($@) {
Subroutine btwN: called CC=1 SCAR=0.0
Pod documented
326 12 my ($N, @strings) = @_;
327 12 my $msg = _btwN_str_back1($N, @strings);
328 12 my $fh = _getoptions()->{logdest};
329 12 print $fh $msg, "\n";
330 }
331  
332 5 ▶sub btw(@) { @_ = (0, @_); goto &btwN; }
Subroutine btw: called CC=1 SCAR=0.0
Pod documented
333 1 ▶sub btwbt(@) { @_ = (\99, @_); goto &btwN; }
Subroutine btwbt: called CC=1 SCAR=0.0
Pod documented
334  
335 # Return ref to hash of effective options (READ-ONLY).
336 # If the first argument is a hashref it is shifted off and
337 # used as options which override defaults.
338 42 ▶sub _getoptions {
Subroutine _getoptions: called CC=6 SCAR=17.9
339 42 my $pkg;
340 42 ▶ my $N=1; while (($pkg=caller($N)||oops) eq __PACKAGE__) { ++$N }
Branch: 1 branch
BranchTrueFalseTotal
if (($pkg = caller $N || &oops()) eq __PACKAGE__) 8 42 50
Condition33%1/3
caller $N || &oops()
l!l&&r!l&&!r
5000
MC/DC0%0/2
caller $N || &oops()
caller $N &oops()
Truth table33%1/3
A || B
A= caller $NB= &oops()
ABresult
1X1
011
000
341 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
342 12 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
343 42 my $r = *{$pkg."::SpreadsheetEdit_Log_Options"}{HASH};
344 +{ %backup_defaults,
345 (defined($r) ? %$r : ()),
346 42 ▶ ((@_ && ref($_[0]) eq 'HASH') ? %{shift(@_)} : ())
Branch: 2 branches
BranchTrueFalseTotal
defined $r ? : 40 2 42
@_ && ref $_[0] eq 'HASH' ? : 19 23 42
Condition100%3/3
@_ and ref $_[0] eq "HASH"
!ll&&!rl&&r
14919
MC/DC100%2/2
@_ and ref $_[0] eq "HASH"
@_ ref $_[0] eq "HASH"
Truth table100%3/3
A and B
A= @_B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
347 }
348 }
349  
350 0 ▶sub get_effective_logdest() { _getoptions->{logdest} }
Subroutine get_effective_logdest: not called CC=1 SCAR=6.9
Pod documented
351  
352 # Format a usually-comma-separated list sans enclosing brackets.
353 #
354 # Items are formatted by vis() and thus strings will be "quoted", except that
355 # \"ref to string" inserts the string value without quotes and suppresses
356 # adjacent commas (for inserting fixed annotations).
357 # Object refs in the top two levels are not visualized.
358 #
359 # If the arguments are recognized as a sequence then they are formatted as
360 # Arg1..ArgN instead of Arg1,Arg2,...,ArgN.
361 #
362 27 ▶sub _fmt_list($) {
Subroutine _fmt_list: called CC=17 SCAR=30.6
363 27 ▶ my @items = ref($_[0]) eq 'ARRAY' ? @{$_[0]} : ($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 23 4 27
364 27 ▶ oops if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 27 27
365 27 ▶ return "()" if @items==0;
Branch: 1 branch
BranchTrueFalseTotal
if @items == 0 0 27 27
366 27 ▶ if (my $is_sequential = (@items >= 4)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $is_sequential = @items >= 4) 1 26 27
367 1 my $seq;
368 1 foreach(@items) {
369 1 ▶ $is_sequential=0,last
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ and /^\w+$/ and ($seq //= $items[0])++ eq $_ 1 0 1
Condition22%2/9
defined $_ and /^\w+$/ and ($seq //= $items[0])++ eq $_
!ll&&!rl&&r
100
defined $_ and /^\w+$/
!ll&&!rl&&r
010
$seq //= $items[0]
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $_ and /^\w+$/ and ($seq //= $items[0])++ eq $_
defined $_ /^\w+$/ ($seq //= $items[0])++ eq $_
MC/DC0%0/2
$seq //= $items[0]
$seq $items[0]
Truth table25%1/4
A and B and C
A= defined $_B= /^\w+$/C= ($seq //= $items[0])++ eq $_
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A //= B
A= $seqB= $items[0]
ABresult
1X1
011
000
370 unless defined($_) && /^\w+$/ && ($seq//=$items[0])++ eq $_
371 }
372 1 ▶ if ($is_sequential) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_sequential) 0 1 1
373 0 return visq($items[0])."..".visq($items[-1])
374 }
375 }
376 join "", map{
377 27 my $item = $items[$_];
378 5 ▶ ($_ > 0 && (ref($items[$_-1]) ne 'SCALAR' || ${$items[$_-1]} eq "")
Condition66%2/3
ref $item ne "SCALAR" or ${$item;} eq ""
l!l&&r!l&&!r
502
MC/DC50%1/2
ref $item ne "SCALAR" or ${$item;} eq ""
ref $item ne "SCALAR" ${$item;} eq ""
Truth table66%2/3
A or B
A= ref $item ne "SCALAR"B= ${$item;} eq ""
ABresult
1X1
011
000
379 2 ▶ && (ref($item) ne 'SCALAR' || ${$item} eq "")
Branch: 1 branch
BranchTrueFalseTotal
ref $item eq 'SCALAR' ? : 21 16 37
380 ? "," : ""
381 )
382 37 ▶ .(ref($item) eq 'SCALAR' ? ${$item} : visnew->Pad(" ")->vis($item)
Branch: 1 branch
BranchTrueFalseTotal
$_ > 0 && (ref $items[$_ - 1] ne 'SCALAR' || ${$items[$_ - 1];} eq '') && (ref $item ne 'SCALAR' || ${$item;} eq '') ? : 5 32 37
Condition100%9/9
$_ > 0 and ref $items[$_ - 1] ne "SCALAR" || ${$items[$_ - 1];} eq "" and ref $item ne "SCALAR" || ${$item;} eq ""
!ll&&!rl&&r
3025
$_ > 0 and ref $items[$_ - 1] ne "SCALAR" || ${$items[$_ - 1];} eq ""
!ll&&!rl&&r
2737
ref $items[$_ - 1] ne "SCALAR" or ${$items[$_ - 1];} eq ""
l!l&&r!l&&!r
523
MC/DC100%4/4
$_ > 0 and ref $items[$_ - 1] ne "SCALAR" || ${$items[$_ - 1];} eq "" and ref $item ne "SCALAR" || ${$item;} eq ""
$_ > 0 ref $items[$_ - 1] ne "SCALAR" ${$items[$_ - 1];} eq "" ref $item ne "SCALAR" || ${$item;} eq ""
Truth table83%5/6
A and B or C and D
A= $_ > 0B= ref $items[$_ - 1] ne "SCALAR"C= ${$items[$_ - 1];} eq ""D= ref $item ne "SCALAR" || ${$item;} eq ""
ABCDresult
0XXX0
100X0
11X00
10100
11X11
10111
383 )
384 } 0..$#items;
385 }
386 ## test
387 #foreach ([], [1..5], ['f'..'i'], ['a'], ['a','x']) {
388 # my @items = @$_;
389 # warn avis(@items)," -> ", scalar(_fmt_list(@items)), "\n";
390 # @items = (\"-FIRST-", @items);
391 # warn avis(@items)," -> ", scalar(_fmt_list(@items)), "\n";
392 # splice @items, int(scalar(@items)/2),0, \"-ANN-" if @items >= 1;
393 # warn avis(@items)," -> ", scalar(_fmt_list(@items)), "\n";
394 # push @items, \"-LAST-";
395 # warn avis(@items)," -> ", scalar(_fmt_list(@items)), "\n";
396 #}
397 #die "TEX";
398  
399 #####################################################################
400 # Locate the nearest call to a public sub in the call stack.
401 #
402 # A callback decides what might be a "public" entrypoint (default:
403 # any sub named starting with [a-z]).
404 #
405 # RETURNS
406 # ([frame], [called args]) in array context
407 # [frame] in scalar context
408 #
409 # "frame" means caller(n) results:
410 # 0 1 2 3
411 # package filename linenum subname ...
412 #
413 ##use constant _CALLER_OVERRIDE_CHECK_OK =>
414 ## (defined(&Carp::CALLER_OVERRIDE_CHECK_OK)
415 ## && &Carp::CALLER_OVERRIDE_CHECK_OK);
416  
417 22 ▶sub _nearest_call($$) {
Subroutine _nearest_call: called CC=5 SCAR=16.1
418 22 my ($state, $opts) = @_;
419 22 my $callback = $opts->{is_public_api};
420 22 for (my $lvl=1 ; ; ++$lvl) {
421 106 my @frame = caller($lvl);
422 106 ▶ confess "No public-API sub found" unless defined($frame[0]);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $frame[0] 0 106 106
423 106 my $calling_pkg = $frame[0];
424 106 ▶ my ($called_pkg) = ($frame[3] =~ /^(.*)::/) or next; # eval?
Branch: 1 branch
BranchTrueFalseTotal
my($called_pkg) = $frame[3] =~ /^(.*)::/ or next 106 0 106
425 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
426 #if ((!any{ $_ eq $called_pkg } (__PACKAGE__,$calling_pkg,@{$calling_pkg."::CARP_NOT"}))
427 106 ▶ if ($called_pkg ne __PACKAGE__ && $callback->($state, \@frame)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($called_pkg ne __PACKAGE__ and &$callback($state, \@frame)) 22 84 106
Condition100%3/3
$called_pkg ne __PACKAGE__ and &$callback($state, \@frame)
!ll&&!rl&&r
711322
MC/DC100%2/2
$called_pkg ne __PACKAGE__ and &$callback($state, \@frame)
$called_pkg ne __PACKAGE__ &$callback($state, \@frame)
Truth table100%3/3
A and B
A= $called_pkg ne __PACKAGE__B= &$callback($state, \@frame)
ABresult
0X0
100
111
428 22 return \@frame;
429 }
430 }
431 }
432 1 ▶sub nearest_call(;$) {
Subroutine nearest_call: called CC=1 SCAR=0.0
Pod documented
433 1 my $opts = &_getoptions;
434 1 _nearest_call({}, $opts);
435 }
436  
437 21 ▶sub _abbrev_call_fn_ln_subname($$) {
Subroutine _abbrev_call_fn_ln_subname: called CC=2 SCAR=7.0
438 21 my ($state, $opts) = @_;
439 21 my @results = @{ &_nearest_call($state, $opts) }[1,2,3]; # (fn, lno, subname)
440 21 $results[0] = basename $results[0]; # filename
441 21 $results[2] =~ s/.*:://; # subname
442 21 ▶ $results[2] = $opts->{subname_override} if defined $opts->{subname_override};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$opts{"subname_override"} 0 21 21
443 @results
444 21 }
445 1 ▶sub abbrev_call_fn_ln_subname(;$) {
Subroutine abbrev_call_fn_ln_subname: called CC=1 SCAR=0.0
Pod documented
446 1 my $opts = &_getoptions;
447 1 _abbrev_call_fn_ln_subname({},$opts);
448 }
449  
450 20 ▶sub _fmt_call($;$$) {
Subroutine _fmt_call: called CC=12 SCAR=25.3
451 20 my $opts = shift;
452 20 ▶ confess "Expecting {optOPTIONS} INPUTS optRESULTS" unless @_==1 or @_==2;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ == 1 or @_ == 2 0 20 20
Condition66%2/3
@_ == 1 or @_ == 2
l!l&&r!l&&!r
7130
MC/DC0%0/2
@_ == 1 or @_ == 2
@_ == 1 @_ == 2
Truth table66%2/3
A or B
A= @_ == 1B= @_ == 2
ABresult
1X1
011
000
453 20 my ($inputs, $retvals) = @_;
454 #warn dvis '### $opts\n $inputs\n $retvals';
455  
456 20 my $state = {};
457 20 my ($fn, $lno, $subname) = _abbrev_call_fn_ln_subname($state, $opts);
458 # TODO: Allow supporessing $fn: for specified package(s)???
459 20 my $msg = ">[$fn:$lno] ";
460  
461 19 ▶ my sub myequal($$) {
Subroutine myequal: called CC=3 SCAR=11.0
462 19 ▶ if ((my $r1 = refaddr($_[0])) && (my $r2 = refaddr($_[1]))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r1 = builtin::refaddr($_[0]) and my $r2 = builtin::refaddr($_[1])) { } 16 3 19
Condition66%2/3
my $r1 = builtin::refaddr($_[0]) and my $r2 = builtin::refaddr($_[1])
!ll&&!rl&&r
0316
MC/DC50%1/2
my $r1 = builtin::refaddr($_[0]) and my $r2 = builtin::refaddr($_[1])
my $r1 = builtin::refaddr($_[0]) my $r2 = builtin::refaddr($_[1])
Truth table66%2/3
A and B
A= my $r1 = builtin::refaddr($_[0])B= my $r2 = builtin::refaddr($_[1])
ABresult
0X0
100
111
463 16 return $r1 == $r2; # same object
464 } else {
465 3 return u($_[0]) eq u($_[1]); # string reps eq, or both undef
466 }
467 }
468  
469 20 state $prev_obj;
470 20 ▶ if (defined(my $obj = $opts->{self})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $obj = $$opts{'self'})) { } 19 1 20
471 # N.B. "self" might not be a ref, or might be unblessed
472 19 ▶ if (! myequal($obj, $prev_obj)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (myequal $obj, $prev_obj) 6 13 19
473 # Show the obj address in only the first of a sequence of calls
474 # with the same object.
475 6 my $rep = $opts->{fmt_object}->($state, $obj);
476 6 ▶ if (defined($rep) && refaddr($rep)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rep and builtin::refaddr($rep)) { } 0 6 6
Condition33%1/3
defined $rep and builtin::refaddr($rep)
!ll&&!rl&&r
060
MC/DC0%0/2
defined $rep and builtin::refaddr($rep)
defined $rep builtin::refaddr($rep)
Truth table33%1/3
A and B
A= defined $repB= builtin::refaddr($rep)
ABresult
0X0
100
111
477 0 $msg .= _fmt_list($rep); # Data::Dumper::Interp
478 } else {
479 6 $msg .= $rep;
480 }
481 6 $prev_obj = $obj;
482 6 weaken($prev_obj);
483 }
484 19 $msg .= ".";
485 } else {
486 1 $prev_obj = undef;
487 }
488  
489 20 $msg .= $subname;
490 20 ▶ $msg .= " "._fmt_list($inputs) if @$inputs;
Branch: 1 branch
BranchTrueFalseTotal
if @$inputs 14 6 20
491 20 ▶ oops "terminal newline in last input item" if substr($msg,-1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($msg, -1) eq "\n" 0 20 20
492 20 ▶ if (defined $retvals) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $retvals) 13 7 20
493 13 ▶ $msg .= "()" if @$inputs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if @$inputs == 0 2 11 13
494 13 $msg .= " ==> ";
495 13 $msg .= _fmt_list($retvals);
496 13 ▶ oops "terminal newline in last retvals item" if substr($msg,-1) eq "\n";
Branch: 1 branch
BranchTrueFalseTotal
if substr($msg, -1) eq "\n" 0 13 13
497 }
498 20 $msg."\n"
499 }
500 0 ▶sub fmt_call {
Subroutine fmt_call: not called CC=1 SCAR=6.9
Pod documented
501 0 my $opts = &_getoptions;
502 0 &_fmt_call($opts, @_);
503 }
504  
505 12 ▶sub log_call {
Subroutine log_call: called CC=1 SCAR=0.0
Pod documented
506 12 my $opts = &_getoptions;
507 12 my $fh = $opts->{logdest};
508 12 print $fh &_fmt_call($opts, @_);
509 }
510  
511 8 ▶sub fmt_methcall($;@) {
Subroutine fmt_methcall: called CC=2 SCAR=7.0
Pod documented
512 8 my $opts = &_getoptions;
513 8 ▶ my $obj = shift // croak "Missing 'self' argument\n";
Condition50%1/2
shift() // croak("Missing 'self' argument\n")
l!l
80
MC/DC0%0/1
shift() // croak("Missing 'self' argument\n")
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
514 8 $opts->{self} = $obj;
515 8 &_fmt_call($opts, @_);
516 }
517  
518 8 ▶sub log_methcall {
Subroutine log_methcall: called CC=1 SCAR=0.0
Pod documented
519 8 my $opts = &_getoptions;
520 8 my $fh = $opts->{logdest};
521 8 print $fh &fmt_methcall($opts, @_);
522 }
523  
524 12 1;
525  
526 __END__
« Edit/IO.pm ↑ Summary Edit.pm »