lib/Perl7/Handy.pm

filter: Statementstmt 40.3% 98 / 243 Branchbran 12.5% 9 / 72 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 73.8% 31 / 42 Podpod 85.7% 6 / 7 totaltotal 34.7% 144 / 414 CC 47 SCAR 36.7
CC
47
Cov
31%
open
155.6
sysopen
52.4
opendir
35.8
CRAP 39.2
↑ Summary
1 package Perl7::Handy;
2 ######################################################################
3 #
4 # Perl7::Handy - Handy Perl7 scripting environment on Perl5
5 #
6 # https://metacpan.org/dist/Perl7-Handy
7 #
8 # Copyright (c) 2020, 2021, 2026 INABA Hitoshi <ina@cpan.org>
9 ######################################################################
10  
11 13 ▶use 5.00503; # Universal Consensus 1998 for primetools
Subroutine BEGIN: called CC=1 SCAR=0.0
12 # use 5.008001; # Lancaster Consensus 2013 for toolchains
13  
14 13 ▶use vars qw($VERSION $_fh_seq);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 13 $VERSION = '0.14';
16 13 $VERSION = $VERSION;
17 13 $_fh_seq = 0;
18  
19 13 ▶BEGIN { pop @INC if $INC[-1] eq '.' } # CVE-2016-1238: Important unsafe module load path flaw
Subroutine BEGIN: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $INC[-1] eq "." 12 1 13
20 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 ▶BEGIN { $INC{'warnings.pm'} = '' if $] < 5.006 } use warnings; local $^W=1;
Subroutine BEGIN: called CC=2 SCAR=7.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.006" 0 13 13
22 13 ▶BEGIN { $INC{'feature.pm'} = '' if $] < 5.010 } use feature ();
Subroutine BEGIN: called CC=2 SCAR=7.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $] < "5.01" 0 13 13
23 13 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
24 13 ▶ if ($] >= 5.010001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.010001) { } 13 0 13
25 13 ▶ eval q{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
26 use bareword::filehandles; # pmake.bat catches /^use .../
27 use multidimensional; # pmake.bat catches /^use .../
28 };
29 }
30 else {
31 0 $INC{'bareword/filehandles.pm'} = '';
32 0 $INC{'multidimensional.pm'} = '';
33 }
34 }
35 13 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 #---------------------------------------------------------------------
38 # confess() for this module
39 0 ▶sub Perl7::Handy::confess (@) {
Subroutine confess: not called CC=2 SCAR=17.9
Pod undocumented
40 0 my $i = 0;
41 0 my @confess = ();
42 0 ▶ while (my($package, $filename, $line, $subroutine) = caller($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i) 0 0 0
43 0 push @confess, "[$i] $filename($line) $subroutine\n";
44 0 $i++;
45 }
46 0 print STDERR "\n", @_, "\n";
47 0 print STDERR CORE::reverse @confess;
48 0 die;
49 }
50  
51 #---------------------------------------------------------------------
52 # open() that can't use bareword
53 0 ▶sub Perl7::Handy::open (*$;$) {
Subroutine open: not called CC=16 SCAR=50.5
Pod documented
54 0 my $handle;
55  
56 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
57 0 Perl7::Handy::confess "Use of bareword handle in open";
58 }
59 else {
60 0 $_fh_seq++;
61 0 my $fhn = "Perl7::Handy::FH::H${_fh_seq}";
62 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
63 0 $handle = $fhn;
64 0 $_[0] = \*{$fhn};
65 }
66  
67 0 ▶ if (@_ >= 4) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ >= 4) { } 0 0 0
elsif (@_ == 3) { } 0 0 0
elsif (@_ == 2) { } 0 0 0
68 0 Perl7::Handy::confess "Too many arguments for open";
69 }
70 elsif (@_ == 3) {
71 0 my($mode, $filename) = @_[1, 2];
72  
73 0 ▶ if ($mode eq '-|') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '-|') { } 0 0 0
elsif ($mode eq '|-') { } 0 0 0
74 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
75 0 my $return = CORE::open($handle, qq{$filename |});
76 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
77 0 return $return;
78 }
79 else {
80 0 Perl7::Handy::confess "Can't open($_[0],$_[1],$_[2]): $!";
81 }
82 }
83 elsif ($mode eq '|-') {
84 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
85 0 my $return = CORE::open($handle, qq{| $filename});
86 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
87 0 return $return;
88 }
89 else {
90 0 Perl7::Handy::confess "Can't open($_[0],$_[1],$_[2]): $!";
91 }
92 }
93 else {
94 0 my %flags = (
95 '<' => O_RDONLY,
96 '>' => O_WRONLY | O_TRUNC | O_CREAT,
97 '>>' => O_WRONLY | O_APPEND | O_CREAT,
98 '+<' => O_RDWR,
99 '+>' => O_RDWR | O_TRUNC | O_CREAT,
100 '+>>' => O_RDWR | O_APPEND | O_CREAT,
101 );
102 0 ▶ if (not exists $flags{$mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $flags{$mode}) 0 0 0
103 0 Perl7::Handy::confess "Unknown open() mode '$mode'";
104 }
105 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
106 0 my $return = CORE::sysopen(*{$handle}, $filename, $flags{$mode});
107 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
108 0 return $return;
109 }
110 else {
111 0 Perl7::Handy::confess "Can't open($_[0],$_[1],$_[2]): $!";
112 }
113 }
114 }
115 elsif (@_ == 2) {
116 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
117 0 my $return = CORE::open($handle, $_[1]);
118 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
119 0 return $return;
120 }
121 else {
122 0 Perl7::Handy::confess "Can't open($_[0],$_[1]): $!";
123 }
124 }
125 else {
126 0 Perl7::Handy::confess "Not enough arguments for open";
127 }
128 }
129  
130 #---------------------------------------------------------------------
131 # opendir() that can't use bareword
132 0 ▶sub Perl7::Handy::opendir (*$) {
Subroutine opendir: not called CC=7 SCAR=35.8
Pod documented
133 0 my $handle;
134  
135 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
136 0 Perl7::Handy::confess "Use of bareword handle in opendir";
137 }
138 else {
139 0 $_fh_seq++;
140 0 my $fhn = "Perl7::Handy::FH::H${_fh_seq}";
141 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
142 0 $handle = $fhn;
143 0 $_[0] = \*{$fhn};
144 }
145  
146 0 my $return;
147 0 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
148 0 ▶ if ($return = CORE::opendir(*{$handle}, $_[1])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($return = CORE::opendir(do { $handle }, $_[1])) { } 0 0 0
elsif ($^O =~ /MSWin32/ and -d "$_[1].") { } 0 0 0
149 }
150 ▶ elsif (($^O =~ /MSWin32/) and (-d qq{$_[1].})) {
Condition0%0/3
$^O =~ /MSWin32/ and -d "$_[1]."
!ll&&!rl&&r
000
MC/DC0%0/2
$^O =~ /MSWin32/ and -d "$_[1]."
$^O =~ /MSWin32/ -d "$_[1]."
Truth table0%0/3
A and B
A= $^O =~ /MSWin32/B= -d "$_[1]."
ABresult
0X0
100
111
151 0 $return = CORE::opendir(*{$handle}, qq{$_[1].});
152 }
153 }
154  
155 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
156 0 return $return;
157 }
158 else {
159 0 Perl7::Handy::confess "Can't opendir($_[0],$_[1]): $!";
160 }
161 }
162  
163 #---------------------------------------------------------------------
164 # sysopen() that can't use bareword
165 0 ▶sub Perl7::Handy::sysopen (*$$;$) {
Subroutine sysopen: not called CC=9 SCAR=39.6
Pod documented
166 0 my $handle;
167  
168 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
169 0 Perl7::Handy::confess "Use of bareword handle in sysopen";
170 }
171 else {
172 0 $_fh_seq++;
173 0 my $fhn = "Perl7::Handy::FH::H${_fh_seq}";
174 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
175 0 $handle = $fhn;
176 0 $_[0] = \*{$fhn};
177 }
178  
179 0 ▶ if (@_ >= 5) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ >= 5) { } 0 0 0
elsif (@_ == 4) { } 0 0 0
elsif (@_ == 3) { } 0 0 0
180 0 Perl7::Handy::confess "Too many arguments for sysopen";
181 }
182 elsif (@_ == 4) {
183 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
184 0 my $return = CORE::sysopen(*{$handle}, $_[1], $_[2], $_[3]);
185 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
186 0 return $return;
187 }
188 else {
189 0 Perl7::Handy::confess "Can't sysopen($_[0],$_[1],$_[2],$_[3]): $!";
190 }
191 }
192 elsif (@_ == 3) {
193 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
194 0 my $return = CORE::sysopen(*{$handle}, $_[1], $_[2]);
195 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
196 0 return $return;
197 }
198 else {
199 0 Perl7::Handy::confess "Can't sysopen($_[0],$_[1],$_[2]): $!";
200 }
201 }
202 else {
203 0 Perl7::Handy::confess "Not enough arguments for sysopen";
204 }
205 }
206  
207 #---------------------------------------------------------------------
208 # pipe() that can't use bareword
209 0 ▶sub Perl7::Handy::pipe (**) {
Subroutine pipe: not called CC=5 SCAR=27.8
Pod documented
210 0 my($handle0, $handle1);
211  
212 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
213 0 Perl7::Handy::confess "Use of bareword handle (\$_[0]) in pipe";
214 }
215 else {
216 0 $_fh_seq++;
217 0 my $fhn0 = "Perl7::Handy::FH::P${_fh_seq}r";
218 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
219 0 $handle0 = $fhn0;
220 0 $_[0] = \*{$fhn0};
221 }
222  
223 0 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 0 0
224 0 Perl7::Handy::confess "Use of bareword handle (\$_[1]) in pipe";
225 }
226 else {
227 0 my $fhn1 = "Perl7::Handy::FH::P${_fh_seq}w";
228 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
229 0 $handle1 = $fhn1;
230 0 $_[1] = \*{$fhn1};
231 }
232  
233 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
234 0 my $return = CORE::pipe(*{$handle0}, *{$handle1});
235 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
236 0 return $return;
237 }
238 else {
239 0 Perl7::Handy::confess "Can't pipe($_[0],$_[1]): $!";
240 }
241 }
242  
243 #---------------------------------------------------------------------
244 # socket() that can't use bareword
245 0 ▶sub Perl7::Handy::socket (*$$$) {
Subroutine socket: not called CC=2 SCAR=11.6
Pod documented
246 0 my $handle;
247  
248 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
249 0 Perl7::Handy::confess "Use of bareword handle in socket";
250 }
251 else {
252 0 $_fh_seq++;
253 0 my $fhn = "Perl7::Handy::FH::H${_fh_seq}";
254 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
255 0 $handle = $fhn;
256 0 $_[0] = \*{$fhn};
257 }
258  
259 # socket doesn't autodie
260 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
261 0 return CORE::socket(*{$handle}, $_[1], $_[2], $_[3]);
262 }
263  
264 #---------------------------------------------------------------------
265 # accept() that can't use bareword
266 0 ▶sub Perl7::Handy::accept (**) {
Subroutine accept: not called CC=4 SCAR=24.4
Pod documented
267 0 my($handle0, $handle1);
268  
269 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
270 0 Perl7::Handy::confess "Use of bareword handle (\$_[0]) in accept";
271 }
272 else {
273 0 $_fh_seq++;
274 0 my $fhn = "Perl7::Handy::FH::H${_fh_seq}";
275 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
276 0 $handle0 = $fhn;
277 0 $_[0] = \*{$fhn};
278 }
279  
280 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
281 0 my $return = CORE::accept(*{$handle0}, *{$_[1]});
282 0 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 0 0 0
Condition0%0/3
$return or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table0%0/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
283 0 return $return;
284 }
285 else {
286 0 Perl7::Handy::confess "Can't accept($_[0],$_[1]): $!";
287 }
288 }
289  
290 #---------------------------------------------------------------------
291 # TIESCALAR to disable $; (internal use to "no multidimensional")
292 0 ▶sub TIESCALAR {
Subroutine TIESCALAR: not called CC=1 SCAR=6.9
293 0 my $class = shift;
294 0 my $dummy;
295 0 return bless \$dummy => $class;
296 }
297  
298 #---------------------------------------------------------------------
299 # FETCH to disable $; (internal use to "no multidimensional")
300 0 ▶sub FETCH {
Subroutine FETCH: not called CC=1 SCAR=6.9
301 0 Perl7::Handy::confess "Use of multidimensional array emulation";
302 }
303  
304 #---------------------------------------------------------------------
305 # STORE to disable $; (internal use to "no multidimensional")
306 0 ▶sub STORE {
Subroutine STORE: not called CC=1 SCAR=6.9
307 0 Perl7::Handy::confess "Use of multidimensional array emulation"
308 }
309  
310 #---------------------------------------------------------------------
311 # gives:
312 # use strict;
313 # use warnings;
314 # no bareword::filehandles;
315 # no multidimensional;
316 # use feature qw(signatures); no warnings qw(experimental::signatures);
317 # no feature qw(indirect);
318 13 ▶sub import {
Subroutine import: called CC=5 SCAR=21.3
319  
320 # gives caller package "use strict;"
321 13 strict->import;
322  
323 # gives caller package "use warnings;" (only perl 5.006 or later)
324 13 ▶ if ($] >= 5.006) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.006") 13 0 13
325 13 warnings->import;
326  
327 # gives caller package "use feature qw(signatures); no warnings qw(experimental::signatures);" (only perl 5.020 or later)
328 13 ▶ if ($] >= 5.020) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.02") 13 0 13
329 13 feature->import('signatures');
330 13 warnings->unimport('experimental::signatures');
331  
332 # disables indirect object syntax in caller package
333 # Bug #138701 for Perl7-Handy: Consider disabling indirect object notation [rt.cpan.org #138701]
334 13 ▶ if ($] >= 5.031009) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= "5.031009") 13 0 13
335 13 feature->unimport('indirect');
336 }
337 }
338 }
339  
340 # new Perl called "Modern Perl"
341 13 ▶ if ($] >= 5.010001) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] >= 5.010001) { } 13 0 13
342  
343 # gives caller package "no bareword::filehandles;"
344 13 bareword::filehandles->unimport;
345  
346 # gives caller package "no multidimensional;"
347 13 multidimensional->unimport;
348 }
349  
350 # support older Perl that we love :)
351 else {
352  
353 # gives caller package "no bareword::filehandles;"
354 # avoid: Can't use string ("main::open") as a symbol ref while "strict refs" in use
355 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
356 {
357 # avoid: Prototype mismatch: sub main::open (*;$) vs (*$;$)
358 0 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
359 0 *{caller() . '::open'} = \&Perl7::Handy::open;
360 }
361 0 *{caller() . '::opendir'} = \&Perl7::Handy::opendir;
362 0 *{caller() . '::sysopen'} = \&Perl7::Handy::sysopen;
363 0 *{caller() . '::pipe'} = \&Perl7::Handy::pipe;
364 0 *{caller() . '::socket'} = \&Perl7::Handy::socket;
365 0 *{caller() . '::accept'} = \&Perl7::Handy::accept;
366  
367 # gives caller package "no multidimensional;"
368 0 tie $;, __PACKAGE__;
369 }
370 }
371  
372 13 1;
373  
374 __END__
↑ Summary