DiaColloDB/Utils.pm

filter: Statementstmt 16.5% 94 / 568 Branchbran 0.0% 0 / 446 Conditioncond 0.0% 0 / 180 MC/DCmcdc 0.0% 0 / 101 Subroutinesub 27.5% 32 / 116 Podpod 84.2% 64 / 76 totaltotal 12.7% 190 / 1487 CC 304 SCAR 41.7
CC
304
Cov
8%
si_str
380.0
tmpfh
210.0
cp_a
182.0
CRAP 64.6
« DiaColloDB/MultiMapFile.pm ↑ Summary DiaColloDB/EnumFile/FixedMap.pm »
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DiaColloDB::Utils.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: generic DiaColloDB utilities
6  
7 package DiaColloDB::Utils;
8 2 ▶use DiaColloDB::Logger;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use IPC::Run;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use File::Basename qw(basename dirname);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use File::Copy qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use File::Spec qw(); ##-- for tmpdir()
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use File::Temp qw(); ##-- for tempdir(), tempfile()
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use Fcntl qw(:DEFAULT SEEK_SET SEEK_CUR SEEK_END);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 ##==============================================================================
26 ## Globals
27  
28 2 our @ISA = qw(Exporter DiaColloDB::Logger);
29 2 our @EXPORT = qw();
30 2 our %EXPORT_TAGS =
31 (
32 fcntl => [qw(fcflags fcread fcwrite fctrunc fccreat fcperl fcopen fcgetfl)],
33 json => [qw(jsonxs loadJsonString loadJsonFile saveJsonString saveJsonFile)],
34 sort => [qw(sortCmd csort_to csortuc_to)],
35 run => [qw(crun opencmd runcmd)],
36 env => [qw(env_set env_push env_pop)],
37 pack => [qw(packsize packsingle packeq packFilterFetch packFilterStore)],
38 math => [qw(isNan isInf isFinite $LOG2 log2 min2 max2 lmax lmin lsum)],
39 list => [qw(luniq sluniq xluniq lcounts)],
40 regex => [qw(regex)],
41 html => [qw(htmlesc)],
42 ddc => [qw(ddc_escape)],
43 time => [qw(s2hms s2timestr timestamp)],
44 file => [qw(file_mtime file_timestamp du_file du_glob copyto moveto copyto_a cp_a fh_flush fh_reopen)],
45 si => [qw(si_str)],
46 pdl => [qw(_intersect_p _union_p _complement_p _setdiff_p),
47 qw(readPdlFile writePdlFile writePdlHeader writeCcsHeader mmzeroes mmtemp),
48 qw(maxval mintype),
49 ],
50 temp => [qw($TMPDIR tmpdir tmpfh tmpfile tmparray tmparrayp tmphash)],
51 jobs => [qw(nCores nJobs sortJobs)],
52 );
53 2 our @EXPORT_OK = map {@$_} values(%EXPORT_TAGS);
54 2 $EXPORT_TAGS{all} = [@EXPORT_OK];
55  
56 ##==============================================================================
57 ## Functions: Fcntl
58  
59 ## $flags = PACKAGE::fcflags($flags)
60 ## + returns Fcntl flags for symbolic string $flags
61 0 ▶sub fcflags {
Subroutine fcflags: not called CC=9 SCAR=45.0
Pod documented
62 0 my $flags = shift;
63 0 ▶ $flags //= 'r';
Condition0%0/2
$flags //= "r"
l!l
00
MC/DC0%0/1
$flags //= "r"
$flags
Truth table0%0/2
A
A= $flags
Aresult
11
00
64 0 ▶ return $flags if ($flags =~ /^[0-9]+$/); ##-- numeric flags are interpreted as Fcntl bitmask
Branch: 1 branch
BranchTrueFalseTotal
if $flags =~ /^[0-9]+$/ 0 0 0
65 0 my $fread = $flags =~ /[r<]/;
66 0 my $fwrite = $flags =~ /[wa>\+]/;
67 0 ▶ my $fappend = ($flags =~ /[a]/ || $flags =~ />>/);
Condition0%0/3
$flags =~ /[a]/ || $flags =~ />>/
l!l&&r!l&&!r
000
MC/DC0%0/2
$flags =~ /[a]/ || $flags =~ />>/
$flags =~ /[a]/ $flags =~ />>/
Truth table0%0/3
A || B
A= $flags =~ /[a]/B= $flags =~ />>/
ABresult
1X1
011
000
68 0 ▶ my $iflags = ($fread
Branch: 3 branches
BranchTrueFalseTotal
$fread ? : 0 0 0
$fwrite ? : 0 0 0
$fwrite ? : 0 0 0
69 ? ($fwrite ? (O_RDWR|O_CREAT) : O_RDONLY)
70 : ($fwrite ? (O_WRONLY|O_CREAT) : 0)
71 );
72 0 ▶ $iflags |= O_TRUNC if ($fwrite && !$fappend);
Branch: 1 branch
BranchTrueFalseTotal
if $fwrite and not $fappend 0 0 0
Condition0%0/3
$fwrite and not $fappend
!ll&&!rl&&r
000
MC/DC0%0/2
$fwrite and not $fappend
$fwrite not $fappend
Truth table0%0/3
A and not(B)
A= $fwriteB= not $fappend
ABresult
0X0
100
111
73 0 return $iflags;
74 }
75  
76 ## $fcflags = fcgetfl($fh)
77 ## + returns Fcntl flags for filehandle $fh
78 0 ▶sub fcgetfl {
Subroutine fcgetfl: not called CC=2 SCAR=17.9
Pod documented
79 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
80 0 my $fh = shift;
81 0 return CORE::fcntl($fh,F_GETFL,0);
82 }
83  
84 ## $bool = fcread($flags)
85 ## + returns true if any read-bits are set for $flags
86 0 ▶sub fcread {
Subroutine fcread: not called CC=2 SCAR=17.9
Pod documented
87 0 my $flags = fcflags(shift);
88 0 ▶ return ($flags&O_RDONLY)==O_RDONLY || ($flags&O_RDWR)==O_RDWR;
Condition0%0/3
($flags & 0) == 0 || ($flags & 2) == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
($flags & 0) == 0 || ($flags & 2) == 2
($flags & 0) == 0 ($flags & 2) == 2
Truth table0%0/3
A || B
A= ($flags & 0) == 0B= ($flags & 2) == 2
ABresult
1X1
011
000
89 }
90  
91 ## $bool = fcwrite($flags)
92 ## + returns true if any write-bits are set for $flags
93 0 ▶sub fcwrite {
Subroutine fcwrite: not called CC=2 SCAR=17.9
Pod documented
94 0 my $flags = fcflags(shift);
95 0 ▶ return ($flags&O_WRONLY)==O_WRONLY || ($flags&O_RDWR)==O_RDWR;
Condition0%0/3
($flags & 1) == 1 || ($flags & 2) == 2
l!l&&r!l&&!r
000
MC/DC0%0/2
($flags & 1) == 1 || ($flags & 2) == 2
($flags & 1) == 1 ($flags & 2) == 2
Truth table0%0/3
A || B
A= ($flags & 1) == 1B= ($flags & 2) == 2
ABresult
1X1
011
000
96 }
97  
98 ## $bool = fctrunc($flags)
99 ## + returns true if truncate-bits are set for $flags
100 0 ▶sub fctrunc {
Subroutine fctrunc: not called CC=1 SCAR=6.9
Pod documented
101 0 my $flags = fcflags(shift);
102 0 return ($flags&O_TRUNC)==O_TRUNC;
103 }
104  
105 ## $bool = fccreat($flags)
106 0 ▶sub fccreat {
Subroutine fccreat: not called CC=1 SCAR=6.9
Pod documented
107 0 my $flags = fcflags(shift);
108 0 return ($flags&O_CREAT)==O_CREAT;
109 }
110  
111 ## $str = fcperl($flags)
112 ## + return perl mode-string for $flags
113 0 ▶sub fcperl {
Subroutine fcperl: not called CC=6 SCAR=37.4
Pod documented
114 0 my $flags = fcflags(shift);
115 0 ▶ return (fcread($flags)
Branch: 5 branches
BranchTrueFalseTotal
fcread($flags) ? : 0 0 0
fcwrite($flags) ? : 0 0 0
fctrunc($flags) ? : 0 0 0
fcwrite($flags) ? : 0 0 0
fctrunc($flags) ? : 0 0 0
116 ? (fcwrite($flags) ##-- +read
117 ? (fctrunc($flags) ##-- +read,+write
118 ? '+>' : '+<') ##-- +read,+write,+/-trunc
119 : '<')
120 : (fcwrite($flags) ##-- -read
121 ? (fctrunc($flags) ##-- -read,+write
122 ? '>' : '>>') ##-- -read,+write,+/-trunc
123 : '<') ##-- -read,-write : default
124 );
125 }
126  
127 ## $fh_or_undef = fcopen($file,$flags)
128 ## $fh_or_undef = fcopen($file,$flags,$mode,$perms)
129 ## + opens $file with fcntl-style flags $flags
130 0 ▶sub fcopen {
Subroutine fcopen: not called CC=8 SCAR=42.8
Pod documented
131 0 my ($file,$flags,$perms) = @_;
132 0 $flags = fcflags($flags);
133 0 ▶ $perms //= (0666 & ~umask);
Condition0%0/2
$perms //= 438 & ~(umask)
l!l
00
MC/DC0%0/1
$perms //= 438 & ~(umask)
$perms
Truth table0%0/2
A
A= $perms
Aresult
11
00
134 0 my $mode = fcperl($flags);
135  
136 0 my ($sysfh);
137 0 ▶ if (ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file) { } 0 0 0
138 ##-- dup an existing filehandle
139 0 $sysfh = $file;
140 }
141 else {
142 ##-- use sysopen() to honor O_CREAT and O_TRUNC
143 0 ▶ sysopen($sysfh, $file, $flags, $perms) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
sysopen $sysfh, $file, $flags, $perms or return undef 0 0 0
144 }
145  
146 ##-- now open perl-fh from system fh
147 0 ▶ open(my $fh, "${mode}&=", fileno($sysfh)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "$mode&=", fileno $sysfh or return undef 0 0 0
148 0 ▶ if (fcwrite($flags) && !fctrunc($flags)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fcwrite($flags) and not fctrunc($flags)) 0 0 0
Condition0%0/3
fcwrite($flags) and not fctrunc($flags)
!ll&&!rl&&r
000
MC/DC0%0/2
fcwrite($flags) and not fctrunc($flags)
fcwrite($flags) not fctrunc($flags)
Truth table0%0/3
A and not(B)
A= fcwrite($flags)B= not fctrunc($flags)
ABresult
0X0
100
111
149 ##-- append mode: seek to end of file
150 0 ▶ seek($fh, 0, SEEK_END) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
seek $fh, 0, 2 or return undef 0 0 0
151 }
152 0 return $fh;
153 }
154  
155 ##==============================================================================
156 ## Functions: JSON
157  
158 ##--------------------------------------------------------------
159 ## JSON: load
160  
161 ## $data = PACKAGE::loadJsonString( $string,%opts)
162 ## $data = PACKAGE::loadJsonString(\$string,%opts)
163 ## + %opts passed to JSON::from_json(), e.g. (relaxed=>0)
164 ## + supports $opts{json} = $json_obj
165 0 ▶sub loadJsonString {
Subroutine loadJsonString: not called CC=4 SCAR=30.0
Pod documented
166 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
167 0 ▶ my $bufr = ref($_[0]) ? $_[0] : \$_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
168 0 my %opts = @_[1..$#_];
169 0 ▶ return $opts{json}->decode($$bufr) if ($opts{json});
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"json"} 0 0 0
170 0 return from_json($$bufr, {utf8=>!utf8::is_utf8($$bufr), relaxed=>1, allow_nonref=>1, %opts});
171 }
172  
173 ## $data = PACKAGE::loadJsonFile($filename_or_handle,%opts)
174 0 ▶sub loadJsonFile {
Subroutine loadJsonFile: not called CC=5 SCAR=34.0
Pod documented
175 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
176 0 my $file = shift;
177 0 ▶ my $fh = ref($file) ? $file : IO::File->new("<$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
178 0 ▶ return undef if (!$fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
179 0 binmode($fh,':raw');
180 0 local $/=undef;
181 0 my $buf = <$fh>;
182 0 ▶ close($fh) if (!ref($file));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $file 0 0 0
183 0 return $that->loadJsonString(\$buf,@_);
184 }
185  
186 ##--------------------------------------------------------------
187 ## JSON: save
188  
189 ## $str = PACKAGE::saveJsonString($data)
190 ## $str = PACKAGE::saveJsonString($data,%opts)
191 ## + %opts passed to JSON::to_json(), e.g. (pretty=>0, canonical=>0)'
192 ## + supports $opts{json} = $json_obj
193 0 ▶sub saveJsonString {
Subroutine saveJsonString: not called CC=3 SCAR=24.8
Pod documented
194 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
195 0 my $data = shift;
196 0 my %opts = @_;
197 0 ▶ return $opts{json}->encode($data) if ($opts{json});
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"json"} 0 0 0
198 0 return to_json($data, {utf8=>1, allow_nonref=>1, allow_unknown=>1, allow_blessed=>1, convert_blessed=>1, pretty=>1, canonical=>1, %opts});
199 }
200  
201 ## $bool = PACKAGE::saveJsonFile($data,$filename_or_handle,%opts)
202 0 ▶sub saveJsonFile {
Subroutine saveJsonFile: not called CC=7 SCAR=40.3
Pod documented
203 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
204 0 my $data = shift;
205 0 my $file = shift;
206 0 ▶ my $fh = ref($file) ? $file : IO::File->new(">$file");
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 0 0 0
207 0 ▶ $that->logconfess("saveJsonFile() failed to open file '$file': $!") if (!$fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
208 0 binmode($fh,':raw');
209 0 ▶ $fh->print($that->saveJsonString($data,@_)) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($that->saveJsonString($data, @_)) or return undef 0 0 0
210 0 ▶ if (!ref($file)) { close($fh) || return undef; }
Branch: 2 branches
BranchTrueFalseTotal
unless (ref $file) 0 0 0
close $fh or return undef 0 0 0
211 0 return 1;
212 }
213  
214 ##--------------------------------------------------------------
215 ## JSON: object
216  
217 ## $json = jsonxs()
218 ## $json = jsonxs(%opts)
219 ## $json = jsonxs(\%opts)
220 0 ▶sub jsonxs {
Subroutine jsonxs: not called CC=4 SCAR=30.0
Pod undocumented
221 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
222 my %opts = (
223 utf8=>1, relaxed=>1, allow_nonref=>1, allow_unknown=>1, allow_blessed=>1, convert_blessed=>1, pretty=>1, canonical=>1,
224 0 ▶ (@_==1 ? %{$_[0]} : @_),
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
225 );
226 0 my $jxs = JSON->new;
227 0 foreach (grep {$jxs->can($_)} keys %opts) {
228 0 $jxs->can($_)->($jxs,$opts{$_});
229 }
230 0 return $jxs;
231 }
232  
233 2 ▶BEGIN { *json = \&jsonxs; }
Subroutine BEGIN: called CC=1 SCAR=0.0
234  
235 ##==============================================================================
236 ## Functions: env
237  
238  
239 ## \%setenv = PACKAGE::env_set(%setenv)
240 0 ▶sub env_set {
Subroutine env_set: not called CC=4 SCAR=30.0
Pod documented
241 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
242 0 my %setenv = @_;
243 0 my ($key,$val);
244 0 ▶ while (($key,$val)=each(%setenv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %setenv) 0 0 0
245 0 ▶ if (!defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val) { } 0 0 0
246 #$that->trace("ENV_UNSET $key");
247 0 delete($ENV{$key});
248 } else {
249 #$that->trace("ENV_SET $key=$val");
250 0 $ENV{$key} = $val;
251 }
252 }
253 0 return \%setenv;
254 }
255  
256 ## \%oldvals = PACKAGE::env_push(%setenv)
257 2 our @env_stack = qw();
258 0 ▶sub env_push {
Subroutine env_push: not called CC=2 SCAR=17.9
Pod documented
259 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
260 0 my %setenv = @_;
261 0 my %oldenv = map {($_=>$ENV{$_})} keys %setenv;
262 0 push(@env_stack, \%oldenv);
263 0 $that->env_set(%setenv);
264 0 return \%oldenv;
265 }
266  
267 ## \%restored = PACKAGE::env_pop(%setenv)
268 0 ▶sub env_pop {
Subroutine env_pop: not called CC=3 SCAR=24.8
Pod documented
269 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
270 0 my $oldvals = pop(@env_stack);
271 0 ▶ $that->env_set(%$oldvals) if ($oldvals);
Branch: 1 branch
BranchTrueFalseTotal
if $oldvals 0 0 0
272 0 return $oldvals;
273 }
274  
275  
276 ##==============================================================================
277 ## Functions: run
278  
279 ## $rc = PACKAGE::runcmd($cmd)
280 ## $rc = PACKAGE::runcmd(@cmd)
281 ## + does log trace at level $TRACE_RUNCMD
282 0 ▶sub runcmd {
Subroutine runcmd: not called CC=2 SCAR=17.9
Pod undocumented
283 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
284 0 $that->trace("CMD ", join(' ',@_));
285 0 return system(@_);
286 }
287  
288 ## $fh_or_undef = PACKAGE::opencmd($cmd)
289 ## $fh_or_undef = PACKAGE::opencmd($mode,@argv)
290 ## + does log trace at level $TRACE_RUNCMD
291 0 ▶sub opencmd {
Subroutine opencmd: not called CC=4 SCAR=30.0
Pod documented
292 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
293 0 $that->trace("CMD ", join(' ',@_));
294 0 my $fh = IO::Handle->new();
295 0 ▶ if ($#_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($#_ > 0) { } 0 0 0
296 0 open($fh,$_[0],$_[1],@_[2..$#_])
297 } else {
298 0 open($fh,$_[0]);
299 }
300 0 ▶ $that->logconfess("opencmd() failed for \`", join(' ',@_), "': $!") if (!$fh);
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
301 0 return $fh;
302 }
303  
304 ## $bool = crun(@IPC_Run_args)
305 ## + wrapper for IPC::Run::run(@IPC_Run_args) with $ENV{LC_ALL}='C'
306 0 ▶sub crun {
Subroutine crun: not called CC=4 SCAR=30.0
Pod documented
307 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
308 $that->trace("RUN ", join(' ',
309 map {
310 0 ▶ (ref($_)
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 0 0
ref $_ eq 'ARRAY' ? : 0 0 0
311 ? (ref($_) eq 'ARRAY'
312 ? join(' ', @$_)
313 : ref($_))
314 : $_)
315 } @_));
316 0 $that->env_push(LC_ALL=>'C');
317 0 my $rc = IPC::Run::run(@_);
318 0 $that->env_pop();
319 0 return $rc;
320 }
321  
322 ## $cmd_prefix = sortCmd($njobs=$DiaColloDB::NJOBS)
323 ## + returns command-prefix for UNIX sort
324 ## + uses environment variable $DIACOLLO_SORT if present, otherwise $SORT if set
325 ## + defualts to system 'sort' command from PATH with appended sortJobs() options
326 0 ▶sub sortCmd {
Subroutine sortCmd: not called CC=4 SCAR=30.0
Pod documented
327 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
328 0 ▶ return $ENV{DIACOLLO_SORT} || $ENV{SORT} || ('sort '.$that->sortJobs(@_));
Condition0%0/5
$ENV{'DIACOLLO_SORT'} || $ENV{'SORT'} || 'sort ' . $that->sortJobs(@_)
l!l
00
$ENV{"DIACOLLO_SORT"} or $ENV{"SORT"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'DIACOLLO_SORT'} || $ENV{'SORT'} || 'sort ' . $that->sortJobs(@_)
$ENV{"DIACOLLO_SORT"} $ENV{"SORT"}
Truth table0%0/3
A or B
A= $ENV{"DIACOLLO_SORT"}B= $ENV{"SORT"}
ABresult
1X1
011
000
329 }
330  
331 ## $bool = csort_to(\@sortargs, \&catcher)
332 ## + runs system sort and feeds resulting lines to \&catcher
333 0 ▶sub csort_to {
Subroutine csort_to: not called CC=2 SCAR=17.9
Pod documented
334 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
335 0 my ($sortargs,$catcher) = @_;
336 0 return crun([$that->sortCmd(),@$sortargs], '>', IPC::Run::new_chunker("\n"), $catcher);
337 }
338  
339 ## $bool = csortuc_to(\@sortargs, \&catcher)
340 ## + runs system sort | uniq -c and feeds resulting lines to \&catcher
341 0 ▶sub csortuc_to {
Subroutine csortuc_to: not called CC=2 SCAR=17.9
Pod documented
342 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
343 0 my ($sortargs,$catcher) = @_;
344 0 return crun([$that->sortCmd(),@$sortargs], '|', [qw(uniq -c)], '>', IPC::Run::new_chunker("\n"), $catcher);
345 }
346  
347  
348 ##==============================================================================
349 ## Functions: pack filters
350  
351 ## $len = PACKAGE::packsize($packfmt)
352 ## $len = PACKAGE::packsize($packfmt,@args)
353 ## + get pack-size for $packfmt with args @args
354 0 ▶sub packsize {
Subroutine packsize: not called CC=2 SCAR=8.1
Pod documented
355 2 ▶ use bytes; #use bytes; ##-- deprecated in perl v5.18.2
Subroutine BEGIN: called CC=1 SCAR=0.0
356 2 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
357 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
358 0 return bytes::length(pack($_[0],@_[1..$#_]));
359 }
360  
361 ## $bool = PACKAGE::packsingle($packfmt)
362 ## $bool = PACKAGE::packsingle($packfmt,@args)
363 ## + guess whether $packfmt is a single-element (scalar) format
364 0 ▶sub packsingle {
Subroutine packsingle: not called CC=3 SCAR=24.8
Pod undocumented
365 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
366 0 ▶ return (packsize($_[0],0)==packsize($_[0],0,0)
Condition0%0/3
packsize($_[0], 0) == packsize($_[0], 0, 0) && !($_[0] =~ /\*|(?:\[(?:[2-9]|[0-9]{2,})\])|(?:[[:alpha:]].*[[:alpha:]])/)
!ll&&!rl&&r
000
MC/DC0%0/2
packsize($_[0], 0) == packsize($_[0], 0, 0) && !($_[0] =~ /\*|(?:\[(?:[2-9]|[0-9]{2,})\])|(?:[[:alpha:]].*[[:alpha:]])/)
packsize($_[0], 0) == packsize($_[0], 0, 0) !($_[0] =~ /\*|(?:\[(?:[2-9]|[0-9]{2,})\])|(?:[[:alpha:]].*[[:alpha:]])/)
Truth table0%0/3
A && not(B)
A= packsize($_[0], 0) == packsize($_[0], 0, 0)B= !($_[0] =~ /\*|(?:\[(?:[2-9]|[0-9]{2,})\])|(?:[[:alpha:]].*[[:alpha:]])/)
ABresult
0X0
100
111
367 && $_[0] !~ m{\*|(?:\[(?:[2-9]|[0-9]{2,})\])|(?:[[:alpha:]].*[[:alpha:]])});
368 }
369  
370 ## $bool = PACKAGE::packeq($packfmt1,$packfmt2,$val=0x123456789abcdef)
371 ## + returns true iff $packfmt1 and $packfmt2 are equivalent for $val
372 0 ▶sub packeq {
Subroutine packeq: not called CC=3 SCAR=24.8
Pod undocumented
373 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
374 0 my ($fmt1,$fmt2,$val) = @_;
375 0 ▶ $val //= 0x12345678;
Condition0%0/2
$val //= 305419896
l!l
00
MC/DC0%0/1
$val //= 305419896
$val
Truth table0%0/2
A
A= $val
Aresult
11
00
376 0 return pack($fmt1,$val) eq pack($fmt2,$val);
377 }
378  
379 ## \&filter_sub = PACKAGE::packFilterStore($pack_template)
380 ## \&filter_sub = PACKAGE::packFilterStore([$pack_template_store, $pack_template_fetch])
381 ## \&filter_sub = PACKAGE::packFilterStore([\&pack_code_store, \&pack_code_fetch])
382 ## + returns a DB_File-style STORE-filter sub for transparent packing of data to $pack_template
383 0 ▶sub packFilterStore {
Subroutine packFilterStore: not called CC=7 SCAR=40.3
Pod documented
384 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
385 0 my $packas = shift;
386 0 ▶ $packas = $packas->[0] if (UNIVERSAL::isa($packas,'ARRAY'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($packas, "ARRAY") 0 0 0
387 0 ▶ return $packas if (UNIVERSAL::isa($packas,'CODE'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($packas, "CODE") 0 0 0
388 0 ▶ return undef if (!$packas || $packas eq 'raw');
Branch: 1 branch
BranchTrueFalseTotal
if not $packas or $packas eq "raw" 0 0 0
Condition0%0/3
not $packas or $packas eq "raw"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $packas or $packas eq "raw"
not $packas $packas eq "raw"
Truth table0%0/3
not(A) or B
A= not $packasB= $packas eq "raw"
ABresult
1X1
011
000
389 0 ▶ if ($that->packsingle($packas)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that->packsingle($packas)) { } 0 0 0
390 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
391 0 ▶ $_ = pack($packas,$_) if (defined($_));
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ 0 0 0
392 0 };
393 } else {
394 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
395 0 ▶ $_ = pack($packas, ref($_) ? @$_ : split(/\t/,$_)) if (defined($_));
Branch: 2 branches
BranchTrueFalseTotal
if defined $_ 0 0 0
ref $_ ? : 0 0 0
396 0 };
397 }
398 }
399  
400 ## \&filter_sub = PACKAGE::packFilterFetch($pack_template)
401 ## \&filter_sub = PACKAGE::packFilterFetch([$pack_template_store, $pack_template_fetch])
402 ## \&filter_sub = PACKAGE::packFilterFetch([\&pack_code_store, \&pack_code_fetch])
403 ## + returns a DB_File-style FETCH-filter sub for transparent unpacking of data from $pack_template
404 0 ▶sub packFilterFetch {
Subroutine packFilterFetch: not called CC=7 SCAR=40.3
Pod documented
405 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
406 0 my $packas = shift;
407 0 ▶ $packas = $packas->[1] if (UNIVERSAL::isa($packas,'ARRAY'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($packas, "ARRAY") 0 0 0
408 0 ▶ return $packas if (UNIVERSAL::isa($packas,'CODE'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($packas, "CODE") 0 0 0
409 0 ▶ return undef if (!$packas || $packas eq 'raw');
Branch: 1 branch
BranchTrueFalseTotal
if not $packas or $packas eq "raw" 0 0 0
Condition0%0/3
not $packas or $packas eq "raw"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $packas or $packas eq "raw"
not $packas $packas eq "raw"
Truth table0%0/3
not(A) or B
A= not $packasB= $packas eq "raw"
ABresult
1X1
011
000
410 0 ▶ if ($that->packsingle($packas)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($that->packsingle($packas)) { } 0 0 0
411 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
412 0 $_ = unpack($packas,$_);
413 0 };
414 } else {
415 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
416 0 $_ = [unpack($packas,$_)];
417 }
418 0 }
419 }
420  
421 ##==============================================================================
422 ## Math stuff
423  
424 0 ▶sub isNan {
Subroutine isNan: not called CC=2 SCAR=10.1
Pod undocumented
425 2 ▶ no warnings qw(uninitialized numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
426 0 ▶ return !($_[0]<=0||$_[0]>=0);
Condition0%0/3
$_[0] <= 0 || $_[0] >= 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[0] <= 0 || $_[0] >= 0
$_[0] <= 0 $_[0] >= 0
Truth table0%0/3
A || B
A= $_[0] <= 0B= $_[0] >= 0
ABresult
1X1
011
000
427 }
428 0 ▶sub isInf {
Subroutine isInf: not called CC=4 SCAR=24.2
Pod undocumented
429 2 ▶ no warnings qw(uninitialized numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
430 0 ▶ return !($_[0]<=0||$_[0]>=0) || ($_[0]==+"INF") || ($_[0]==-"INF");
Condition0%0/9
!($_[0] <= 0 || $_[0] >= 0) || $_[0] == 'INF' || $_[0] == '-INF'
l!l&&r!l&&!r
000
not $_[0] <= 0 || $_[0] >= 0 or $_[0] == "INF"
l!l&&r!l&&!r
000
$_[0] <= 0 || $_[0] >= 0
l!l&&r!l&&!r
000
MC/DC0%0/4
!($_[0] <= 0 || $_[0] >= 0) || $_[0] == 'INF' || $_[0] == '-INF'
$_[0] <= 0 $_[0] >= 0 $_[0] == "INF" $_[0] == '-INF'
Truth table0%0/7
not(A || B) or C || D
A= $_[0] <= 0B= $_[0] >= 0C= $_[0] == "INF"D= $_[0] == '-INF'
ABCDresult
00XX1
1X1X1
011X1
1X011
01011
1X000
01000
431 }
432 0 ▶sub isFinite {
Subroutine isFinite: not called CC=4 SCAR=24.2
Pod undocumented
433 2 ▶ no warnings qw(uninitialized numeric);
Subroutine BEGIN: called CC=1 SCAR=0.0
434 0 ▶ return ($_[0]<=0||$_[0]>=0) && ($_[0]!=+"INF") && ($_[0]!=-"INF");
Condition0%0/9
($_[0] <= 0 || $_[0] >= 0) && $_[0] != 'INF' && $_[0] != '-INF'
!ll&&!rl&&r
000
$_[0] <= 0 || $_[0] >= 0 and $_[0] != "INF"
!ll&&!rl&&r
000
$_[0] <= 0 or $_[0] >= 0
l!l&&r!l&&!r
000
MC/DC0%0/4
($_[0] <= 0 || $_[0] >= 0) && $_[0] != 'INF' && $_[0] != '-INF'
$_[0] <= 0 $_[0] >= 0 $_[0] != "INF" $_[0] != '-INF'
Truth table0%0/7
A or B and C && D
A= $_[0] <= 0B= $_[0] >= 0C= $_[0] != "INF"D= $_[0] != '-INF'
ABCDresult
00XX0
1X0X0
010X0
1X100
01100
1X111
01111
435 }
436  
437 2 our ($LOG2);
438 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
439 2 $LOG2 = log(2.0);
440 }
441  
442 ## $log2 = log2($x)
443 0 ▶sub log2 {
Subroutine log2: not called CC=2 SCAR=17.9
Pod documented
444 0 ▶ return $_[0]==0 ? -inf : log($_[0])/$LOG2;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] == 0 ? : 0 0 0
445 }
446  
447 ## $max2 = max2($x,$y)
448 0 ▶sub max2 {
Subroutine max2: not called CC=2 SCAR=17.9
Pod documented
449 0 ▶ return $_[0] > $_[1] ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 0 0 0
450 }
451  
452 ## $min2 = min2($x,$y)
453 0 ▶sub min2 {
Subroutine min2: not called CC=2 SCAR=17.9
Pod documented
454 0 ▶ return $_[0] < $_[1] ? $_[0] : $_[1];
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 0 0 0
455 }
456  
457 ## $max = lmax(@vals)
458 0 ▶sub lmax {
Subroutine lmax: not called CC=5 SCAR=34.0
Pod undocumented
459 0 my $max = undef;
460 0 foreach (@_) {
461 0 ▶ $max = $_ if (!defined($max) || (defined($_) && $_ > $max));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or defined $_ and $_ > $max 0 0 0
Condition0%0/6
not defined $max or defined $_ and $_ > $max
l!l&&r!l&&!r
000
defined $_ and $_ > $max
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $max or defined $_ and $_ > $max
not defined $max defined $_ $_ > $max
Truth table0%0/4
not(A) or B and C
A= not defined $maxB= defined $_C= $_ > $max
ABCresult
1XX1
0111
00X0
0100
462 }
463 0 return $max;
464 }
465  
466 ## $min = lmin(@vals)
467 0 ▶sub lmin {
Subroutine lmin: not called CC=5 SCAR=34.0
Pod undocumented
468 0 my $min = undef;
469 0 foreach (@_) {
470 0 ▶ $min = $_ if (!defined($min) || (defined($_) && $_ < $min));
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or defined $_ and $_ < $min 0 0 0
Condition0%0/6
not defined $min or defined $_ and $_ < $min
l!l&&r!l&&!r
000
defined $_ and $_ < $min
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $min or defined $_ and $_ < $min
not defined $min defined $_ $_ < $min
Truth table0%0/4
not(A) or B and C
A= not defined $minB= defined $_C= $_ < $min
ABCresult
1XX1
0111
00X0
0100
471 }
472 0 return $min;
473 }
474  
475 ## $sum = lsum(@vals)
476 0 ▶sub lsum {
Subroutine lsum: not called CC=2 SCAR=17.9
Pod undocumented
477 0 my $sum = 0;
478 0 $sum += $_ foreach (grep {defined($_)} @_);
479 0 return $sum;
480 }
481  
482 ##==============================================================================
483 ## Functions: lists
484  
485 ## \@l_uniq = luniq(\@l)
486 ## + returns unique defined elements of @l; @l need not be sorted
487 0 ▶sub luniq {
Subroutine luniq: not called CC=4 SCAR=30.0
Pod documented
488 0 my ($tmp);
489 0 ▶ return [map {defined($tmp) && $tmp eq $_ ? qw() : ($tmp=$_)} sort grep {defined($_)} @{$_[0]//[]}];
Branch: 1 branch
BranchTrueFalseTotal
defined $tmp && $tmp eq $_ ? : 0 0 0
Condition0%0/5
defined $tmp and $tmp eq $_
!ll&&!rl&&r
000
$_[0] // []
l!l
00
MC/DC0%0/2
defined $tmp and $tmp eq $_
defined $tmp $tmp eq $_
MC/DC0%0/1
$_[0] // []
$_[0]
Truth table0%0/3
A and B
A= defined $tmpB= $tmp eq $_
ABresult
0X0
100
111
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
490 }
491  
492 ## \@l_sorted_uniq = sluniq(\@l_sorted)
493 ## + returns unique defined elements of pre-sorted @l
494 0 ▶sub sluniq {
Subroutine sluniq: not called CC=4 SCAR=30.0
Pod documented
495 0 my ($tmp);
496 0 ▶ return [map {defined($tmp) && $tmp eq $_ ? qw() : ($tmp=$_)} grep {defined($_)} @{$_[0]//[]}];
Branch: 1 branch
BranchTrueFalseTotal
defined $tmp && $tmp eq $_ ? : 0 0 0
Condition0%0/5
defined $tmp and $tmp eq $_
!ll&&!rl&&r
000
$_[0] // []
l!l
00
MC/DC0%0/2
defined $tmp and $tmp eq $_
defined $tmp $tmp eq $_
MC/DC0%0/1
$_[0] // []
$_[0]
Truth table0%0/3
A and B
A= defined $tmpB= $tmp eq $_
ABresult
0X0
100
111
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
497 }
498  
499 ## \@l_uniq = xluniq(\@l,\&keyfunc)
500 ## + returns elements of @l with unique defined keys according to \&keyfunc (default=\&overload::StrVal)
501 0 ▶sub xluniq {
Subroutine xluniq: not called CC=5 SCAR=34.0
Pod documented
502 0 my ($l,$keyfunc) = @_;
503 0 ▶ $keyfunc //= \&overload::StrVal;
Condition0%0/2
$keyfunc //= \&overload::StrVal
l!l
00
MC/DC0%0/1
$keyfunc //= \&overload::StrVal
$keyfunc
Truth table0%0/2
A
A= $keyfunc
Aresult
11
00
504 0 my $tmp;
505 return [
506 0 map {$_->[1]}
507 0 ▶ map {defined($tmp) && $tmp->[0] eq $_->[0] ? qw() : ($tmp=$_)}
Branch: 1 branch
BranchTrueFalseTotal
defined $tmp && $tmp->[0] eq $_->[0] ? : 0 0 0
Condition0%0/3
defined $tmp and $tmp->[0] eq $_->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tmp and $tmp->[0] eq $_->[0]
defined $tmp $tmp->[0] eq $_->[0]
Truth table0%0/3
A and B
A= defined $tmpB= $tmp->[0] eq $_->[0]
ABresult
0X0
100
111
508 0 sort {$a->[0] cmp $b->[0]}
509 0 grep {defined($_->[0])}
510 0 map {[$keyfunc->($_),$_]}
511 0 ▶ @{$l//[]}
Condition0%0/2
$l // []
l!l
00
MC/DC0%0/1
$l // []
$l
Truth table0%0/2
A
A= $l
Aresult
11
00
512 ];
513 }
514  
515 ## \%l_counts = lcounts(\@l)
516 ## + return occurrence counts for elements of @l
517 0 ▶sub lcounts {
Subroutine lcounts: not called CC=3 SCAR=24.8
Pod undocumented
518 0 my %counts = qw();
519 0 ▶ ++$counts{$_} foreach (grep {defined($_)} @{$_[0]//[]});
Condition0%0/2
$_[0] // []
l!l
00
MC/DC0%0/1
$_[0] // []
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
520 0 return \%counts;
521 }
522  
523 ##==============================================================================
524 ## Functions: regexes
525  
526 ## $re = regex($re_str)
527 ## + parses "/"-quoted regex $re_str
528 ## + parses modifiers /[gimsadlu] a la ddc
529 0 ▶sub regex {
Subroutine regex: not called CC=6 SCAR=37.4
Pod documented
530 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
531 0 my $re = shift;
532 0 ▶ return $re if (ref($re));
Branch: 1 branch
BranchTrueFalseTotal
if ref $re 0 0 0
533 0 $re =~ s/^\s*\///;
534  
535 0 ▶ my $mods = ($re =~ s/\/([gimsadlux]*)\s*$// ? $1 : '');
Branch: 1 branch
BranchTrueFalseTotal
$re =~ s[/([gimsadlux]*)\s*$][] ? : 0 0 0
536 0 ▶ if ($mods =~ s/g//g) {
Branch: 2 branches
BranchTrueFalseTotal
if ($mods =~ s/g//g) { } 0 0 0
elsif ($mods) { } 0 0 0
537 0 $re = "^(?${mods}:${re})\$"; ##-- parse /g modifier a la ddc
538 } elsif ($mods) {
539 0 $re = "(?${mods}:$re)";
540 }
541  
542 0 return qr{$re};
543 }
544  
545 ##==============================================================================
546 ## Functions: html
547  
548 ## $escaped = htmlesc($str)
549 0 ▶sub htmlesc {
Subroutine htmlesc: not called CC=1 SCAR=6.9
Pod documented
550 ##-- html escapes
551 0 my $str = shift;
552 0 $str =~ s/\&/\&amp;/sg;
553 0 $str =~ s/\'/\&#39;/sg;
554 0 $str =~ s/\"/\&quot;/sg;
555 0 $str =~ s/\</\&lt;/sg;
556 0 $str =~ s/\>/\&gt;/sg;
557 0 return $str;
558 }
559  
560 ##==============================================================================
561 ## Functions: ddc
562  
563 ## $escaped_str = ddc_escape($str)
564 ## $escaped_str = ddc_escape($str, $addQuotes=1)
565 0 ▶sub ddc_escape {
Subroutine ddc_escape: not called CC=5 SCAR=34.0
Pod undocumented
566 0 ▶ shift(@_) if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
567 0 ▶ return $_[0] if ($_[0] =~ /^[a-zA-Z][a-zA-Z0-9]*$/s); ##-- bareword ok
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /^[a-zA-Z][a-zA-Z0-9]*$/s 0 0 0
568 0 my $s = shift;
569 0 $s =~ s/\\/\\\\/g;
570 0 $s =~ s/\'/\\'/g;
571 0 ▶ return !exists($_[1]) || $_[1] ? "'$s'" : $s;
Branch: 1 branch
BranchTrueFalseTotal
!exists $_[1] || $_[1] ? : 0 0 0
Condition0%0/3
not exists $_[1] or $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not exists $_[1] or $_[1]
not exists $_[1] $_[1]
Truth table0%0/3
not(A) or B
A= not exists $_[1]B= $_[1]
ABresult
1X1
011
000
572 }
573  
574 ##==============================================================================
575 ## Functions: time
576  
577 ## $hms = PACKAGE::s2hms($seconds,$sfmt="%06.3f")
578 ## ($h,$m,$s) = PACKAGE::s2hms($seconds,$sfmt="%06.3f")
579 0 ▶sub s2hms {
Subroutine s2hms: not called CC=4 SCAR=30.0
Pod documented
580 0 ▶ shift(@_) if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
581 0 my ($secs,$sfmt) = @_;
582 0 ▶ $sfmt ||= '%06.3f';
Condition0%0/2
$sfmt ||= "%06.3f"
l!l
00
MC/DC0%0/1
$sfmt ||= "%06.3f"
$sfmt
Truth table0%0/2
A
A= $sfmt
Aresult
11
00
583 0 my $h = int($secs/(60*60));
584 0 $secs -= $h*60*60;
585 0 my $m = int($secs/60);
586 0 $secs -= $m*60;
587 0 my $s = sprintf($sfmt, $secs);
588 0 ▶ return wantarray ? ($h,$m,$s) : sprintf("%02d:%02d:%s", $h,$m,$s);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
589 }
590  
591 ## $timestr = PACKAGE::s2timestr($seconds,$sfmt="%f")
592 0 ▶sub s2timestr {
Subroutine s2timestr: not called CC=5 SCAR=34.0
Pod documented
593 0 ▶ shift(@_) if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
594 0 my ($h,$m,$s) = s2hms(@_);
595 0 ▶ if ($h==0 && $m==0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($h == 0 and $m == 0) { } 0 0 0
elsif ($h == 0) { } 0 0 0
Condition0%0/3
$h == 0 and $m == 0
!ll&&!rl&&r
000
MC/DC0%0/2
$h == 0 and $m == 0
$h == 0 $m == 0
Truth table0%0/3
A and B
A= $h == 0B= $m == 0
ABresult
0X0
100
111
596 0 $s =~ s/^0+(?!\.)//;
597 0 return "${s}s";
598 }
599 elsif ($h==0) {
600 0 return sprintf("%2dm%ss",$m,$s)
601 }
602 0 return sprintf("%dh%02dm%ss",$h,$m,$s);
603 }
604  
605 ## $rfc_timestamp = PACAKGE->timestamp()
606 ## $rfc_timestamp = PACAKGE->timestamp($time)
607 0 ▶sub timestamp {
Subroutine timestamp: not called CC=3 SCAR=24.8
Pod documented
608 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
609 0 ▶ return POSIX::strftime("%Y-%m-%dT%H:%M:%SZ", (@_ ? gmtime($_[0]) : gmtime()));
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
610 }
611  
612 ##==============================================================================
613 ## Functions: file
614  
615 ## $mtime = PACKAGE->file_mtime($file_or_fh)
616 0 ▶sub file_mtime {
Subroutine file_mtime: not called CC=3 SCAR=24.8
Pod documented
617 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
618 0 ▶ return (stat($_[0]))[9] // 0;
Condition0%0/2
(stat $_[0])[9] // 0
l!l
00
MC/DC0%0/1
(stat $_[0])[9] // 0
(stat $_[0])[9]
Truth table0%0/2
A
A= (stat $_[0])[9]
Aresult
11
00
619 }
620  
621 ## $timestamp = PACKAGE->file_timestamp($file_or_fh)
622 0 ▶sub file_timestamp {
Subroutine file_timestamp: not called CC=2 SCAR=17.9
Pod documented
623 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
624 0 return timestamp(file_mtime(@_));
625 }
626  
627 ## $nbytes = du_file(@filenames_or_dirnames_or_fhs)
628 0 ▶sub du_file {
Subroutine du_file: not called CC=6 SCAR=37.4
Pod documented
629 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
630 0 my $du = 0;
631 0 foreach (@_) {
632 0 ▶ $du += (!ref($_) && -d $_ ? du_glob("$_/*") : (-s $_))//0;
Branch: 1 branch
BranchTrueFalseTotal
!ref($_) && -d $_ ? : 0 0 0
Condition0%0/5
(!ref($_) && -d $_ ? du_glob("$_/*") : -s $_) // 0
l!l
00
not ref $_ and -d $_
!ll&&!rl&&r
000
MC/DC0%0/2
(!ref($_) && -d $_ ? du_glob("$_/*") : -s $_) // 0
not ref $_ -d $_
Truth table0%0/3
not(A) and B
A= not ref $_B= -d $_
ABresult
111
0X0
100
633 }
634 0 return $du;
635 }
636  
637 ## $nbytes = du_glob(@globs)
638 0 ▶sub du_glob {
Subroutine du_glob: not called CC=2 SCAR=10.9
Pod documented
639 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
640 0 ▶ return du_file(map {glob($_)} @_);
Subroutine BEGIN: called CC=1 SCAR=0.6
641 }
642  
643 ## $bool = PACKAGE->copyto($srcfile, $dstdir, %opts)
644 ## $bool = PACKAGE->copyto(\@srcfiles, $dstdir, %opts)
645 ## + copies file(s) $srcfile (first form) or @srcfiles (second form) to $dstdir, creating $dstdir if it doesn't already exist;
646 ## options %opts:
647 ## (
648 ## from => $from, ##-- replace prefix $from in file(s) with $todir; default=undef: flat copy to $todir
649 ## method => \&method, ##-- use CODE-ref \&method as underlying copy routing; default=\&File::Copy::copy
650 ## label => $label, ##-- report errors as '$label'; (default='copyto()')
651 ## )
652 0 ▶sub copyto {
Subroutine copyto: not called CC=10 SCAR=47.0
Pod documented
653 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
654 0 my ($srcfiles,$todir,%opts) = @_;
655 0 ▶ my $method = $opts{method} || \&File::Copy::copy;
Condition0%0/2
$opts{'method'} || \&File::Copy::copy
l!l
00
MC/DC0%0/1
$opts{'method'} || \&File::Copy::copy
$opts{'method'}
Truth table0%0/2
A
A= $opts{'method'}
Aresult
11
00
656 0 ▶ my $label = $opts{label} || 'copyto()';
Condition0%0/2
$opts{'label'} || 'copyto()'
l!l
00
MC/DC0%0/1
$opts{'label'} || 'copyto()'
$opts{'label'}
Truth table0%0/2
A
A= $opts{'label'}
Aresult
11
00
657 0 my $from = $opts{from};
658 0 my ($src,$dst,$dstdir);
659 0 ▶ foreach $src (UNIVERSAL::isa($srcfiles,'ARRAY') ? @$srcfiles : $srcfiles) {
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($srcfiles, 'ARRAY') ? : 0 0 0
660 0 ▶ if (defined($from)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $from) { } 0 0 0
661 0 ($dst = $src) =~ s{^\Q$from\E}{$todir};
662 } else {
663 0 $dst = "$todir/".basename($src);
664 }
665 0 $dstdir = dirname($dst);
666 0 ▶ -d $dstdir
Branch: 1 branch
BranchTrueFalseTotal
-d $dstdir or make_path($dstdir) or $that->logconfess("${label}: failed to create target directory '${dstdir}': $!") 0 0 0
Condition0%0/3
-d $dstdir or make_path($dstdir)
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $dstdir or make_path($dstdir)
-d $dstdir make_path($dstdir)
Truth table0%0/3
A or B
A= -d $dstdirB= make_path($dstdir)
ABresult
1X1
011
000
667 or make_path($dstdir)
668 or $that->logconfess("$label: failed to create target directory '$dstdir': $!");
669 0 ▶ $method->($src,$dst)
Branch: 1 branch
BranchTrueFalseTotal
&$method($src, $dst) or $that->logconfess("${label}: failed to transfer file '${src}' to to '${dst}': $!") 0 0 0
670 or $that->logconfess("$label: failed to transfer file '$src' to to '$dst': $!");
671 }
672 0 return 1;
673 }
674  
675 ## $bool = PACKAGE->copyto_a($src,$dstdir,%opts)
676 ## + wrapper for PACKAGE->copyto($src,$dstdir, %opts,method=>PACKAGE->can('cp_a'),label=>'copyto_a()')
677 0 ▶sub copyto_a {
Subroutine copyto_a: not called CC=2 SCAR=17.9
Pod documented
678 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
679 0 return $that->copyto(@_, method=>\&cp_a, label=>'copyto_a()');
680 }
681  
682 ## $bool = PACKAGE->moveto($src,$dstdir, %opts)
683 ## + wrapper for PACKAGE->copyto($src,$dstdir, %opts,method=>\&File::Copy::move,label=>'moveto()')
684 0 ▶sub moveto {
Subroutine moveto: not called CC=2 SCAR=17.9
Pod documented
685 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
686 0 return $that->copyto(@_, method=>\&File::Copy::move, label=>'moveto()');
687 }
688  
689 ## $bool = PACKAGE->cp_a($src,$dst)
690 ## $bool = PACKAGE->cp_a($src,$dstdir)
691 ## + copies file $src to $dst, propagating ownership, permissions, and timestamps
692 0 ▶sub cp_a {
Subroutine cp_a: not called CC=13 SCAR=52.0
Pod documented
693 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
694 0 my ($src,$dst) = @_;
695 0 ▶ if (File::Copy->can('syscopy') && File::Copy->can('syscopy') ne File::Copy->can('copy')) {
Branch: 1 branch
BranchTrueFalseTotal
if ("File::Copy"->can("syscopy") and "File::Copy"->can("syscopy") ne "File::Copy"->can("copy")) 0 0 0
Condition0%0/3
"File::Copy"->can("syscopy") and "File::Copy"->can("syscopy") ne "File::Copy"->can("copy")
!ll&&!rl&&r
000
MC/DC0%0/2
"File::Copy"->can("syscopy") and "File::Copy"->can("syscopy") ne "File::Copy"->can("copy")
"File::Copy"->can("syscopy") "File::Copy"->can("syscopy") ne "File::Copy"->can("copy")
Truth table0%0/3
A and B
A= "File::Copy"->can("syscopy")B= "File::Copy"->can("syscopy") ne "File::Copy"->can("copy")
ABresult
0X0
100
111
696 ##-- use File::copy::syscopy() if available
697 0 return File::Copy::syscopy($src,$dst,3);
698 }
699 ##-- copy and then manually propagate file attributes
700 0 ▶ my $rc = File::Copy::copy($src,$dst) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $rc = File::Copy::copy($src, $dst) or return undef 0 0 0
701 0 ▶ $dst = "$dst/".basename($src) if (-d $dst);
Branch: 1 branch
BranchTrueFalseTotal
if -d $dst 0 0 0
702 0 my @stat = stat($src);
703 0 my ($perm,$gid,$atime,$mtime) = @stat[2,5,8,9];
704 0 ▶ my $uid = $>==0 ? $stat[4] : $>; ##-- don't try to set uid unless we're running as root
Branch: 1 branch
BranchTrueFalseTotal
$> == 0 ? : 0 0 0
705 0 ▶ $rc &&= CORE::chown($uid,$gid,$dst)
Branch: 1 branch
BranchTrueFalseTotal
$rc &&= chown($uid, $gid, $dst) or $that->warn("cp_a(): failed to propagate ownership from '${src}' to '${dst}': $!") 0 0 0
Condition0%0/3
$rc &&= chown($uid, $gid, $dst)
!ll&&!rl&&r
000
MC/DC0%0/2
$rc &&= chown($uid, $gid, $dst)
$rc chown($uid, $gid, $dst)
Truth table0%0/3
A &&= B
A= $rcB= chown($uid, $gid, $dst)
ABresult
0X0
100
111
706 or $that->warn("cp_a(): failed to propagate ownership from '$src' to '$dst': $!");
707 0 ▶ $rc &&= CORE::chmod(($perm & 07777), $dst)
Branch: 1 branch
BranchTrueFalseTotal
$rc &&= chmod($perm & 4095, $dst) or $that->warn("cp_a(): failed to propagate persmissions from '${src}' to '${dst}': $!") 0 0 0
Condition0%0/3
$rc &&= chmod($perm & 4095, $dst)
!ll&&!rl&&r
000
MC/DC0%0/2
$rc &&= chmod($perm & 4095, $dst)
$rc chmod($perm & 4095, $dst)
Truth table0%0/3
A &&= B
A= $rcB= chmod($perm & 4095, $dst)
ABresult
0X0
100
111
708 or $that->warn("cp_a(): failed to propagate persmissions from '$src' to '$dst': $!");
709 0 ▶ $rc &&= CORE::utime($atime,$mtime,$dst)
Branch: 1 branch
BranchTrueFalseTotal
$rc &&= utime($atime, $mtime, $dst) or $that->warn("cp_a(): failed to propagate timestamps from '${src}' to '${dst}': $!") 0 0 0
Condition0%0/3
$rc &&= utime($atime, $mtime, $dst)
!ll&&!rl&&r
000
MC/DC0%0/2
$rc &&= utime($atime, $mtime, $dst)
$rc utime($atime, $mtime, $dst)
Truth table0%0/3
A &&= B
A= $rcB= utime($atime, $mtime, $dst)
ABresult
0X0
100
111
710 or $that->warn("cp_a(): failed to propagate timestamps from '$src' to '$dst': $!");
711 0 return $rc;
712 }
713  
714 ## $fh_or_undef = PACKAGE->fh_flush($fh)
715 ## + flushes filehandle $fh using its flush() method if available
716 0 ▶sub fh_flush {
Subroutine fh_flush: not called CC=3 SCAR=24.8
Pod documented
717 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
718 0 my $fh = shift;
719 0 ▶ return UNIVERSAL::can($fh,'flush') ? $fh->flush() : $fh;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can($fh, 'flush') ? : 0 0 0
720 }
721  
722 ## $fh_or_undef = PACKAGE->fh_reopen($fh,$file)
723 ## + closes and re-opens filehandle $fh
724 0 ▶sub fh_reopen {
Subroutine fh_reopen: not called CC=8 SCAR=42.8
Pod documented
725 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
726 0 my ($fh,$file) = @_;
727 0 my $flags = fcgetfl($fh) & (~O_TRUNC);
728 0 my @layers0 = PerlIO::get_layers($fh);
729 0 ▶ CORE::close($fh) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
close $fh or return undef 0 0 0
730 0 ▶ CORE::open($fh, fcperl($flags), $file) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, fcperl($flags), $file or return undef 0 0 0
731 0 my @layers1 = PerlIO::get_layers($fh);
732 0 ▶ while (@layers0 && @layers1 && $layers0[0] eq $layers1[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (@layers0 and @layers1 and $layers0[0] eq $layers1[0]) 0 0 0
Condition0%0/6
@layers0 and @layers1 and $layers0[0] eq $layers1[0]
!ll&&!rl&&r
000
@layers0 and @layers1
!ll&&!rl&&r
000
MC/DC0%0/3
@layers0 and @layers1 and $layers0[0] eq $layers1[0]
@layers0 @layers1 $layers0[0] eq $layers1[0]
Truth table0%0/4
A and B and C
A= @layers0B= @layers1C= $layers0[0] eq $layers1[0]
ABCresult
0XX0
10X0
1100
1111
733 0 shift(@layers0);
734 0 shift(@layers1);
735 }
736 0 binmode($fh,":$_") foreach (@layers1);
737 0 return $fh;
738 }
739  
740  
741  
742 ##==============================================================================
743 ## Utils: SI
744  
745 ## $str = si_str($float)
746 0 ▶sub si_str {
Subroutine si_str: not called CC=19 SCAR=59.4
Pod documented
747 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
748 0 my $x = shift;
749 0 ▶ return sprintf("%.2fY", $x/10**24) if ($x >= 10**24); ##-- yotta
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e+24" 0 0 0
750 0 ▶ return sprintf("%.2fZ", $x/10**21) if ($x >= 10**21); ##-- zetta
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e+21" 0 0 0
751 0 ▶ return sprintf("%.2fE", $x/10**18) if ($x >= 10**18); ##-- exa
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e+18" 0 0 0
752 0 ▶ return sprintf("%.2fP", $x/10**15) if ($x >= 10**15); ##-- peta
Branch: 1 branch
BranchTrueFalseTotal
if $x >= '1000000000000000' 0 0 0
753 0 ▶ return sprintf("%.2fT", $x/10**12) if ($x >= 10**12); ##-- tera
Branch: 1 branch
BranchTrueFalseTotal
if $x >= '1000000000000' 0 0 0
754 0 ▶ return sprintf("%.2fG", $x/10**9) if ($x >= 10**9); ##-- giga
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1000000000 0 0 0
755 0 ▶ return sprintf("%.2fM", $x/10**6) if ($x >= 10**6); ##-- mega
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1000000 0 0 0
756 0 ▶ return sprintf("%.2fk", $x/10**3) if ($x >= 10**3); ##-- kilo
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1000 0 0 0
757 0 ▶ return sprintf("%.2f", $x) if ($x >= 1); ##-- (natural units)
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1 0 0 0
758 0 ▶ return sprintf("%.2fm", $x*10**3) if ($x >= 10**-3); ##-- milli
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "0.001" 0 0 0
759 0 ▶ return sprintf("%.2fu", $x*10**6) if ($x >= 10**-6); ##-- micro
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-06" 0 0 0
760 0 ▶ return sprintf("%.2fn", $x*10**9) if ($x >= 10**-9); ##-- nano
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-09" 0 0 0
761 0 ▶ return sprintf("%.2fp", $x*10**12) if ($x >= 10**-12); ##-- pico
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-12" 0 0 0
762 0 ▶ return sprintf("%.2ff", $x*10**15) if ($x >= 10**-15); ##-- femto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-15" 0 0 0
763 0 ▶ return sprintf("%.2fa", $x*10**18) if ($x >= 10**-18); ##-- atto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-18" 0 0 0
764 0 ▶ return sprintf("%.2fz", $x*10**21) if ($x >= 10**-21); ##-- zepto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-21" 0 0 0
765 0 ▶ return sprintf("%.2fy", $x*10**24) if ($x >= 10**-24); ##-- yocto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-24" 0 0 0
766 0 return sprintf("%.2g", $x); ##-- default
767 }
768  
769 ##==============================================================================
770 ## Functions: pdl: setops
771  
772 ## $pi = CLASS::_intersect_p($p1,$p2)
773 ## $pi = CLASS->_intersect_p($p1,$p2)
774 ## + intersection of 2 piddles; undef is treated as the universal set
775 ## + argument piddles MUST be sorted in ascending order
776 0 ▶sub _intersect_p {
Subroutine _intersect_p: not called CC=4 SCAR=30.0
777 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
778 0 ▶ return (defined($_[0])
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
defined $_[1] ? : 0 0 0
779 ? (defined($_[1])
780 ? $_[0]->v_intersect($_[1]) ##-- v_intersect is 1.5-3x faster than PDL::Primitive::intersect()
781 : $_[0])
782 : $_[1]);
783 }
784 ## $pu = CLASS::_union_p($p1,$p2)
785 ## $pi = CLASS->_intersect_p($p1,$p2)
786 ## + union of 2 piddles; undef is treated as the universal set
787 ## + argument piddles MUST be sorted in ascending order
788 0 ▶sub _union_p {
Subroutine _union_p: not called CC=4 SCAR=30.0
789 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
790 0 ▶ return (defined($_[0])
Branch: 2 branches
BranchTrueFalseTotal
defined $_[0] ? : 0 0 0
defined $_[1] ? : 0 0 0
791 ? (defined($_[1])
792 ? $_[0]->v_union($_[1]) ##-- v_union is 1.5-3x faster than PDL::Primitive::setops($a,'OR',$b)
793 : $_[0])
794 : $_[1]);
795 }
796  
797 ## $pneg = CLASS::_complement_p($p,$N)
798 ## $pneg = CLASS->_complement_p($p,$N)
799 ## + index-piddle negation; undef is treated as the universal set
800 ## + $N is the total number of elements in the index-universe
801 2 ▶BEGIN { *_not_p = *_negate_p = \&_complement_p; }
Subroutine BEGIN: called CC=1 SCAR=0.0
802 0 ▶sub _complement_p {
Subroutine _complement_p: not called CC=4 SCAR=25.6
803 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
804 0 my ($p,$N) = @_;
805 0 ▶ if (!defined($p)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $p) { } 0 0 0
elsif ($p->nelem == 0) { } 0 0 0
806 ##-- neg(\universe) = \emptyset
807 0 return PDL->null->long;
808 }
809 elsif ($p->nelem==0) {
810 ##-- neg(\emptyset) = \universe
811 0 return undef;
812 }
813 else {
814 ##-- non-trivial negation
815 ##
816 ##-- mask: ca. 2.2x faster than v_setdiff
817 2 ▶ no strict 'subs';
Subroutine BEGIN: called CC=1 SCAR=0.0
818 0 my $mask = PDL->ones(PDL::byte(),$N);
819 0 (my $tmp=$mask->index($p)) .= 0;
820 0 return $mask->which;
821 ##
822 ##-- v_setdiff: ca. 68% slower than mask
823 #my $U = sequence($p->type, $N);
824 #return scalar($U->v_setdiff($p));
825 }
826 }
827  
828  
829 ## $pdiff = CLASS::_setdiff_p($a,$b,$N)
830 ## $pdiff = CLASS->_setdiff_p($a,$b,$N)
831 ## + index-piddle difference; undef is treated as the universal set
832 ## + $N is the total number of elements in the index-universe
833 0 ▶sub _setdiff_p {
Subroutine _setdiff_p: not called CC=6 SCAR=37.4
834 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
835 0 my ($a,$b,$N) = @_;
836 0 ▶ if (!defined($a)) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $a) { } 0 0 0
elsif (not defined $b) { } 0 0 0
elsif ($a->nelem == 0) { } 0 0 0
elsif ($b->nelem == 0) { } 0 0 0
837 ##-- \universe - b = \neg(b)
838 0 return _complement_p($b,$N);
839 }
840 elsif (!defined($b)) {
841 ##-- a - \universe = \emptyset
842 0 return PDL->null->long;
843 }
844 elsif ($a->nelem==0) {
845 ##-- \empyset - b = \emptyset
846 0 return $a;
847 }
848 elsif ($b->nelem==0) {
849 ##-- a - \emptyset = a
850 0 return $a;
851 }
852 else {
853 ##-- non-trivial setdiff
854 0 return scalar($a->v_setdiff($b));
855 }
856 }
857  
858 ##==============================================================================
859 ## Functions: pdl: I/O
860  
861 ## $pdl_or_undef = CLASS->readPdlFile($basename, %opts)
862 ## + %opts:
863 ## class=>$class, # one of qw(PDL PDL::CCS::Nd)
864 ## mmap =>$bool, # use mapfraw() (default=1)
865 ## log=>$level, # log-level (default=undef: off)
866 ## ... # other keys passed to CLASS->mapfraw() rsp. CLASS->readfraw()
867 0 ▶sub readPdlFile {
Subroutine readPdlFile: not called CC=9 SCAR=45.0
Pod documented
868 #require PDL::IO::FastRaw;
869 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
870 0 my ($file,%opts) = @_;
871 0 ▶ my $class = $opts{class} // 'PDL';
Condition0%0/2
$opts{"class"} // "PDL"
l!l
00
MC/DC0%0/1
$opts{"class"} // "PDL"
$opts{"class"}
Truth table0%0/2
A
A= $opts{"class"}
Aresult
11
00
872 0 ▶ my $mmap = $opts{mmap} // 1;
Condition0%0/2
$opts{"mmap"} // 1
l!l
00
MC/DC0%0/1
$opts{"mmap"} // 1
$opts{"mmap"}
Truth table0%0/2
A
A= $opts{"mmap"}
Aresult
11
00
873 0 ▶ my $ro = (!$mmap || (exists($opts{ReadOnly}) ? $opts{ReadOnly} : (!-w "$file.hdr"))) || 0;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts{'ReadOnly'} ? : 0 0 0
Condition0%0/5
!$mmap || (exists $opts{'ReadOnly'} ? $opts{'ReadOnly'} : !(-w "$file.hdr")) || 0
l!l
00
not $mmap or exists $opts{"ReadOnly"} ? $opts{"ReadOnly"} : !(-w "$file.hdr")
l!l&&r!l&&!r
000
MC/DC0%0/2
!$mmap || (exists $opts{'ReadOnly'} ? $opts{'ReadOnly'} : !(-w "$file.hdr")) || 0
not $mmap exists $opts{"ReadOnly"} ? $opts{"ReadOnly"} : !(-w "$file.hdr")
Truth table0%0/3
not(A) or B
A= not $mmapB= exists $opts{"ReadOnly"} ? $opts{"ReadOnly"} : !(-w "$file.hdr")
ABresult
1X1
011
000
874 0 $that->vlog($opts{log}, "readPdlFile($file) [class=$class,mmap=$mmap,ReadOnly=$ro]");
875 0 delete @opts{qw(class mmap ReadOnly verboseIO)};
876 0 ▶ return undef if (!-e "$file.hdr");
Branch: 1 branch
BranchTrueFalseTotal
unless -e "$file.hdr" 0 0 0
877 0 ▶ return $mmap ? $class->mapfraw($file,{%opts,ReadOnly=>$ro}) : $class->readfraw($file,\%opts);
Branch: 1 branch
BranchTrueFalseTotal
$mmap ? : 0 0 0
878 }
879  
880 ## $bool = CLASS->writePdlFile($pdl_or_undef, $basename, %opts)
881 ## + unlinks target file(s) if $pdl is not defined
882 ## + %opts:
883 ## log => $bool, # log-level (default=undef: off)
884 ## ... # other keys passed to $pdl->writefraw()
885 0 ▶sub writePdlFile {
Subroutine writePdlFile: not called CC=5 SCAR=34.0
Pod documented
886 #require PDL::IO::FastRaw;
887 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
888 0 my ($pdl,$file,%opts) = @_;
889 0 ▶ if (defined($pdl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdl) { } 0 0 0
890 ##-- write: raw
891 0 $that->vlog($opts{log}, "writePdlFile($file)");
892 0 delete($opts{verboseIO});
893 0 return $pdl->writefraw($file,\%opts);
894 }
895 else {
896 ##-- write: undef: unlink
897 0 $that->vlog($opts{log}, "writePdlFile($file): unlink");
898 0 foreach (grep {-e "file$_"} ('','.hdr','.ix','.ix.hdr','.nz','.nz.hdr','.fits')) {
899 0 ▶ unlink("file$_") or $that->logconfess(__PACKAGE__, "::writePdlFile(): failed to unlink '$file$_': $!");
Branch: 1 branch
BranchTrueFalseTotal
unlink "file$_" or $that->logconfess(__PACKAGE__, "::writePdlFile(): failed to unlink '$file${_}': $!") 0 0 0
900 }
901 }
902 0 return 1;
903 }
904  
905 ## $bool = CLASS->writePdlHeader($filename, $type, $ndims, @dims)
906 ## + writes a PDL::IO::FastRaw-style header $filename (e.g. "pdl.hdr")
907 ## + adapted from PDL::IO::FastRaw::_writefrawhdr()
908 ## + arguments
909 ## $type ##-- PDL::Type or integer
910 ## $ndims ##-- number of piddle dimensions
911 ## @dims ##-- dimension size list, piddle, or ARRAY-ref
912 0 ▶sub writePdlHeader {
Subroutine writePdlHeader: not called CC=7 SCAR=40.3
Pod documented
913 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
914 0 my ($file,$type,$ndims,@dims) = @_;
915 0 ▶ $that->logconfess("writePdlHeader(): missing required parameter (FILE,TYPE,NDIMS,DIMS...)") if (@_ < 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 0 0 0
916 0 ▶ $type = $type->enum if (UNIVERSAL::isa($type,'PDL::Type'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($type, "PDL::Type") 0 0 0
917 0 ▶ @dims = map {UNIVERSAL::isa($_,'PDL') ? $_->list : (UNIVERSAL::isa($_,'ARRAY') ? @$_ : $_)} @dims;
Branch: 2 branches
BranchTrueFalseTotal
UNIVERSAL::isa($_, 'PDL') ? : 0 0 0
UNIVERSAL::isa($_, 'ARRAY') ? : 0 0 0
918 0 ▶ open(my $fh, ">$file")
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">$file" or return undef 0 0 0
919 or return undef;
920 #$that->logconfess("writePdlHeader(): open failed for '$file': $!");
921 0 print $fh join("\n", $type, $ndims, join(' ', @dims), '');
922 0 close($fh);
923 }
924  
925 ## $bool = CLASS->writeCcsHeader($filename, $itype, $vtype, $pdims, %opts)
926 ## + writes a PDL::CCS::IO::FastRaw-style header $filename (e.g. "pdl.hdr")
927 ## + arguments:
928 ## $itype, ##-- PDL::Type for index (default: PDL::CCS::Utils::ccs_indx())
929 ## $vtype, ##-- PDL::Type for values (default: $PDL::IO::Misc::deftype)
930 ## $pdims, ##-- dimension piddle or ARRAY-ref
931 ## + %opts: ##-- passed to PDL::CCS::Nd->newFromWich
932 0 ▶sub writeCcsHeader {
Subroutine writeCcsHeader: not called CC=6 SCAR=37.4
Pod documented
933 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
934 0 ▶ $that->logconfess("writeCcsFile(): missing required parameter (FILE,ITYPE,VTYPE,DIMS...)") if (@_ < 3);
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 0 0 0
935 0 my ($file,$itype,$vtype,$pdims,%opts) = @_;
936 0 ▶ $itype = PDL::CCS::Utils::ccs_indx() if (!defined($itype));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $itype 0 0 0
937 0 ▶ $vtype = $PDL::IO::Misc::deftype if (!defined($vtype));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vtype 0 0 0
938 0 ▶ $pdims = PDL->pdl($itype, $pdims) if (!UNIVERSAL::isa($pdims,'PDL'));
Branch: 1 branch
BranchTrueFalseTotal
unless UNIVERSAL::isa($pdims, "PDL") 0 0 0
939 0 my $ccs = PDL::CCS::Nd->newFromWhich(PDL->zeroes($itype,$pdims->nelem,1),
940 PDL->zeroes($vtype,2),
941 pdims=>$pdims, sorted=>1, steal=>1, %opts);
942 0 return PDL::CCS::IO::Common::_ccsio_write_header($ccs, $file);
943 }
944  
945 ##==============================================================================
946 ## Functions: pdl: mmap temporaries
947  
948 ## $pdl = mmzeroes($file?, $type?, @dims, \%opts?)
949 ## $pdl = $pdl->mmzeroes($file?, $type?, \%opts?)
950 ## + create a temporary mmap()ed pdl using DiaColloDB::PDL::MM; %opts:
951 ## (
952 ## file => $template, ##-- file basename or File::Temp template; default='pdlXXXX'
953 ## suffix => $suffix, ##-- File::Temp::tempfile() suffix (default='.pdl')
954 ## log => $level, ##-- logging verbosity (default=undef: off)
955 ## temp => $bool, ##-- delete on END (default: $file =~ /X{4}/)
956 ## )
957 0 ▶sub mmzeroes {
Subroutine mmzeroes: not called CC=2 SCAR=17.9
Pod documented
958 0 require DiaColloDB::PDL::MM;
959 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
960 0 return DiaColloDB::PDL::MM::new(@_);
961 }
962 0 ▶sub mmtemp {
Subroutine mmtemp: not called CC=2 SCAR=17.9
Pod documented
963 0 require DiaColloDB::PDL::MM;
964 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
965 0 return DiaColloDB::PDL::MM::mmtemp(@_);
966 }
967  
968 ## $bool = mmunlink(@mmfiles)
969 ## $bool = mmunlink($mmpdl,@mmfiles)
970 ## + unlinkes file(s) generated by mmzeroes($basename)
971 0 ▶sub mmunlink {
Subroutine mmunlink: not called CC=2 SCAR=17.9
Pod documented
972 0 require DiaColloDB::PDL::MM;
973 0 ▶ shift if (UNIVERSAL::isa($_[0],__PACKAGE__));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], __PACKAGE__) 0 0 0
974 0 return DiaColloDB::PDL::MM::unlink(@_);
975 }
976  
977 ##==============================================================================
978 ## Functions: pdl: misc
979  
980 ## $type = CLASS->mintype($pdl, @types)
981 ## $type = CLASS->mintype($maxval, @types)
982 ## + returns minimum PDL::Types type from @types required for representing $maxval ($pdl->max if passed as a PDL)
983 ## + @types defaults to all known PDL types
984 0 ▶sub mintype {
Subroutine mintype: not called CC=8 SCAR=42.8
Pod documented
985 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
986 0 my ($arg,@types) = @_;
987 0 ▶ $arg = $arg->max if (UNIVERSAL::isa($arg,'PDL'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($arg, "PDL") 0 0 0
988 0 ▶ @types = map {$_->{ioname}} values(%PDL::Types::typehash) if (!@types);
Branch: 1 branch
BranchTrueFalseTotal
unless @types 0 0 0
989 0 ▶ @types = sort {$a->enum <=> $b->enum} map {ref($_) ? $_ : (PDL->can($_) ? PDL->can($_)->() : qw())} @types;
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 0 0
'PDL'->can($_) ? : 0 0 0
990 0 foreach my $type (@types) {
991 0 ▶ return $type if (maxval($type) >= $arg);
Branch: 1 branch
BranchTrueFalseTotal
if maxval($type) >= $arg 0 0 0
992 }
993 0 return PDL::float(); ##-- float is enough to represent anything, in principle
994 }
995 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
996 2 *PDL::mintype = \&mintype;
997 }
998  
999 ## $maxval = $type->maxval()
1000 ## $maxval = CLASS::maxval($type_or_name)
1001 0 ▶sub maxval {
Subroutine maxval: not called CC=4 SCAR=25.0
Pod documented
1002 2 ▶ no warnings 'pack';
Subroutine BEGIN: called CC=1 SCAR=0.0
1003 0 my $type = shift;
1004 0 ▶ $type = PDL->can($type)->() if (!ref($type) && PDL->can($type));
Branch: 1 branch
BranchTrueFalseTotal
if not ref $type and "PDL"->can($type) 0 0 0
Condition0%0/3
not ref $type and "PDL"->can($type)
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $type and "PDL"->can($type)
not ref $type "PDL"->can($type)
Truth table0%0/3
not(A) and B
A= not ref $typeB= "PDL"->can($type)
ABresult
0X0
100
111
1005 0 ▶ return 'inf' if ($type >= PDL::float());
Branch: 1 branch
BranchTrueFalseTotal
if $type >= PDL::float() 0 0 0
1006 0 my $nbits = 8*length(pack($PDL::Types::pack[$type->enum],0));
1007 0 return (PDL->pdl($type,2)->pow(PDL->sequence($type,$nbits+1))-1)->double->max;
1008 }
1009 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
1010 2 *PDL::Type::maxval = \&maxval;
1011 }
1012  
1013  
1014 ## ($vals,$counts) = $pdl->valcounts()
1015 ## + wrapper for $pdl->flat->qsort->rle() with masking lifted from MUDL::PDL::Smooth
1016 0 ▶sub valcounts {
Subroutine valcounts: not called CC=1 SCAR=6.9
Pod documented
1017 0 my $pdl = shift;
1018 0 my ($counts,$vals) = $pdl->flat->qsort->rle;
1019 0 my $mask = ($counts > 0);
1020 0 return ($vals->where($mask), $counts->where($mask));
1021 }
1022 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
1023 2 ▶ no warnings 'redefine'; ##-- avoid irritating "PDL::valcounts redefined" messages when running together with (legacy) MUDL & DocClassify code
Subroutine BEGIN: called CC=1 SCAR=0.0
1024 2 *PDL::valcounts = \&valcounts;
1025 }
1026  
1027 ##==============================================================================
1028 ## Functions: temporaries
1029  
1030 ## $TMPDIR : global temp directory to use
1031 2 our $TMPDIR = undef;
1032  
1033 ## TMPFILES : temporary files to be unlinked on END
1034 2 our @TMPFILES = qw();
1035 2 ▶END {
Subroutine END: called CC=4 SCAR=26.4
1036 2 foreach (@TMPFILES) {
1037 0 ▶ !-e $_
Branch: 1 branch
BranchTrueFalseTotal
not -e $_ or unlink $_ or __PACKAGE__->logwarn("failed to unlink temporary file $_ in final cleanup") 0 0 0
Condition0%0/3
not -e $_ or unlink $_
l!l&&r!l&&!r
000
MC/DC0%0/2
not -e $_ or unlink $_
not -e $_ unlink $_
Truth table0%0/3
not(A) or B
A= not -e $_B= unlink $_
ABresult
1X1
011
000
1038 or CORE::unlink($_)
1039 or __PACKAGE__->logwarn("failed to unlink temporary file $_ in final cleanup");
1040 }
1041 }
1042  
1043 ## $tmpdir = CLASS->tmpdir()
1044 ## $tmpdir = CLASS_>tmpdir($template, %opts)
1045 ## + in first form, get name of global tempdir ($TMPDIR || File::Spec::tmpdir())
1046 ## + in second form, create and return a new temporary directory via File::Temp::tempdir()
1047 0 ▶sub tmpdir {
Subroutine tmpdir: not called CC=4 SCAR=30.0
Pod documented
1048 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1049 0 ▶ my $tmpdir = $TMPDIR || File::Spec->tmpdir();
Condition0%0/3
$DiaColloDB::Utils::TMPDIR || 'File::Spec'->tmpdir
l!l&&r!l&&!r
000
MC/DC0%0/2
$DiaColloDB::Utils::TMPDIR || 'File::Spec'->tmpdir
$DiaColloDB::Utils::TMPDIR 'File::Spec'->tmpdir
Truth table0%0/3
A || B
A= $DiaColloDB::Utils::TMPDIRB= 'File::Spec'->tmpdir
ABresult
1X1
011
000
1050 0 ▶ return @_ ? File::Temp::tempdir($_[0], DIR=>$tmpdir, @_[1..$#_]) : $tmpdir;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1051 }
1052  
1053 ## $fh = CLASS->tmpfh()
1054 ## $fh = CLASS->tmpfh($template_or_filename, %opts)
1055 ## + get a new temporary filehandle or undef on error
1056 ## + in list context, returns ($fh,$filename) or empty list on error
1057 0 ▶sub tmpfh {
Subroutine tmpfh: not called CC=14 SCAR=53.5
Pod documented
1058 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1059 0 ▶ my $template = shift // 'tmpXXXXX';
Condition0%0/2
shift() // "tmpXXXXX"
l!l
00
MC/DC0%0/1
shift() // "tmpXXXXX"
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1060 0 my ($fh,$filename);
1061 0 ▶ if ($template =~ /X{4}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($template =~ /X{4}/) { } 0 0 0
1062 ##-- use File::Temp::tempfile()
1063 0 ▶ ($fh,$filename) = File::Temp::tempfile($template, DIR=>$that->tmpdir(), @_) or return qw();
Branch: 1 branch
BranchTrueFalseTotal
() = File::Temp::tempfile($template, "DIR", $that->tmpdir, @_) or return () 0 0 0
1064 } else {
1065 ##-- use literal filename, honoring DIR, TMPDIR, and SUFFIX options
1066 0 my %opts = @_;
1067 0 $filename = $template;
1068 0 ▶ do { $opts{DIR} =~ s{/$}{}; $filename = "$opts{DIR}/$filename"; } if ($filename !~ m{^/} && defined($opts{DIR}));
Branch: 1 branch
BranchTrueFalseTotal
if (not $filename =~ m[^/] and defined $opts{"DIR"}) 0 0 0
Condition0%0/3
not $filename =~ m[^/] and defined $opts{"DIR"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $filename =~ m[^/] and defined $opts{"DIR"}
not $filename =~ m[^/] defined $opts{"DIR"}
Truth table0%0/3
not(A) and B
A= not $filename =~ m[^/]B= defined $opts{"DIR"}
ABresult
0X0
100
111
1069 0 ▶ $filename = $that->tmpdir."/".$filename if ($filename !~ m{^/} && $opts{TMPDIR});
Branch: 1 branch
BranchTrueFalseTotal
if not $filename =~ m[^/] and $opts{"TMPDIR"} 0 0 0
Condition0%0/3
not $filename =~ m[^/] and $opts{"TMPDIR"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $filename =~ m[^/] and $opts{"TMPDIR"}
not $filename =~ m[^/] $opts{"TMPDIR"}
Truth table0%0/3
not(A) and B
A= not $filename =~ m[^/]B= $opts{"TMPDIR"}
ABresult
0X0
100
111
1070 0 ▶ $filename .= $opts{SUFFIX} if (defined($opts{SUFFIX}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"SUFFIX"} 0 0 0
1071 0 ▶ CORE::open($fh, ($opts{APPEND} ? '+<' : '+>'), $filename)
Branch: 2 branches
BranchTrueFalseTotal
open $fh, $opts{"APPEND"} ? "+<" : "+>", $filename or $that->logconfess("tmpfh(): open failed for file '${filename}': $!") 0 0 0
$opts{'APPEND'} ? : 0 0 0
1072 or $that->logconfess("tmpfh(): open failed for file '$filename': $!");
1073 0 ▶ push(@TMPFILES, $filename) if ($opts{UNLINK});
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"UNLINK"} 0 0 0
1074 }
1075 0 ▶ return wantarray ? ($fh,$filename) : $fh;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
1076 }
1077  
1078 ## $filename = CLASS->tmpfile()
1079 ## $filename = CLASS->tmpfile($template, %opts)
1080 0 ▶sub tmpfile {
Subroutine tmpfile: not called CC=3 SCAR=24.8
Pod documented
1081 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1082 0 ▶ my ($fh,$filename) = $that->tmpfh(@_) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
() = $that->tmpfh(@_) or return undef 0 0 0
1083 0 $fh->close();
1084 0 return $filename;
1085 }
1086  
1087 ## \@tmparray = CLASS->tmparray($template, %opts)
1088 ## + ties a new temporary array via $class (default='Tie::File::Indexed::JSON')
1089 ## + calls tie(my @tmparray, 'DiaColloDB::Temp::Array', $tmpfilename, %opts)
1090 0 ▶sub tmparray {
Subroutine tmparray: not called CC=8 SCAR=42.8
Pod documented
1091 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1092 0 my ($template,%opts) = @_;
1093  
1094 ##-- load target module
1095 0 ▶ eval { require "DiaColloDB/Temp/Array.pm" }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require "DiaColloDB/Temp/Array.pm" } } or $that->logconfess("tmparray(): failed to load class DiaColloDB::Temp::Array: $@") 0 0 0
1096 or $that->logconfess("tmparray(): failed to load class DiaColloDB::Temp::Array: $@");
1097  
1098 ##-- default options
1099 0 ▶ $template //= 'dcdbXXXXXX';
Condition0%0/2
$template //= "dcdbXXXXXX"
l!l
00
MC/DC0%0/1
$template //= "dcdbXXXXXX"
$template
Truth table0%0/2
A
A= $template
Aresult
11
00
1100 0 ▶ $opts{SUFFIX} //= '.tmpa';
Condition0%0/2
$opts{"SUFFIX"} //= ".tmpa"
l!l
00
MC/DC0%0/1
$opts{"SUFFIX"} //= ".tmpa"
$opts{"SUFFIX"}
Truth table0%0/2
A
A= $opts{"SUFFIX"}
Aresult
11
00
1101 0 ▶ $opts{UNLINK} = 1 if (!exists($opts{UNLINK}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"UNLINK"} 0 0 0
1102 0 ▶ $opts{APPEND} = 0 if (!exists($opts{APPEND}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"APPEND"} 0 0 0
1103  
1104 ##-- tie it up
1105 0 my $tmpfile = $that->tmpfile($template, %opts);
1106 0 ▶ tie(my @tmparray, 'DiaColloDB::Temp::Array', $tmpfile, %opts)
Branch: 1 branch
BranchTrueFalseTotal
tie my @tmparray, "DiaColloDB::Temp::Array", $tmpfile, %opts or $that->logconfess("tmparray(): failed to tie file '${tmpfile}' via DiaColloDB::Temp::Array: $@") 0 0 0
1107 or $that->logconfess("tmparray(): failed to tie file '$tmpfile' via DiaColloDB::Temp::Array: $@");
1108 0 return \@tmparray;
1109 }
1110  
1111 ## \@tmparrayp = CLASS->tmparrayp($template, $packas, %opts)
1112 ## + ties a new temporary integer-array via DiaColloDB::PackedFile)
1113 ## + calls tie(my @tmparray, 'DiaColloDB::PackedFile', $tmpfilename, %opts)
1114 0 ▶sub tmparrayp {
Subroutine tmparrayp: not called CC=9 SCAR=45.0
Pod documented
1115 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1116 0 my ($template,$packas,%opts) = @_;
1117  
1118 ##-- load target module
1119 0 ▶ eval { require "DiaColloDB/PackedFile.pm" }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require "DiaColloDB/PackedFile.pm" } } or $that->logconfess("tmparrayp(): failed to load class DiaColloDB::PackedFile: $@") 0 0 0
1120 or $that->logconfess("tmparrayp(): failed to load class DiaColloDB::PackedFile: $@");
1121  
1122 ##-- default options
1123 0 ▶ $template //= 'dcdbXXXXXX';
Condition0%0/2
$template //= "dcdbXXXXXX"
l!l
00
MC/DC0%0/1
$template //= "dcdbXXXXXX"
$template
Truth table0%0/2
A
A= $template
Aresult
11
00
1124 0 ▶ $opts{SUFFIX} //= '.pf';
Condition0%0/2
$opts{"SUFFIX"} //= ".pf"
l!l
00
MC/DC0%0/1
$opts{"SUFFIX"} //= ".pf"
$opts{"SUFFIX"}
Truth table0%0/2
A
A= $opts{"SUFFIX"}
Aresult
11
00
1125 0 ▶ $opts{UNLINK} = 1 if (!exists($opts{UNLINK}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"UNLINK"} 0 0 0
1126 0 ▶ $opts{APPEND} = 0 if (!exists($opts{APPEND}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"APPEND"} 0 0 0
1127  
1128 ##-- tie it up
1129 0 my $tmpfile = $that->tmpfile($template, %opts);
1130 0 ▶ my $mode = 'rw'.($opts{APPEND} ? 'a' : '');
Branch: 1 branch
BranchTrueFalseTotal
$opts{'APPEND'} ? : 0 0 0
1131 0 ▶ tie(my @tmparray, 'DiaColloDB::PackedFile', $tmpfile, $mode, packas=>$packas, temp=>$opts{UNLINK}, %opts)
Branch: 1 branch
BranchTrueFalseTotal
tie my @tmparray, "DiaColloDB::PackedFile", $tmpfile, $mode, "packas", $packas, "temp", $opts{"UNLINK"}, %opts or $that->logconfess("tmparrayp(): failed to tie file '${tmpfile}' via DiaColloDB::PackedFile: $@") 0 0 0
1132 or $that->logconfess("tmparrayp(): failed to tie file '$tmpfile' via DiaColloDB::PackedFile: $@");
1133 0 return \@tmparray;
1134 }
1135  
1136 ## \%tmphash = CLASS->tmphash($template, %opts)
1137 ## + ties a new temporary hash via $class (default='DB_File')
1138 ## + calls tie(my @tmparray, $class, $tmpfilename, temp=>1, %opts)
1139 0 ▶sub tmphash {
Subroutine tmphash: not called CC=10 SCAR=47.0
Pod documented
1140 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1141 0 my ($template,%opts) = @_;
1142  
1143 ##-- load target module
1144 0 ▶ eval { require "DiaColloDB/Temp/Hash.pm" }
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require "DiaColloDB/Temp/Hash.pm" } } or $that->logconfess("tmparray(): failed to load class DiaColloDB::Temp::Hash: $@") 0 0 0
1145 or $that->logconfess("tmparray(): failed to load class DiaColloDB::Temp::Hash: $@");
1146  
1147 ##-- default options
1148 0 ▶ $template //= 'dcdbXXXXXX';
Condition0%0/2
$template //= "dcdbXXXXXX"
l!l
00
MC/DC0%0/1
$template //= "dcdbXXXXXX"
$template
Truth table0%0/2
A
A= $template
Aresult
11
00
1149 0 ▶ $opts{SUFFIX} //= '.tmph';
Condition0%0/2
$opts{"SUFFIX"} //= ".tmph"
l!l
00
MC/DC0%0/1
$opts{"SUFFIX"} //= ".tmph"
$opts{"SUFFIX"}
Truth table0%0/2
A
A= $opts{"SUFFIX"}
Aresult
11
00
1150 0 ▶ $opts{UNLINK} = 1 if (!exists($opts{UNLINK}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"UNLINK"} 0 0 0
1151 0 ▶ $opts{APPEND} = 0 if (!exists($opts{APPEND}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"APPEND"} 0 0 0
1152 0 ▶ $opts{flags} //= fcflags('rw'.($opts{APPEND} ? 'a' : ''));
Branch: 1 branch
BranchTrueFalseTotal
$opts{'APPEND'} ? : 0 0 0
Condition0%0/3
$opts{"flags"} //= fcflags('rw' . ($opts{"APPEND"} ? "a" : ""))
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"flags"} //= fcflags('rw' . ($opts{"APPEND"} ? "a" : ""))
$opts{"flags"} fcflags('rw' . ($opts{"APPEND"} ? "a" : ""))
Truth table0%0/3
A //= B
A= $opts{"flags"}B= fcflags('rw' . ($opts{"APPEND"} ? "a" : ""))
ABresult
1X1
011
000
1153  
1154 ##-- tie it up
1155 0 my $tmpfile = $that->tmpfile($template, %opts);
1156 0 ▶ tie(my %tmphash, 'DiaColloDB::Temp::Hash', $tmpfile, %opts)
Branch: 1 branch
BranchTrueFalseTotal
tie my %tmphash, "DiaColloDB::Temp::Hash", $tmpfile, %opts or $that->logconfess("tmphash(): failed to tie file '${tmpfile}' via DiaColloDB::Temp::Hash: $@") 0 0 0
1157 or $that->logconfess("tmphash(): failed to tie file '$tmpfile' via DiaColloDB::Temp::Hash: $@");
1158 0 return \%tmphash;
1159 }
1160  
1161 ##==============================================================================
1162 ## Functions: jobs: parallelization
1163  
1164 ## %NCORES : cache for nCores() utility ($cpuinfo_file=>$n, ...)
1165 2 our %NCORES = qw();
1166  
1167 ## $ncores = PACKAGE::nCores()
1168 ## $ncores = PACKAGE::nCores($proc_cpuinfo_filename)
1169 ## + returns the number of CPU cores on the system according to /proc/cpuinfo, or zero if unavailable
1170 ## + caches result in %NCORES
1171 0 ▶sub nCores {
Subroutine nCores: not called CC=10 SCAR=47.0
Pod documented
1172 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1173 0 ▶ my $filename = shift || '/proc/cpuinfo';
Condition0%0/2
shift() || '/proc/cpuinfo'
l!l
00
MC/DC0%0/1
shift() || '/proc/cpuinfo'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1174 0 ▶ return $NCORES{$filename} if (exists($NCORES{$filename}));
Branch: 1 branch
BranchTrueFalseTotal
if exists $DiaColloDB::Utils::NCORES{$filename} 0 0 0
1175  
1176 0 ▶ if (CORE::open(my $fh, "<$filename")) {
Branch: 2 branches
BranchTrueFalseTotal
if (open my $fh, "<$filename") { } 0 0 0
elsif (open my $pipefh, 'nproc|') { } 0 0 0
1177 0 my $ncores = 0;
1178 0 ▶ while (defined($_=<$fh>)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
1179 0 ▶ ++$ncores if (/^processor\s*:/);
Branch: 1 branch
BranchTrueFalseTotal
if /^processor\s*:/ 0 0 0
1180 }
1181 0 close($fh);
1182 0 $NCORES{$filename} = $ncores;
1183 }
1184 elsif (CORE::open(my $pipefh, "nproc|")) {
1185 0 my $ncores = <$pipefh>;
1186 0 chomp $ncores;
1187 0 close($pipefh);
1188 0 ▶ $NCORES{$filename} = $NCORES{'nproc|'} = $ncores if ($ncores);
Branch: 1 branch
BranchTrueFalseTotal
if $ncores 0 0 0
1189 }
1190 0 ▶ return ($NCORES{$filename} //= 0);
Condition0%0/2
$DiaColloDB::Utils::NCORES{$filename} //= 0
l!l
00
MC/DC0%0/1
$DiaColloDB::Utils::NCORES{$filename} //= 0
$DiaColloDB::Utils::NCORES{$filename}
Truth table0%0/2
A
A= $DiaColloDB::Utils::NCORES{$filename}
Aresult
11
00
1191 }
1192  
1193 ## $njobs = PACKAGE::nJobs($njobs=$DiaColloDB::NJOBS)
1194 ## + gets non-negative number of jobs for user request $njobs (default=-1)
1195 ## + returns nCores() if $njobs is negative
1196 ## + returns int($njobs*nCores()) if (0 < $njobs < 1)
1197 ## + otherwise returns $njobs+0
1198 0 ▶sub nJobs {
Subroutine nJobs: not called CC=7 SCAR=40.3
Pod documented
1199 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1200 0 ▶ my $njobs = @_ ? shift : $DiaColloDB::NJOBS;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1201 0 ▶ $njobs //= -1;
Condition0%0/2
$njobs //= -1
l!l
00
MC/DC0%0/1
$njobs //= -1
$njobs
Truth table0%0/2
A
A= $njobs
Aresult
11
00
1202 0 ▶ return $that->nCores() if ($njobs < 0);
Branch: 1 branch
BranchTrueFalseTotal
if $njobs < 0 0 0 0
1203 0 ▶ return $njobs*$that->nCores if (0 < $njobs && $njobs < 1);
Branch: 1 branch
BranchTrueFalseTotal
if 0 < $njobs and $njobs < 1 0 0 0
Condition0%0/3
0 < $njobs and $njobs < 1
!ll&&!rl&&r
000
MC/DC0%0/2
0 < $njobs and $njobs < 1
0 < $njobs $njobs < 1
Truth table0%0/3
A and B
A= 0 < $njobsB= $njobs < 1
ABresult
0X0
100
111
1204 0 return int($njobs+0);
1205 }
1206  
1207 ## $sort_parallel_option = sortJobs($njobs=$DiaColloDB::NJOBS)
1208 ## + returns --parallel option for 'sort' calls to use $njobs jobs
1209 0 ▶sub sortJobs {
Subroutine sortJobs: not called CC=7 SCAR=40.3
Pod documented
1210 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
1211 0 ▶ my $njobs = @_ ? shift : $DiaColloDB::NJOBS;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1212 0 ▶ my $args = (!$njobs || $njobs < 1 ? '' : ("--parallel=".nJobs($njobs)));
Branch: 1 branch
BranchTrueFalseTotal
!$njobs || $njobs < 1 ? : 0 0 0
Condition0%0/3
not $njobs or $njobs < 1
l!l&&r!l&&!r
000
MC/DC0%0/2
not $njobs or $njobs < 1
not $njobs $njobs < 1
Truth table0%0/3
not(A) or B
A= not $njobsB= $njobs < 1
ABresult
1X1
011
000
1213 0 ▶ return $args ? ($args) : qw() if (wantarray);
Branch: 2 branches
BranchTrueFalseTotal
if wantarray 0 0 0
$args ? : 0 0 0
1214 0 return $args;
1215 }
1216  
1217 ##==============================================================================
1218 ## Footer
1219 2 1; ##-- be happy
« DiaColloDB/MultiMapFile.pm ↑ Summary DiaColloDB/EnumFile/FixedMap.pm »