lib/Modern/Open.pm

filter: Statementstmt 83.1% 178 / 214 Branchbran 60.0% 36 / 60 Conditioncond 24.2% 8 / 33 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 94.1% 32 / 34 Podpod 71.4% 5 / 7 totaltotal 70.0% 259 / 370 CC 42 SCAR 21.5
CC
42
Cov
72%
open
20.9
sysopen
14.0
accept
11.5
CRAP 8.6
↑ Summary
1 package Modern::Open;
2 ######################################################################
3 #
4 # Modern::Open - Autovivification, Autodie, and 3-args open support
5 #
6 # https://metacpan.org/dist/Modern-Open
7 #
8 # Copyright (c) 2014, 2015, 2018, 2019, 2020, 2021, 2023, 2026 INABA Hitoshi <ina@cpan.org>
9 ######################################################################
10  
11 10 ▶use 5.00503;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use vars qw($VERSION $_fh_seq);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 0 ▶BEGIN { if ($] < 5.006 && !defined(&warnings::import)) { $INC{'warnings.pm'} = 'stub';
Subroutine BEGIN: called CC=3 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.006" and not defined &warnings::import) 0 10 10
Condition33%1/3
$] < "5.006" and not defined &warnings::import
!ll&&!rl&&r
1000
MC/DC0%0/2
$] < "5.006" and not defined &warnings::import
$] < "5.006" not defined &warnings::import
Truth table33%1/3
A and not(B)
A= $] < "5.006"B= not defined &warnings::import
ABresult
0X0
100
111
15 0 ▶ eval 'package warnings; sub import {}' } } use warnings; local $^W=1;
Subroutine BEGIN: called CC=1 SCAR=0.1
16  
17 10 $VERSION = '0.16';
18 10 $VERSION = $VERSION;
19 10 $_fh_seq = 0;
20  
21 10 ▶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 "." 9 1 10
22 10 ▶use Fcntl;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 #---------------------------------------------------------------------
25 22 ▶sub Modern::Open::confess (@) {
Subroutine confess: called CC=2 SCAR=6.9
Pod undocumented
26 22 my $i = 0;
27 22 my @confess = ();
28 22 ▶ while (my($package, $filename, $line, $subroutine) = caller($i)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = caller $i) 66 22 88
29 66 push @confess, "[$i] $filename($line) $subroutine\n";
30 66 $i++;
31 }
32 22 print STDERR "\n", @_, "\n";
33 22 print STDERR CORE::reverse @confess;
34 22 die;
35 }
36  
37 #---------------------------------------------------------------------
38 34 ▶sub Modern::Open::open (*$;$) {
Subroutine open: called CC=16 SCAR=30.4
Pod documented
39 34 my $handle;
40  
41 34 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 17 17 34
42 17 Modern::Open::confess "Bare handle no longer supported";
43 }
44 else {
45 17 $_fh_seq++;
46 17 my $fhn = "Modern::Open::FH::H${_fh_seq}";
47 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
48 17 $handle = $fhn;
49 17 $_[0] = \*{$fhn};
50 }
51  
52 17 ▶ if (@_ >= 4) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ >= 4) { } 0 17 17
elsif (@_ == 3) { } 8 9 17
elsif (@_ == 2) { } 9 0 9
53 0 Modern::Open::confess "Too many arguments for open";
54 }
55 elsif (@_ == 3) {
56 8 my($mode, $filename) = @_[1, 2];
57  
58 8 ▶ if ($mode eq '-|') {
Branch: 2 branches
BranchTrueFalseTotal
if ($mode eq '-|') { } 1 7 8
elsif ($mode eq '|-') { } 1 6 7
59 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
60 1 my $return = CORE::open($handle, qq{$filename |});
61 1 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 1 0 1
Condition33%1/3
$return or defined wantarray
l!l&&r!l&&!r
100
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table33%1/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
62 1 return $return;
63 }
64 else {
65 0 Modern::Open::confess "Can't open($_[0],$_[1],$_[2]): $!";
66 }
67 }
68 elsif ($mode eq '|-') {
69 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
70 1 my $return = CORE::open($handle, qq{| $filename});
71 1 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 1 0 1
Condition33%1/3
$return or defined wantarray
l!l&&r!l&&!r
100
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table33%1/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
72 1 return $return;
73 }
74 else {
75 0 Modern::Open::confess "Can't open($_[0],$_[1],$_[2]): $!";
76 }
77 }
78 else {
79 6 my %flags = (
80 '<' => O_RDONLY,
81 '>' => O_WRONLY | O_TRUNC | O_CREAT,
82 '>>' => O_WRONLY |O_APPEND | O_CREAT,
83 '+<' => O_RDWR,
84 '+>' => O_RDWR | O_TRUNC | O_CREAT,
85 '+>>' => O_RDWR | O_APPEND | O_CREAT,
86 );
87 6 ▶ if (not exists $flags{$mode}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $flags{$mode}) 0 6 6
88 0 Modern::Open::confess "Unknown open() mode '$mode'";
89 }
90 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
91 6 my $return = CORE::sysopen(*{$handle}, $filename, $flags{$mode});
92 6 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 6 0 6
Condition33%1/3
$return or defined wantarray
l!l&&r!l&&!r
600
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table33%1/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
93 6 return $return;
94 }
95 else {
96 0 Modern::Open::confess "Can't open($_[0],$_[1],$_[2]): $!";
97 }
98 }
99 }
100 elsif (@_ == 2) {
101 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
102 9 my $return = CORE::open($handle, $_[1]);
103 9 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 9 0 9
Condition33%1/3
$return or defined wantarray
l!l&&r!l&&!r
900
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table33%1/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
104 9 return $return;
105 }
106 else {
107 0 Modern::Open::confess "Can't open($_[0],$_[1]): $!";
108 }
109 }
110 else {
111 0 Modern::Open::confess "Not enough arguments for open";
112 }
113 }
114  
115 #---------------------------------------------------------------------
116 2 ▶sub Modern::Open::opendir (*$) {
Subroutine opendir: called CC=7 SCAR=21.6
Pod documented
117 2 my $handle;
118  
119 2 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 1 1 2
120 1 Modern::Open::confess "Bare handle no longer supported";
121 }
122 else {
123 1 $_fh_seq++;
124 1 my $fhn = "Modern::Open::FH::H${_fh_seq}";
125 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
126 1 $handle = $fhn;
127 1 $_[0] = \*{$fhn};
128 }
129  
130 1 my $return;
131 10 ▶ { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
132 1 ▶ if ($return = CORE::opendir(*{$handle}, $_[1])) {
Branch: 2 branches
BranchTrueFalseTotal
if ($return = CORE::opendir(do { $handle }, $_[1])) { } 1 0 1
elsif ($^O =~ /MSWin32/ and -d "$_[1].") { } 0 0 0
133 }
134 ▶ 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
135 0 $return = CORE::opendir(*{$handle}, qq{$_[1].});
136 }
137 }
138  
139 1 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 1 0 1
Condition33%1/3
$return or defined wantarray
l!l&&r!l&&!r
100
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table33%1/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
140 1 return $return;
141 }
142 else {
143 0 Modern::Open::confess "Can't opendir($_[0],$_[1]): $!";
144 }
145 }
146  
147 #---------------------------------------------------------------------
148 2 ▶sub Modern::Open::sysopen (*$$;$) {
Subroutine sysopen: called CC=9 SCAR=26.4
Pod documented
149 2 my $handle;
150  
151 2 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 1 1 2
152 1 Modern::Open::confess "Bare handle no longer supported";
153 }
154 else {
155 1 $_fh_seq++;
156 1 my $fhn = "Modern::Open::FH::H${_fh_seq}";
157 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
158 1 $handle = $fhn;
159 1 $_[0] = \*{$fhn};
160 }
161  
162 1 ▶ if (@_ >= 5) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ >= 5) { } 0 1 1
elsif (@_ == 4) { } 0 1 1
elsif (@_ == 3) { } 1 0 1
163 0 Modern::Open::confess "Too many arguments for sysopen";
164 }
165 elsif (@_ == 4) {
166 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
167 0 my $return = CORE::sysopen(*{$handle}, $_[1], $_[2], $_[3]);
168 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
169 0 return $return;
170 }
171 else {
172 0 Modern::Open::confess "Can't sysopen($_[0],$_[1],$_[2],$_[3]): $!";
173 }
174 }
175 elsif (@_ == 3) {
176 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
177 1 my $return = CORE::sysopen(*{$handle}, $_[1], $_[2]);
178 1 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 1 0 1
Condition33%1/3
$return or defined wantarray
l!l&&r!l&&!r
100
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table33%1/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
179 1 return $return;
180 }
181 else {
182 0 Modern::Open::confess "Can't sysopen($_[0],$_[1],$_[2]): $!";
183 }
184 }
185 else {
186 0 Modern::Open::confess "Not enough arguments for sysopen";
187 }
188 }
189  
190 #---------------------------------------------------------------------
191 2 ▶sub Modern::Open::pipe (**) {
Subroutine pipe: called CC=5 SCAR=16.3
Pod documented
192 2 my($handle0, $handle1);
193  
194 2 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 1 1 2
195 1 Modern::Open::confess "Bare handle no longer supported";
196 }
197 else {
198 1 $_fh_seq++;
199 1 my $fhn0 = "Modern::Open::FH::P${_fh_seq}r";
200 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
201 1 $handle0 = $fhn0;
202 1 $_[0] = \*{$fhn0};
203 }
204  
205 1 ▶ if (defined $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[1]) { } 0 1 1
206 0 Modern::Open::confess "Bare handle no longer supported";
207 }
208 else {
209 1 my $fhn1 = "Modern::Open::FH::P${_fh_seq}w";
210 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
211 1 $handle1 = $fhn1;
212 1 $_[1] = \*{$fhn1};
213 }
214  
215 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
216 1 my $return = CORE::pipe(*{$handle0}, *{$handle1});
217 1 ▶ if ($return or defined wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if ($return or defined wantarray) { } 1 0 1
Condition33%1/3
$return or defined wantarray
l!l&&r!l&&!r
100
MC/DC0%0/2
$return or defined wantarray
$return defined wantarray
Truth table33%1/3
A or B
A= $returnB= defined wantarray
ABresult
1X1
011
000
218 1 return $return;
219 }
220 else {
221 0 Modern::Open::confess "Can't pipe($_[0],$_[1]): $!";
222 }
223 }
224  
225 #---------------------------------------------------------------------
226 3 ▶sub Modern::Open::socket (*$$$) {
Subroutine socket: called CC=2 SCAR=6.9
Pod undocumented
227 3 my $handle;
228  
229 3 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 2 1 3
230 2 Modern::Open::confess "Bare handle no longer supported";
231 }
232 else {
233 1 $_fh_seq++;
234 1 my $fhn = "Modern::Open::FH::H${_fh_seq}";
235 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
236 1 $handle = $fhn;
237 1 $_[0] = \*{$fhn};
238 }
239  
240 # socket doesn't autodie
241 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
242 1 return CORE::socket(*{$handle}, $_[1], $_[2], $_[3]);
243 }
244  
245 #---------------------------------------------------------------------
246 0 ▶sub Modern::Open::accept (**) {
Subroutine accept: not called CC=4 SCAR=24.4
Pod documented
247 0 my($handle0, $handle1);
248  
249 0 ▶ if (defined $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) { } 0 0 0
250 0 Modern::Open::confess "Bare handle no longer supported";
251 }
252 else {
253 0 $_fh_seq++;
254 0 my $fhn = "Modern::Open::FH::H${_fh_seq}";
255 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
256 0 $handle0 = $fhn;
257 0 $_[0] = \*{$fhn};
258 }
259  
260 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
261 0 my $return = CORE::accept(*{$handle0}, *{$_[1]});
262 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
263 0 return $return;
264 }
265 else {
266 0 Modern::Open::confess "Can't accept($_[0],$_[1]): $!";
267 }
268 }
269  
270 #---------------------------------------------------------------------
271 10 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
272  
273 # avoid: Can't use string ("main::open") as a symbol ref while "strict refs" in use
274 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
275 {
276 # avoid: Prototype mismatch: sub main::open (*;$) vs (*$;$)
277 10 ▶ local $SIG{__WARN__} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
278 10 *{caller() . '::open'} = \&Modern::Open::open;
279 }
280 10 *{caller() . '::opendir'} = \&Modern::Open::opendir;
281 10 *{caller() . '::sysopen'} = \&Modern::Open::sysopen;
282 10 *{caller() . '::pipe'} = \&Modern::Open::pipe;
283 10 *{caller() . '::socket'} = \&Modern::Open::socket;
284 10 *{caller() . '::accept'} = \&Modern::Open::accept;
285 }
286  
287 10 1;
288  
289 __END__
↑ Summary