CAB/Utils.pm

filter: Statementstmt 13.8% 40 / 288 Branchbran 0.0% 0 / 186 Conditioncond 0.0% 0 / 97 MC/DCmcdc 0.0% 0 / 49 Subroutinesub 20.8% 10 / 48 Podpod 17.1% 6 / 35 totaltotal 7.9% 56 / 703 CC 137 SCAR 47.5
CC
137
Cov
7%
deep_encode
342.0
si_str
342.0
deep_decode
306.0
CRAP 115.8
« CAB/Format/XmlLing.pm ↑ Summary CAB/Analyzer/TokPP/Perl.pm »
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Utils.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: generic DTA::CAB utilities
6  
7 package DTA::CAB::Utils;
8 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Encode qw(encode decode);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use POSIX qw(strftime); ##-- for strftime
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 ##==============================================================================
17 ## Globals
18 ##==============================================================================
19  
20 1 our @ISA = qw(Exporter);
21 1 our @EXPORT= qw();
22 1 our %EXPORT_TAGS =
23 (
24 minmax => [qw(min2 max2)],
25 xml => [qw(xml_safe_string xml_escape)],
26 libxml => [qw(libxml_parser libxml_doc libxml_xpnodes libxml_xpnode libxml_xpvalue libxml_xpcontext)],
27 libxslt => [qw(xsl_stylesheet)],
28 data => [qw(path_value path_parse)],
29 encode => [qw(deep_encode deep_decode deep_recode deep_utf8_upgrade)],
30 profile => [qw(si_str profile_str)],
31 version => [qw(cab_version)],
32 threads => [qw(threads_enabled downup)],
33 temp => [qw(tmpfsdir tmpfsfile mktmpfsdir)],
34 getopt => [qw(GetArrayOptions GetStringOptions)],
35 proc => [qw(mstat memsize memrss pid_cmd)],
36 files => [qw(fhbits file_mtime)],
37 time => [qw(timestamp_str emavg)]
38 );
39 1 our @EXPORT_OK = map {@$_} values(%EXPORT_TAGS);
40 1 $EXPORT_TAGS{all} = [@EXPORT_OK];
41  
42 ##==============================================================================
43 ## Functions: options
44 ##==============================================================================
45  
46 ## $bool = GetArrayOptions(\@pseudo_argv, %options)
47 ## + wrapper for Getopt::Long::GetOptionsFromArray()
48 0 ▶sub GetArrayOptions {
Subroutine GetArrayOptions: not called CC=1 SCAR=6.9
Pod undocumented
49 0 return Getopt::Long::GetOptionsFromArray(@_);
50 }
51  
52 ## $bool = GetStringOptions($argv_string, %options)
53 ## ($bool,\@args) = GetStringOptions($argv_string, %options)
54 ## + wrapper for Getopt::Long::GetOptionsFromString()
55 0 ▶sub GetStringOptions {
Subroutine GetStringOptions: not called CC=1 SCAR=6.9
Pod undocumented
56 0 return Getopt::Long::GetOptionsFromString(@_);
57 }
58  
59 ##==============================================================================
60 ## Functions: minmax
61 ##==============================================================================
62  
63 0 ▶sub min2 { return $_[0]<$_[1] ? $_[0] : $_[1]; }
Subroutine min2: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] < $_[1] ? : 0 0 0
64 0 ▶sub max2 { return $_[0]>$_[1] ? $_[0] : $_[1]; }
Subroutine max2: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] > $_[1] ? : 0 0 0
65  
66 ##==============================================================================
67 ## Functions: temporaries
68 ##==============================================================================
69  
70 ## $dir = tmpfsdir()
71 ## + gets root temporary directory:
72 ## first writable directory among @ENV{qw(CAB_TMPDIR TMPDIR TMP)},"/tmpfs","/dev/shm","/tmp","."
73 ## + returns undef if none of the above succeeds
74 0 ▶sub tmpfsdir {
Subroutine tmpfsdir: not called CC=5 SCAR=34.0
Pod undocumented
75 0 foreach (@ENV{qw(CAB_TMPDIR TMP TMPDIR)},qw(/tmp .)) { #/tmpfs /dev/shm
76 0 ▶ return $_ if (defined($_) && -d $_ && -w $_);
Branch: 1 branch
BranchTrueFalseTotal
if defined $_ and -d $_ and -w $_ 0 0 0
Condition0%0/6
defined $_ and -d $_ and -w $_
!ll&&!rl&&r
000
defined $_ and -d $_
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ and -d $_ and -w $_
defined $_ -d $_ -w $_
Truth table0%0/4
A and B and C
A= defined $_B= -d $_C= -w $_
ABCresult
0XX0
10X0
1100
1111
77 }
78 0 return undef;
79 }
80  
81 ## $dir = mktmpfsdir($template,%args)
82 ## + creates new temp directory in tmpfsdir()
83 0 ▶sub mktmpfsdir {
Subroutine mktmpfsdir: not called CC=2 SCAR=17.9
Pod undocumented
84 0 my ($template,%args) = @_;
85 0 ▶ $template = "tmpXXXXX" if (!defined($template));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $template 0 0 0
86 0 return File::Temp::tempdir($template, DIR=>tmpfsdir(), %args);
87 }
88  
89 ## $filename = tmpfsfile($template,%args)
90 ## ($fh,$file) = tmpfsfile($template,%args)
91 ## + default %args = (DIR=>tmpfsdir())
92 0 ▶sub tmpfsfile {
Subroutine tmpfsfile: not called CC=2 SCAR=12.2
Pod undocumented
93 1 ▶ use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 0 my ($template,%args) = @_;
95 0 my ($fh,$file) = File::Temp::tempfile($template, DIR=>tmpfsdir(), %args);
96 0 ▶ return ($fh,$file) if (wantarray);
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
97 0 $fh->close();
98 0 return $file;
99 }
100  
101 ##==============================================================================
102 ## Functions: threads & semaphores
103 ##==============================================================================
104  
105 ## $bool = threads_enabled()
106 ## + should return a true value iff the 'threads' module has been loaded
107 ## + realls just checks for $threads::VERSION
108 ## + thread support is basically BROKEN (see the v1.16-threads-argh branch for a start)
109 0 ▶sub threads_enabled {
Subroutine threads_enabled: not called CC=1 SCAR=6.9
Pod undocumented
110 0 return defined($threads::VERSION);
111 }
112  
113  
114 ## @rc = downup($semaphore,\&sub,$count=1) ##-- list context
115 ## $rc = downup($semaphore,\&sub,$count=1) ##-- scalar context
116 ## + wraps sub() call in { $semaphore->down() ... $semaphore->up() }, catching die()
117 ## + thread support is basically BROKEN (see the v1.16-threads-argh branch for a start)
118 0 ▶sub downup {
Subroutine downup: not called CC=5 SCAR=34.0
Pod undocumented
119 0 my ($sem,$sub,$count) = @_;
120 0 ▶ $count = 1 if (!defined($count));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $count 0 0 0
121 0 DTA::CAB->trace("downup($sem): DOWN");
122 0 $sem->down($count);
123 0 my (@rc);
124 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
125 0 eval { @rc = $sub->(); };
126 } else {
127 0 eval { $rc[0] = $sub->(); };
128 }
129 0 DTA::CAB->trace("downup($sem): UP");
130 0 $sem->up($count);
131 0 ▶ die ($@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
132 0 ▶ return wantarray ? @rc : $rc[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
133 }
134  
135  
136 ##==============================================================================
137 ## Functions: version dump
138 ##==============================================================================
139  
140 ## $str = cab_version(%opts)
141 ## + %opts:
142 ## program => $program_name, ##-- default: basename($0) (undef for no report)
143 ## program_version => $program_version, ##-- default: undef (don't report)
144 ## author => $author, ##-- default: $DTA::CAB::Utils::CAB_AUTHOR
145 1 our $CAB_AUTHOR = "Bryan Jurish <jurish\@bbaw.de>";
146 0 ▶sub cab_version {
Subroutine cab_version: not called CC=6 SCAR=37.4
Pod undocumented
147 0 my %opts = @_;
148 0 ▶ $opts{program} = basename($0) if (!exists($opts{program}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"program"} 0 0 0
149 0 ▶ $opts{author} = $CAB_AUTHOR if (!exists($opts{author}));
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts{"author"} 0 0 0
150 return
151 (
152 ($opts{program}
153 ? ($opts{program}
154 .($opts{program_version} ? " version $opts{program_version}" : '')
155 0 ▶ .($opts{author} ? " by $opts{author}" : '')
Branch: 3 branches
BranchTrueFalseTotal
$opts{'program'} ? : 0 0 0
$opts{'program_version'} ? : 0 0 0
$opts{'author'} ? : 0 0 0
156 ."\n")
157 : '')
158 ." : DTA::CAB version $DTA::CAB::VERSION\n"
159 ." : $DTA::CAB::SVNVERSION\n"
160 );
161 }
162  
163 ##==============================================================================
164 ## Functions: XML strings
165 ##==============================================================================
166  
167 ## $safe = xml_safe_string($str)
168 ## + returns an XML-safe string
169 0 ▶sub xml_safe_string {
Subroutine xml_safe_string: not called CC=1 SCAR=6.9
Pod documented
170 0 my $s = shift;
171 0 $s =~ s/\:\:/\./g;
172 0 $s =~ s/[\s\/\\]/_/g;
173 0 return $s;
174 }
175  
176 ## $xmlstr = xml_escape($str)
177 0 ▶sub xml_escape {
Subroutine xml_escape: not called CC=1 SCAR=6.9
Pod undocumented
178 0 my $s = shift;
179 0 $s =~ s/\&(?![\w\#]+\;)/\&amp;/g;
180 0 $s =~ s/\'/\&apos;/g;
181 0 $s =~ s/\"/\&quot;/g;
182 0 $s =~ s/\</\&lt;/g;
183 0 $s =~ s/\>/\&gt;/g;
184 0 return $s;
185 }
186  
187 ##==============================================================================
188 ## Functions: Deep recoding
189 ##==============================================================================
190  
191 ## $decoded = deep_decode($encoding,$thingy,%options)
192 ## + %options:
193 ## force => $bool, ##-- decode even if the utf8 flag is set
194 ## skipvals => \@vals, ##-- don't decode (or recurse into) $val (overrides $force)
195 ## skiprefs => \@refs, ##-- don't decode (or recurse into) $$ref (overrides $force)
196 ## skippkgs => \@pkgs, ##-- don't decode (or recurse into) anything of package $pkg (overrides $force)
197 0 ▶sub deep_decode {
Subroutine deep_decode: not called CC=17 SCAR=57.2
Pod documented
198 0 my ($enc,$thingy,%opts) = @_;
199 0 ▶ my %skipvals = defined($opts{skipvals}) ? (map {($_=>undef)} @{$opts{skipvals}}) : qw();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'skipvals'} ? : 0 0 0
200 0 ▶ my %skiprefs = defined($opts{skiprefs}) ? (map {($_=>undef)} @{$opts{skiprefs}}) : qw();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'skiprefs'} ? : 0 0 0
201 0 ▶ my %skippkgs = defined($opts{skippkgs}) ? (map {($_=>undef)} @{$opts{skippkgs}}) : qw();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'skippkgs'} ? : 0 0 0
202 0 my $force = $opts{force};
203 0 my @queue = (\$thingy);
204 0 my ($ar);
205 0 ▶ while (defined($ar=shift(@queue))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($ar = shift @queue)) 0 0 0
206 0 ▶ if (exists($skiprefs{$ar}) || exists($skipvals{$$ar}) || (ref($$ar) && exists($skippkgs{ref($$ar)}))) {
Branch: 5 branches
BranchTrueFalseTotal
if (exists $skiprefs{$ar} or exists $skipvals{$$ar} or ref $$ar and exists $skippkgs{ref $$ar}) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'ARRAY')) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'HASH')) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'SCALAR') or UNIVERSAL::isa($$ar, 'REF')) { } 0 0 0
elsif (not ref $$ar) { } 0 0 0
Condition0%0/9
exists $skiprefs{$ar} or exists $skipvals{$$ar} or ref $$ar and exists $skippkgs{ref $$ar}
l!l&&r!l&&!r
000
exists $skiprefs{$ar} or exists $skipvals{$$ar}
l!l&&r!l&&!r
000
ref $$ar and exists $skippkgs{ref $$ar}
!ll&&!rl&&r
000
MC/DC0%0/4
exists $skiprefs{$ar} or exists $skipvals{$$ar} or ref $$ar and exists $skippkgs{ref $$ar}
exists $skiprefs{$ar} exists $skipvals{$$ar} ref $$ar exists $skippkgs{ref $$ar}
Truth table0%0/5
A or B or C and D
A= exists $skiprefs{$ar}B= exists $skipvals{$$ar}C= ref $$arD= exists $skippkgs{ref $$ar}
ABCDresult
1XXX1
01XX1
00111
000X0
00100
207 0 next;
208 } elsif (UNIVERSAL::isa($$ar,'ARRAY')) {
209 0 push(@queue, map { \$_ } @{$$ar});
210 } elsif (UNIVERSAL::isa($$ar,'HASH')) {
211 0 push(@queue, map { \$_ } values %{$$ar});
212 ▶ } elsif (UNIVERSAL::isa($$ar, 'SCALAR') || UNIVERSAL::isa($$ar,'REF')) {
Condition0%0/3
UNIVERSAL::isa($$ar, "SCALAR") or UNIVERSAL::isa($$ar, "REF")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($$ar, "SCALAR") or UNIVERSAL::isa($$ar, "REF")
UNIVERSAL::isa($$ar, "SCALAR") UNIVERSAL::isa($$ar, "REF")
Truth table0%0/3
A or B
A= UNIVERSAL::isa($$ar, "SCALAR")B= UNIVERSAL::isa($$ar, "REF")
ABresult
1X1
011
000
213 0 push(@queue, $$ar);
214 } elsif (!ref($$ar)) {
215 0 ▶ $$ar = decode($enc,$$ar) if (defined($$ar) && ($force || !utf8::is_utf8($$ar)));
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ar and $force || !utf8::is_utf8($$ar) 0 0 0
Condition0%0/6
defined $$ar and $force || !utf8::is_utf8($$ar)
!ll&&!rl&&r
000
$force or not utf8::is_utf8($$ar)
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $$ar and $force || !utf8::is_utf8($$ar)
defined $$ar $force not utf8::is_utf8($$ar)
Truth table0%0/4
A and B or not(C)
A= defined $$arB= $forceC= not utf8::is_utf8($$ar)
ABCresult
0XX0
1000
11X1
1011
216 }
217 }
218 0 return $thingy;
219 }
220  
221 ## $encoded = deep_encode($encoding,$thingy,%opts)
222 ## + %opts:
223 ## force => $bool, ##-- encode even if the utf8 flag is NOT set
224 ## skipvals => \@vals, ##-- don't encode (or recurse into) $val (overrides $force)
225 ## skiprefs => \@refs, ##-- don't encode (or recurse into) $$ref (overrides $force)
226 ## skippkgs => \@pkgs, ##-- don't encode (or recurse into) anything of package $pkg (overrides $force)
227 0 ▶sub deep_encode {
Subroutine deep_encode: not called CC=18 SCAR=58.3
Pod documented
228 0 my ($enc,$thingy,%opts) = @_;
229 0 ▶ my %skipvals = defined($opts{skipvals}) ? (map {($_=>undef)} @{$opts{skipvals}}) : qw();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'skipvals'} ? : 0 0 0
230 0 ▶ my %skiprefs = defined($opts{skiprefs}) ? (map {($_=>undef)} @{$opts{skiprefs}}) : qw();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'skiprefs'} ? : 0 0 0
231 0 ▶ my %skippkgs = defined($opts{skippkgs}) ? (map {($_=>undef)} @{$opts{skippkgs}}) : qw();
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'skippkgs'} ? : 0 0 0
232 0 my $force = $opts{force};
233 0 my @queue = (\$thingy);
234 0 my ($ar);
235 0 ▶ while (defined($ar=shift(@queue))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($ar = shift @queue)) 0 0 0
236 0 ▶ if (exists($skiprefs{$ar}) || !defined($$ar) || exists($skipvals{$$ar}) || (ref($$ar) && exists($skippkgs{ref($$ar)}))) {
Branch: 5 branches
BranchTrueFalseTotal
if (exists $skiprefs{$ar} or not defined $$ar or exists $skipvals{$$ar} or ref $$ar and exists $skippkgs{ref $$ar}) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'ARRAY')) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'HASH')) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'SCALAR') or UNIVERSAL::isa($$ar, 'REF')) { } 0 0 0
elsif (not ref $$ar) { } 0 0 0
Condition0%0/12
exists $skiprefs{$ar} or not defined $$ar or exists $skipvals{$$ar} or ref $$ar and exists $skippkgs{ref $$ar}
l!l&&r!l&&!r
000
exists $skiprefs{$ar} or not defined $$ar or exists $skipvals{$$ar}
l!l&&r!l&&!r
000
exists $skiprefs{$ar} or not defined $$ar
l!l&&r!l&&!r
000
ref $$ar and exists $skippkgs{ref $$ar}
!ll&&!rl&&r
000
MC/DC0%0/5
exists $skiprefs{$ar} or not defined $$ar or exists $skipvals{$$ar} or ref $$ar and exists $skippkgs{ref $$ar}
exists $skiprefs{$ar} not defined $$ar exists $skipvals{$$ar} ref $$ar exists $skippkgs{ref $$ar}
Truth table0%0/6
A or not(B) or C or D and E
A= exists $skiprefs{$ar}B= not defined $$arC= exists $skipvals{$$ar}D= ref $$arE= exists $skippkgs{ref $$ar}
ABCDEresult
1XXXX1
01XXX1
001XX1
000111
0000X0
000100
237 0 next;
238 } elsif (UNIVERSAL::isa($$ar,'ARRAY')) {
239 0 push(@queue, map { \$_ } @{$$ar});
240 } elsif (UNIVERSAL::isa($$ar,'HASH')) {
241 0 push(@queue, map { \$_ } values %{$$ar});
242 ▶ } elsif (UNIVERSAL::isa($$ar, 'SCALAR') || UNIVERSAL::isa($$ar,'REF')) {
Condition0%0/3
UNIVERSAL::isa($$ar, "SCALAR") or UNIVERSAL::isa($$ar, "REF")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($$ar, "SCALAR") or UNIVERSAL::isa($$ar, "REF")
UNIVERSAL::isa($$ar, "SCALAR") UNIVERSAL::isa($$ar, "REF")
Truth table0%0/3
A or B
A= UNIVERSAL::isa($$ar, "SCALAR")B= UNIVERSAL::isa($$ar, "REF")
ABresult
1X1
011
000
243 0 push(@queue, $$ar);
244 } elsif (!ref($$ar)) {
245 0 ▶ $$ar = encode($enc,$$ar) if (defined($$ar) && ($force || utf8::is_utf8($$ar)));
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ar and $force || utf8::is_utf8($$ar) 0 0 0
Condition0%0/6
defined $$ar and $force || utf8::is_utf8($$ar)
!ll&&!rl&&r
000
$force or utf8::is_utf8($$ar)
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $$ar and $force || utf8::is_utf8($$ar)
defined $$ar $force utf8::is_utf8($$ar)
Truth table0%0/4
A and B or C
A= defined $$arB= $forceC= utf8::is_utf8($$ar)
ABCresult
0XX0
1000
11X1
1011
246 }
247 }
248 0 return $thingy;
249 }
250  
251 ## $recoded = deep_recode($from,$to,$thingy, %opts);
252 0 ▶sub deep_recode {
Subroutine deep_recode: not called CC=1 SCAR=6.9
Pod documented
253 0 my ($from,$to,$thingy,%opts) = @_;
254 0 return deep_encode($to,deep_decode($from,$thingy,%opts),%opts);
255 }
256  
257 ## $upgraded = deep_utf8_upgrade($thingy)
258 0 ▶sub deep_utf8_upgrade {
Subroutine deep_utf8_upgrade: not called CC=8 SCAR=42.8
Pod documented
259 0 my ($thingy) = @_;
260 0 my @queue = (\$thingy);
261 0 my ($ar);
262 0 ▶ while (defined($ar=shift(@queue))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($ar = shift @queue)) 0 0 0
263 0 ▶ if (UNIVERSAL::isa($$ar,'ARRAY')) {
Branch: 4 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($$ar, 'ARRAY')) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'HASH')) { } 0 0 0
elsif (UNIVERSAL::isa($$ar, 'SCALAR') or UNIVERSAL::isa($$ar, 'REF')) { } 0 0 0
elsif (not ref $$ar) { } 0 0 0
264 0 push(@queue, map { \$_ } @{$$ar});
265 } elsif (UNIVERSAL::isa($$ar,'HASH')) {
266 0 push(@queue, map { \$_ } values %{$$ar});
267 ▶ } elsif (UNIVERSAL::isa($$ar, 'SCALAR') || UNIVERSAL::isa($$ar,'REF')) {
Condition0%0/3
UNIVERSAL::isa($$ar, "SCALAR") or UNIVERSAL::isa($$ar, "REF")
l!l&&r!l&&!r
000
MC/DC0%0/2
UNIVERSAL::isa($$ar, "SCALAR") or UNIVERSAL::isa($$ar, "REF")
UNIVERSAL::isa($$ar, "SCALAR") UNIVERSAL::isa($$ar, "REF")
Truth table0%0/3
A or B
A= UNIVERSAL::isa($$ar, "SCALAR")B= UNIVERSAL::isa($$ar, "REF")
ABresult
1X1
011
000
268 0 push(@queue, $$ar);
269 } elsif (!ref($$ar)) {
270 0 ▶ utf8::upgrade($$ar) if (defined($$ar));
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ar 0 0 0
271 }
272 }
273 0 return $thingy;
274 }
275  
276  
277 ##==============================================================================
278 ## Functions: abstract data path value
279 ##==============================================================================
280  
281 ## $val_or_undef = path_value($obj, \@path)
282 ## $val_or_undef = path_value($obj, $path_str)
283 0 ▶sub path_value {
Subroutine path_value: not called CC=6 SCAR=37.4
Pod documented
284 0 my $obj = shift;
285 0 foreach (@{path_parse($_[0])}) {
286 0 ▶ return undef if (!ref($obj));
Branch: 1 branch
BranchTrueFalseTotal
unless ref $obj 0 0 0
287 0 ▶ $obj = (UNIVERSAL::isa($obj,'HASH') ? $obj->{$_}
Branch: 3 branches
BranchTrueFalseTotal
UNIVERSAL::isa($obj, 'HASH') ? : 0 0 0
UNIVERSAL::isa($obj, 'ARRAY') ? : 0 0 0
UNIVERSAL::isa($obj, 'CODE') ? : 0 0 0
288 : (UNIVERSAL::isa($obj,'ARRAY') ? $obj->[$_]
289 : (UNIVERSAL::isa($obj,'CODE') ? $obj->($_)
290 : die(__PACKAGE__ . "::path_value(): cannot handle object $obj"))));
291 }
292 0 return $obj;
293 }
294  
295 ## \@path = PACKAGE::path_parse(\@path)
296 ## \@path = PACKAGE::path_parse($path_str)
297 0 ▶sub path_parse {
Subroutine path_parse: not called CC=3 SCAR=16.5
Pod undocumented
298 1 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
299 0 ▶ return UNIVERSAL::isa($_[0],'ARRAY') ? $_[0] : [split(m{/},$_[0]=~m{^/} ? substr($_[0],1) : $_[0])];
Branch: 2 branches
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], 'ARRAY') ? : 0 0 0
$_[0] =~ m[^/] ? : 0 0 0
300 }
301  
302  
303 ##======================================================
304 ## Profiling
305  
306 ## $str = si_str($float)
307 0 ▶sub si_str {
Subroutine si_str: not called CC=18 SCAR=58.3
Pod undocumented
308 0 my $x = shift;
309 0 ▶ return sprintf("%.2fY", $x/10**24) if ($x >= 10**24); ##-- yotta
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e+24" 0 0 0
310 0 ▶ return sprintf("%.2fZ", $x/10**21) if ($x >= 10**21); ##-- zetta
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e+21" 0 0 0
311 0 ▶ return sprintf("%.2fE", $x/10**18) if ($x >= 10**18); ##-- exa
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e+18" 0 0 0
312 0 ▶ return sprintf("%.2fP", $x/10**15) if ($x >= 10**15); ##-- peta
Branch: 1 branch
BranchTrueFalseTotal
if $x >= '1000000000000000' 0 0 0
313 0 ▶ return sprintf("%.2fT", $x/10**12) if ($x >= 10**12); ##-- tera
Branch: 1 branch
BranchTrueFalseTotal
if $x >= '1000000000000' 0 0 0
314 0 ▶ return sprintf("%.2fG", $x/10**9) if ($x >= 10**9); ##-- giga
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1000000000 0 0 0
315 0 ▶ return sprintf("%.2fM", $x/10**6) if ($x >= 10**6); ##-- mega
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1000000 0 0 0
316 0 ▶ return sprintf("%.2fK", $x/10**3) if ($x >= 10**3); ##-- kilo
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 1000 0 0 0
317 0 ▶ return sprintf("%.2f", $x) if ($x >= 0); ##-- (natural units)
Branch: 1 branch
BranchTrueFalseTotal
if $x >= 0 0 0 0
318 0 ▶ return sprintf("%.2fm", $x*10**3) if ($x >= 10**-3); ##-- milli
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "0.001" 0 0 0
319 0 ▶ return sprintf("%.2fu", $x*10**6) if ($x >= 10**-6); ##-- micro
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-06" 0 0 0
320 0 ▶ return sprintf("%.2fn", $x*10**9) if ($x >= 10**-9); ##-- nano
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-09" 0 0 0
321 0 ▶ return sprintf("%.2fp", $x*10**12) if ($x >= 10**-12); ##-- pico
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-12" 0 0 0
322 0 ▶ return sprintf("%.2ff", $x*10**15) if ($x >= 10**-15); ##-- femto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-15" 0 0 0
323 0 ▶ return sprintf("%.2fa", $x*10**18) if ($x >= 10**-18); ##-- atto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-18" 0 0 0
324 0 ▶ return sprintf("%.2fz", $x*10**21) if ($x >= 10**-21); ##-- zepto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-21" 0 0 0
325 0 ▶ return sprintf("%.2fy", $x*10**24) if ($x >= 10**-24); ##-- yocto
Branch: 1 branch
BranchTrueFalseTotal
if $x >= "1e-24" 0 0 0
326 0 return sprintf("%.2g", $x); ##-- default
327 }
328  
329 ## $str = profile_str($elapsed_secs, $ntoks, $nchrs)
330 0 ▶sub profile_str {
Subroutine profile_str: not called CC=5 SCAR=34.0
Pod undocumented
331 0 my ($elapsed,$ntoks,$nchrs) = @_;
332 0 ▶ my $toksPerSec = si_str($ntoks>0 && $elapsed>0 ? ($ntoks/$elapsed) : -0);
Branch: 1 branch
BranchTrueFalseTotal
$ntoks > 0 && $elapsed > 0 ? : 0 0 0
Condition0%0/3
$ntoks > 0 and $elapsed > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$ntoks > 0 and $elapsed > 0
$ntoks > 0 $elapsed > 0
Truth table0%0/3
A and B
A= $ntoks > 0B= $elapsed > 0
ABresult
0X0
100
111
333 0 ▶ my $chrsPerSec = si_str($nchrs>0 && $elapsed>0 ? ($nchrs/$elapsed) : -0);
Branch: 1 branch
BranchTrueFalseTotal
$nchrs > 0 && $elapsed > 0 ? : 0 0 0
Condition0%0/3
$nchrs > 0 and $elapsed > 0
!ll&&!rl&&r
000
MC/DC0%0/2
$nchrs > 0 and $elapsed > 0
$nchrs > 0 $elapsed > 0
Truth table0%0/3
A and B
A= $nchrs > 0B= $elapsed > 0
ABresult
0X0
100
111
334 0 my $d = int($elapsed/(60*60*24));
335 0 my $h = int($elapsed/(60*60)) % 24;
336 0 my $m = int($elapsed/60) % 60;
337 0 my $s = ($elapsed % 60) + ($elapsed-(60*int($elapsed/60))-($elapsed % 60));
338 0 my $timestr = sprintf("%dd %dh %dm %.2fs (%.2fs)", $d,$h,$m,$s,$elapsed);
339 0 $timestr =~ s/^(?:0[dhm]\s*)+//;
340 0 return sprintf("%d tok, %d chr in %s: %s tok/sec ~ %s chr/sec\n",
341 $ntoks,$nchrs, $timestr, $toksPerSec,$chrsPerSec);
342 }
343  
344 ##==============================================================================
345 ## Utils: XML::LibXML
346 ##==============================================================================
347  
348 ## %LIBXML_PARSERS
349 ## + XML::LibXML parsers, keyed by parser attribute strings (see libxml_parser())
350 1 our %LIBXML_PARSERS = qw();
351  
352 ## $parser = libxml_parser(%opts)
353 ## + %opts:
354 ## line_numbers => $bool, ##-- default: 1
355 ## load_ext_dtd => $bool, ##-- default: 0
356 ## validation => $bool, ##-- default: 0
357 ## keep_blanks => $bool, ##-- default: 1
358 ## expand_entities => $bool, ##-- default: 1
359 ## recover => $bool, ##-- default: 1
360 0 ▶sub libxml_parser {
Subroutine libxml_parser: not called CC=9 SCAR=45.0
Pod undocumented
361 0 require XML::LibXML;
362 0 my %opts = @_;
363 0 my %defaults = (
364 line_numbers => 1,
365 load_ext_dtd => 0,
366 validation => 0,
367 keep_blanks => 1,
368 expand_entities => 1,
369 recover => 1,
370 );
371 0 %opts = (%defaults,%opts);
372 0 ▶ my $key = join(', ', map {"$_=>".($opts{$_} ? 1 : 0)} sort(keys(%defaults)));
Branch: 1 branch
BranchTrueFalseTotal
$opts{$_} ? : 0 0 0
373 0 ▶ return $LIBXML_PARSERS{$key} if ($LIBXML_PARSERS{$key});
Branch: 1 branch
BranchTrueFalseTotal
if $DTA::CAB::Utils::LIBXML_PARSERS{$key} 0 0 0
374  
375 0 my $parser = $LIBXML_PARSERS{$key} = XML::LibXML->new();
376 0 ▶ $parser->keep_blanks($opts{keep_blanks}||0); ##-- do we want blanks kept?
Condition0%0/2
$opts{'keep_blanks'} || 0
l!l
00
MC/DC0%0/1
$opts{'keep_blanks'} || 0
$opts{'keep_blanks'}
Truth table0%0/2
A
A= $opts{'keep_blanks'}
Aresult
11
00
377 0 ▶ $parser->expand_entities($opts{expand_ents}||0); ##-- do we want entities expanded?
Condition0%0/2
$opts{'expand_ents'} || 0
l!l
00
MC/DC0%0/1
$opts{'expand_ents'} || 0
$opts{'expand_ents'}
Truth table0%0/2
A
A= $opts{'expand_ents'}
Aresult
11
00
378 0 ▶ $parser->line_numbers($opts{line_numbers}||0);
Condition0%0/2
$opts{'line_numbers'} || 0
l!l
00
MC/DC0%0/1
$opts{'line_numbers'} || 0
$opts{'line_numbers'}
Truth table0%0/2
A
A= $opts{'line_numbers'}
Aresult
11
00
379 0 ▶ $parser->load_ext_dtd($opts{load_ext_dtd}||0);
Condition0%0/2
$opts{'load_ext_dtd'} || 0
l!l
00
MC/DC0%0/1
$opts{'load_ext_dtd'} || 0
$opts{'load_ext_dtd'}
Truth table0%0/2
A
A= $opts{'load_ext_dtd'}
Aresult
11
00
380 0 ▶ $parser->validation($opts{validation}||0);
Condition0%0/2
$opts{'validation'} || 0
l!l
00
MC/DC0%0/1
$opts{'validation'} || 0
$opts{'validation'}
Truth table0%0/2
A
A= $opts{'validation'}
Aresult
11
00
381 0 ▶ $parser->recover($opts{recover}||0);
Condition0%0/2
$opts{'recover'} || 0
l!l
00
MC/DC0%0/1
$opts{'recover'} || 0
$opts{'recover'}
Truth table0%0/2
A
A= $opts{'recover'}
Aresult
11
00
382 0 return $parser;
383 }
384  
385 ## $doc = libxml_doc($which=>$src, %parserOpts)
386 ## + $which is one of 'file', 'string', 'fh', or 'doc'
387 0 ▶sub libxml_doc {
Subroutine libxml_doc: not called CC=5 SCAR=34.0
Pod undocumented
388 0 my ($which,$src,%opts) = @_;
389 0 my $parser = libxml_parser(%opts);
390 0 ▶ if ($which eq 'file') {
Branch: 4 branches
BranchTrueFalseTotal
if ($which eq 'file') { } 0 0 0
elsif ($which eq 'fh') { } 0 0 0
elsif ($which eq 'string') { } 0 0 0
elsif ($which eq 'doc') { } 0 0 0
391 0 return $parser->parse_file($src);
392 } elsif ($which eq 'fh') {
393 0 return $parser->parse_fh($src);
394 } elsif ($which eq 'string') {
395 0 return $parser->parse_string($src);
396 } elsif ($which eq 'doc') {
397 0 return $src;
398 }
399 0 confess(__PACKAGE__, "::libxml_doc() unknown source type '$which'!");
400 0 return undef;
401 }
402  
403 ## \@vals = libxml_xpnodes($nod,$xpath)
404 ## + wrapper for scalar($nod->findnodes($xpath)||[])
405 0 ▶sub libxml_xpnodes {
Subroutine libxml_xpnodes: not called CC=2 SCAR=17.9
Pod undocumented
406 0 ▶ return undef if (!defined($_[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[0] 0 0 0
407 0 return scalar($_[0]->findnodes($_[1]));
408 }
409  
410 ## \@vals = libxml_xpnode($nod,$xpath)
411 ## + wrapper for scalar($nod->findnodes($xpath)||[])->[0]
412 0 ▶sub libxml_xpnode {
Subroutine libxml_xpnode: not called CC=2 SCAR=17.9
Pod undocumented
413 0 ▶ return (libxml_xpnodes(@_)||[])->[0];
Condition0%0/2
libxml_xpnodes(@_) || []
l!l
00
MC/DC0%0/1
libxml_xpnodes(@_) || []
libxml_xpnodes(@_)
Truth table0%0/2
A
A= libxml_xpnodes(@_)
Aresult
11
00
414 }
415  
416 ## $val_or_undef = libxml_xpvalue($nod,$xpath)
417 ## + wrapper for $nod->findnodes($xpath)->[0]->textContent
418 0 ▶sub libxml_xpvalue {
Subroutine libxml_xpvalue: not called CC=2 SCAR=17.9
Pod undocumented
419 0 my $nod = libxml_xpnode(@_);
420 0 ▶ return defined($nod) ? $nod->textContent : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $nod ? : 0 0 0
421 }
422  
423 ## $xpc = libxml_xpcontext($doc_or_nod)
424 ## + returns an XML::LibXML::XPathContext suitable for matching $doc_or_nod
425 ## - if $doc_or_nod is an XML::LibXML::Document, its root node is used as context, otherwise $nod
426 ## + registers all namespaces returned by $doc_or_nod->namespaces()
427 0 ▶sub libxml_xpcontext {
Subroutine libxml_xpcontext: not called CC=5 SCAR=34.0
Pod undocumented
428 0 my $nod = shift;
429 0 ▶ $nod = $nod->documentElement if (UNIVERSAL::isa($nod,'XML::LibXML::Document'));
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($nod, "XML::LibXML::Document") 0 0 0
430 0 my $xc = XML::LibXML::XPathContext->new($nod);
431 0 ▶ $xc->registerNs(($_->declaredPrefix||'DEFAULT'),$_->declaredURI) foreach ($nod ? $nod->namespaces : qw());
Branch: 1 branch
BranchTrueFalseTotal
$nod ? : 0 0 0
Condition0%0/2
$_->declaredPrefix || 'DEFAULT'
l!l
00
MC/DC0%0/1
$_->declaredPrefix || 'DEFAULT'
$_->declaredPrefix
Truth table0%0/2
A
A= $_->declaredPrefix
Aresult
11
00
432 0 return $xc;
433 }
434  
435 ##==============================================================================
436 ## Utils: XML::LibXSLT
437 ##==============================================================================
438  
439 ## $XSLT
440 ## + package-global shared XML::LibXSLT object (or undef)
441 1 our $XSLT = undef;
442  
443 ## $xslt = PACKAGE::xsl_xslt()
444 ## + returns XML::LibXSLT object
445 0 ▶sub xsl_xslt {
Subroutine xsl_xslt: not called CC=2 SCAR=17.9
Pod undocumented
446 0 require XML::LibXML;
447 0 require XML::LibXSLT;
448 0 ▶ $XSLT = XML::LibXSLT->new() if (!$XSLT);
Branch: 1 branch
BranchTrueFalseTotal
unless $DTA::CAB::Utils::XSLT 0 0 0
449 0 return $XSLT;
450 }
451  
452 ## $stylesheet = PACKAGE::xsl_stylesheet(file=>$xsl_file)
453 ## $stylesheet = PACKAGE::xsl_stylesheet(fh=>$xsl_fh)
454 ## $stylesheet = PACKAGE::xsl_stylesheet(doc=>$xsl_doc)
455 ## $stylesheet = PACKAGE::xsl_stylesheet(string=>$xsl_string)
456 0 ▶sub xsl_stylesheet {
Subroutine xsl_stylesheet: not called CC=3 SCAR=24.8
Pod undocumented
457 0 require XML::LibXML;
458 0 require XML::LibXSLT;
459 0 my ($which,$src) = @_;
460 0 my $xsldoc = libxml_doc($which=>$src, line_numbers=>1);
461 0 ▶ confess(__PACKAGE__, "::xsl_stylesheet: could not parse XSL $which source as XML: $!") if (!$xsldoc);
Branch: 1 branch
BranchTrueFalseTotal
unless $xsldoc 0 0 0
462  
463 0 my $xslt = xsl_xslt();
464 0 ▶ my $stylesheet = $xslt->parse_stylesheet($xsldoc)
Branch: 1 branch
BranchTrueFalseTotal
my $stylesheet = $xslt->parse_stylesheet($xsldoc) or confess(__PACKAGE__, "::xsl_stylesheet(): could not parse XSL $which document as stylesheet: $!") 0 0 0
465 or confess(__PACKAGE__, "::xsl_stylesheet(): could not parse XSL $which document as stylesheet: $!");
466  
467 0 return $stylesheet;
468 }
469  
470 ##==============================================================================
471 ## Functions: file stuff
472 ##==============================================================================
473  
474 ## $bits = fhbits(@fhs_or_fds)
475 0 ▶sub fhbits {
Subroutine fhbits: not called CC=3 SCAR=24.8
Pod undocumented
476 0 my $bits='';
477 0 ▶ vec($bits,$_,1)=1 foreach (map {ref($_) ? fileno($_) : $_} @_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
478 0 return $bits;
479 }
480  
481 ##==============================================================================
482 ## Functions: proc filestsystem
483  
484 ## \%mstat_or_undef = mstat()
485 ## \%mstat_or_undef = mstat($pid=$$)
486 ## \%mstat_or_undef = mstat(\%mstat)
487 ## + class or instance method
488 0 ▶sub mstat {
Subroutine mstat: not called CC=5 SCAR=34.0
Pod undocumented
489 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
490 0 ▶ return $_[0] if (UNIVERSAL::isa($_[0],'HASH')); ##-- redundant call mstat(\%mstat)
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($_[0], "HASH") 0 0 0
491 0 ▶ my $pid = shift || $$;
Condition0%0/3
shift() || $$
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $$
shift() $$
Truth table0%0/3
A || B
A= shift()B= $$
ABresult
1X1
011
000
492 0 ▶ open(my $fh, "/proc/$pid/statm") or return {pid=>$pid};
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "/proc/$pid/statm" or return {"pid", $pid} 0 0 0
493 0 local $/ = undef;
494 0 my $buf = <$fh>;
495 0 chomp($buf);
496 0 close($fh);
497 0 my (%mstat);
498 0 @mstat{qw(pid size resident share text lib data dt)} = ($pid, split(' ',$buf));
499 0 $mstat{pagesize} = POSIX::sysconf( POSIX::_SC_PAGESIZE );
500 0 return \%mstat;
501 }
502  
503 ## $memsize_kb_or_undef = memsize()
504 ## $memsize_kb_or_undef = memsize($pid)
505 ## $memsize_kb_or_undef = memsize(\%mstat)
506 ## + virtual memory size (address space)
507 0 ▶sub memsize {
Subroutine memsize: not called CC=4 SCAR=30.0
Pod undocumented
508 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
509 0 my $mstat = $that->mstat(@_);
510 0 ▶ return defined($mstat) ? $mstat->{size}*(($mstat->{pagesize}||4096)/1024) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $mstat ? : 0 0 0
Condition0%0/2
$mstat->{'pagesize'} || 4096
l!l
00
MC/DC0%0/1
$mstat->{'pagesize'} || 4096
$mstat->{'pagesize'}
Truth table0%0/2
A
A= $mstat->{'pagesize'}
Aresult
11
00
511 }
512  
513 ## $resident_kb_or_undef = memrss()
514 ## $resident_kb_or_undef = memrss($pid)
515 ## $resident_kb_or_undef = memrss(\%mstat)
516 ## + resident set size (physical memory used)
517 0 ▶sub memrss {
Subroutine memrss: not called CC=4 SCAR=30.0
Pod undocumented
518 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
519 0 my $mstat = $that->mstat(@_);
520 0 ▶ return defined($mstat) ? $mstat->{resident}*(($mstat->{pagesize}||4096)/1024) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $mstat ? : 0 0 0
Condition0%0/2
$mstat->{'pagesize'} || 4096
l!l
00
MC/DC0%0/1
$mstat->{'pagesize'} || 4096
$mstat->{'pagesize'}
Truth table0%0/2
A
A= $mstat->{'pagesize'}
Aresult
11
00
521 }
522  
523 ## $cmd = pid_cmd($pid)
524 0 ▶sub pid_cmd {
Subroutine pid_cmd: not called CC=7 SCAR=40.3
Pod undocumented
525 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
526 0 my $pid = shift;
527 0 my ($fh,$buf);
528 return
529 ($pid && (readlink("/proc/$pid/exe")
530 0 ▶ || do {
Condition0%0/8
$pid && (readlink "/proc/$pid/exe" || do { open $fh, "/proc/$pid/cmdline" and scalar($buf = readline $fh) and (split(/\0/, $buf, 2))[0] }) || undef
l!l
00
$pid and readlink "/proc/$pid/exe" || do { open $fh, "/proc/$pid/cmdline" and scalar($buf = readline $fh) and (split(/\0/, $buf, 2))[0] }
!ll&&!rl&&r
000
readlink "/proc/$pid/exe" or do { open $fh, "/proc/$pid/cmdline" and scalar($buf = readline $fh) and (split(/\0/, $buf, 2))[0] }
l!l&&r!l&&!r
000
MC/DC0%0/3
$pid && (readlink "/proc/$pid/exe" || do { open $fh, "/proc/$pid/cmdline" and scalar($buf = readline $fh) and (split(/\0/, $buf, 2))[0] }) || undef
$pid readlink "/proc/$pid/exe" do { open $fh, "/proc/$pid/cmdline" and scalar($buf = readline $fh) and (split(/\0/, $buf, 2))[0] }
Truth table0%0/4
A and B or C
A= $pidB= readlink "/proc/$pid/exe"C= do { open $fh, "/proc/$pid/cmdline" and scalar($buf = readline $fh) and (split(/\0/, $buf, 2))[0] }
ABCresult
11X1
1011
0XX0
1000
531 0 ▶ open($fh, "/proc/$pid/cmdline")
Condition0%0/5
open($fh, "/proc/$pid/cmdline") && scalar($buf = readline $fh) && (split(/\0/, $buf, 2))[0]
!ll&&!rl&&r
000
open $fh, "/proc/$pid/cmdline" and scalar($buf = readline $fh)
!ll
00
MC/DC0%0/2
open($fh, "/proc/$pid/cmdline") && scalar($buf = readline $fh) && (split(/\0/, $buf, 2))[0]
open $fh, "/proc/$pid/cmdline" (split(/\0/, $buf, 2))[0]
Truth table0%0/3
A && B
A= open $fh, "/proc/$pid/cmdline"B= (split(/\0/, $buf, 2))[0]
ABresult
0X0
100
111
532 && scalar($buf=<$fh>)
533 && (split(/\0/,$buf,2))[0]
534 })
535 ) || undef;
536 }
537  
538 ##==============================================================================
539 ## Functions: files
540  
541 ## $mtime_in_floating_seconds = file_mtime($filename_or_fh)
542 ## + de-references symlinks
543 0 ▶sub file_mtime {
Subroutine file_mtime: not called CC=3 SCAR=24.8
Pod undocumented
544 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
545 0 my $file = shift;
546 0 ▶ my @stat = (UNIVERSAL::can('Time::HiRes','stat') ? Time::HiRes::stat($file) : stat($file));
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::can('Time::HiRes', 'stat') ? : 0 0 0
547 0 return $stat[9];
548 }
549  
550 ##==============================================================================
551 ## Functions: time
552  
553 ## $timestamp_str = PACAKGE::timestamp_str()
554 ## $timestamp_str = PACAKGE::timestamp_str($time)
555 0 ▶sub timestamp_str {
Subroutine timestamp_str: not called CC=3 SCAR=24.8
Pod undocumented
556 0 ▶ my $that = UNIVERSAL::isa($_[0],__PACKAGE__) ? shift : __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($_[0], __PACKAGE__) ? : 0 0 0
557 0 ▶ my $time = @_ ? shift : time();
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
558 0 return POSIX::strftime("%FT%T%z", localtime($time));
559 }
560  
561 ## DTA::CAB::Utils::EMA : exponential moving average
562 package DTA::CAB::Utils::EMA;
563 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
564  
565 ## $ema = PACKAGE->new(%args)
566 ## + %args, object structure:
567 ## {
568 ## decay => \@seconds, ##-- number(s) of seconds until the contribution of a sample falls below 1/e (default=[60 300 900] ~ 1,5,15 minutes)
569 ## vals => \@vals, ##-- current moving average values (default=0)
570 ## t => $time, ##-- timestamp of the current sample as returned by Time::HiRes::gettimeofday (default=current timestamp)
571 ## }
572 0 ▶sub new {
Subroutine new: not called CC=4 SCAR=30.0
573 0 require Time::HiRes;
574 0 my $that = shift;
575 0 my $ema = {
576 decay =>[60, (5*60), (15*60)],
577 vals =>[],
578 t =>[Time::HiRes::gettimeofday()],
579 @_,
580 };
581 0 ▶ $ema->{vals}[$_] //= 0 foreach (0..$#{$ema->{decay}});
Condition0%0/2
$ema->{"vals"}[$_] //= 0
l!l
00
MC/DC0%0/1
$ema->{"vals"}[$_] //= 0
$ema->{"vals"}[$_]
Truth table0%0/2
A
A= $ema->{"vals"}[$_]
Aresult
11
00
582 0 ▶ return bless($ema,ref($that)||$that);
Condition0%0/3
ref $that || $that
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $that || $that
ref $that $that
Truth table0%0/3
A || B
A= ref $thatB= $that
ABresult
1X1
011
000
583 }
584  
585 ## $ema = $ema->append($newValue)
586 ## $ema = $ema->append($newValue,$newTime)
587 ## + append a new sample value with timestamp $newTime
588 0 ▶sub append {
Subroutine append: not called CC=4 SCAR=30.0
589 0 require Time::HiRes;
590 0 my ($ema,$newVal,$newTime) = @_;
591 0 ▶ $newVal //= 0;
Condition0%0/2
$newVal //= 0
l!l
00
MC/DC0%0/1
$newVal //= 0
$newVal
Truth table0%0/2
A
A= $newVal
Aresult
11
00
592 0 ▶ $newTime = [Time::HiRes::gettimeofday()] if (!$newTime);
Branch: 1 branch
BranchTrueFalseTotal
unless $newTime 0 0 0
593 0 my $tdiff = Time::HiRes::tv_interval($ema->{t},$newTime);
594 0 my ($alpha);
595 0 foreach (0..$#{$ema->{decay}}) {
596 0 $alpha = exp(-$tdiff/$ema->{decay}[$_]);
597 0 $ema->{vals}[$_] = (1-$alpha)*$newVal + $alpha*$ema->{vals}[$_];
598 }
599 0 $ema->{t} = $newTime;
600 0 return $ema;
601 }
602  
603 ## @vals = $ema->vals($newVal,$newTime)
604 ## \@vals = $ema->vals($newVal,$newTime)
605 ## + wrapper for $ema->append($newVal,$newTime)->{vals}
606 ## + optionally append a sample and return current (decayed) value(s)
607 ## + default $newVal=0, default $newTime=[Time::HiRes::gettimeofday] --> current decayed sample values
608 0 ▶sub vals {
Subroutine vals: not called CC=2 SCAR=17.9
609 0 $_[0]->append(@_[1..$#_]);
610 0 ▶ return wantarray ? @{$_[0]{vals}} : $_[0]{vals};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
611 }
612  
613  
614 1 1; ##-- be happy
615  
616 __END__
« CAB/Format/XmlLing.pm ↑ Summary CAB/Analyzer/TokPP/Perl.pm »