lib/Chj/xopen.pm

filter: Statementstmt 47.9% 57 / 119 Branchbran 15.1% 10 / 66 Conditioncond 9.5% 4 / 42 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 50.0% 14 / 28 Podpod 42.1% 8 / 19 totaltotal 30.7% 93 / 302 CC 48 SCAR 27.8
CC
48
Cov
31%
xopen_update
56.0
xopen_append
42.0
fd_to_fh
20.0
CRAP 16.1
« lib/FP/Repl/Stack.pm ↑ Summary lib/FP/Repl/WithRepl.pm »
1 #
2 # Copyright (c) 2003-2020 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 Chj::xopen
13  
14 =head1 SYNOPSIS
15  
16 use Chj::xopen;
17 {
18 my $in = xopen_read "foo.txt";
19 my $out = glob_to_fh(*STDOUT,"utf-8");
20 local $_;
21 while (<$in>) { # default operation. (overload not possible :/)
22 $out->xprint($_); # print, throwing an exception on error
23 }
24 $out->xclose; # close explicitely, throwing an exception on error
25 }
26 # $in and $out are closed automatically in any case
27 # (issuing a warning on error)
28  
29 =head1 DESCRIPTION
30  
31 Constructors around Chj::IO::File.
32  
33 =head1 FUNCTIONS
34  
35 =over 4
36  
37 =item xopen ( EXPR | MODE,LIST )
38  
39 Open the given file like the perl builtin "open" or croak on errors.
40 Returns an anonymous symbol
41 blessed into Chj::xopen::file, which can be used both as object
42 or filehandle (more correctly: anonymous glob) (? always? Perl is a
43 bit complicated when handling filehandles in indirect object notation).
44  
45 (BTW, note that perl won't give an error if you open a directory instead
46 of a file for reading. The returned filehandle will give empty results if either
47 used with read or readdir. That's true for perl 5.005x - 5.6.1 on linux.)
48  
49 =item xopen_read EXPR
50  
51 =item xopen_write EXPR
52  
53 =item xopen_append EXPR
54  
55 =item xopen_update EXPR
56  
57 Those *optionally exported* functions check the one given input
58 parameter for <>+ chars at the beginning, and either croak if they
59 don't match the purpose of the function, or prepend the right chars if
60 missing.
61  
62 =item stdin, stdout, stderr
63  
64 The functions with these names call C<glob_to_fh(*STD..., "utf-8")> by
65 default. If given an argument, it is used as the encoding instead of
66 "utf-8".
67  
68 =back
69  
70 =head1 BUGS
71  
72 Stuff like >&1 not yet really supported by the above xopen_* functions.
73  
74 =head1 SEE ALSO
75  
76 L<Chj::IO::File>, L<Chj::xsysopen>, L<Chj::xopendir>
77  
78 =head1 NOTE
79  
80 This is alpha software! Read the status section in the package README
81 or on the L<website|http://functional-perl.org/>.
82  
83 =cut
84  
85 package Chj::xopen;
86 51 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 51 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 51 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
89 51 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 51 our @EXPORT = qw(xopen xopen_read);
92 51 our @EXPORT_OK = qw(xopen_write xopen_append xopen_update
93 perhaps_open_read perhaps_xopen_read
94 devnull devzero
95 glob_to_fh
96 fd_to_fh
97 stdin
98 stdout
99 stderr
100 inout_fd_to_fh
101 input_fd_to_fh
102 output_fd_to_fh
103 fh_to_fh
104 possibly_fh_to_fh
105 );
106 51 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
107  
108 51 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 51 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
110 51 ▶use Chj::IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 51 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
112  
113 45 ▶sub glob_to_fh {
Subroutine glob_to_fh: called CC=3 SCAR=11.6
Pod undocumented
114 45 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 45 0 45
Condition33%1/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
0045
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table33%1/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
115 45 my ($glob, $maybe_layer_or_encoding) = @_;
116 45 my $fh = bless(*{$glob}{IO}, "Chj::IO::File");
117 45 $fh->perhaps_set_layer_or_encoding($maybe_layer_or_encoding);
118 45 $fh
119 }
120  
121 0 ▶sub stdin {
Subroutine stdin: not called CC=3 SCAR=24.8
Pod documented
122 0 ▶ @_ <= 1 or fp_croak_arity "0-1";
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 1 or fp_croak_arity("0-1") 0 0 0
123 0 ▶ my $encoding = @_ ? $_[0] : "utf-8";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
124 0 glob_to_fh(*STDIN, $encoding)
125 }
126  
127 0 ▶sub stdout {
Subroutine stdout: not called CC=3 SCAR=24.8
Pod documented
128 0 ▶ @_ <= 1 or fp_croak_arity "0-1";
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 1 or fp_croak_arity("0-1") 0 0 0
129 0 ▶ my $encoding = @_ ? $_[0] : "utf-8";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
130 0 glob_to_fh(*STDOUT, $encoding)
131 }
132  
133 0 ▶sub stderr {
Subroutine stderr: not called CC=3 SCAR=24.8
Pod documented
134 0 ▶ @_ <= 1 or fp_croak_arity "0-1";
Branch: 1 branch
BranchTrueFalseTotal
@_ <= 1 or fp_croak_arity("0-1") 0 0 0
135 0 ▶ my $encoding = @_ ? $_[0] : "utf-8";
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
136 0 glob_to_fh(*STDERR, $encoding)
137 }
138  
139 # --------------------------------------------------
140 # Turn unix fd-s to Chj::IO::File handles
141  
142 # `open my $fh, '<&'.$fd` dup's the file descriptor, so use
143 # IO::Handle's `new_from_fd` instead
144  
145 51 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
146  
147 0 ▶sub fd_to_fh {
Subroutine fd_to_fh: not called CC=4 SCAR=30.0
Pod undocumented
148 0 ▶ @_ >= 2 and @_ <= 3 or fp_croak_arity "2-3";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 and @_ <= 3 or fp_croak_arity("2-3") 0 0 0
Condition0%0/3
@_ >= 2 and @_ <= 3
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 2 and @_ <= 3
@_ >= 2 @_ <= 3
Truth table0%0/3
A and B
A= @_ >= 2B= @_ <= 3
ABresult
0X0
100
111
149 0 my ($fd, $mode, $maybe_layer_or_encoding) = @_;
150 0 ▶ $fd =~ /^\d+\z/s or die "fd argument must be a natural number";
Branch: 1 branch
BranchTrueFalseTotal
$fd =~ /^\d+\z/s or die "fd argument must be a natural number" 0 0 0
151 0 my $fh = IO::Handle->new_from_fd($fd, $mode);
152 0 bless $fh, "Chj::IO::File";
153 0 $fh->perhaps_set_layer_or_encoding($maybe_layer_or_encoding);
154 0 $fh
155 }
156  
157 0 ▶sub inout_fd_to_fh {
Subroutine inout_fd_to_fh: not called CC=3 SCAR=24.8
Pod undocumented
158 0 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 0 0 0
Condition0%0/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
159 0 my ($fd, $maybe_layer_or_encoding) = @_;
160 0 fd_to_fh $fd, "rw", $maybe_layer_or_encoding
161 }
162  
163 0 ▶sub input_fd_to_fh {
Subroutine input_fd_to_fh: not called CC=3 SCAR=24.8
Pod undocumented
164 0 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 0 0 0
Condition0%0/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
165 0 my ($fd, $maybe_layer_or_encoding) = @_;
166 0 fd_to_fh $fd, "r", $maybe_layer_or_encoding
167 }
168  
169 0 ▶sub output_fd_to_fh {
Subroutine output_fd_to_fh: not called CC=3 SCAR=24.8
Pod undocumented
170 0 ▶ @_ >= 1 and @_ <= 2 or fp_croak_arity "1-2";
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 1 and @_ <= 2 or fp_croak_arity("1-2") 0 0 0
Condition0%0/3
@_ >= 1 and @_ <= 2
!ll&&!rl&&r
000
MC/DC0%0/2
@_ >= 1 and @_ <= 2
@_ >= 1 @_ <= 2
Truth table0%0/3
A and B
A= @_ >= 1B= @_ <= 2
ABresult
0X0
100
111
171 0 my ($fd, $maybe_layer_or_encoding) = @_;
172 0 fd_to_fh $fd, "w", $maybe_layer_or_encoding
173 }
174  
175 # --------------------------------------------------
176 # Wrap a Perl fh of another kind (class) as a Chj::IO::File handle,
177 # for cases where reblessing is not ok.
178  
179 0 ▶sub fh_to_fh {
Subroutine fh_to_fh: not called CC=2 SCAR=17.9
Pod undocumented
180 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
181 0 my ($fh) = @_;
182 0 require Chj::IO::WrappedFile;
183 0 Chj::IO::WrappedFile->new($fh)
184 }
185  
186 19 ▶sub possibly_fh_to_fh {
Subroutine possibly_fh_to_fh: called CC=4 SCAR=16.4
Pod undocumented
187 19 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 19 0 19
188 19 my ($fh) = @_;
189 19 ▶ if (defined blessed($fh) and $fh->isa("Chj::IO::File")) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined builtin::blessed($fh) and $fh->isa('Chj::IO::File')) { } 19 0 19
Condition33%1/3
defined builtin::blessed($fh) and $fh->isa("Chj::IO::File")
!ll&&!rl&&r
0019
MC/DC0%0/2
defined builtin::blessed($fh) and $fh->isa("Chj::IO::File")
defined builtin::blessed($fh) $fh->isa("Chj::IO::File")
Truth table33%1/3
A and B
A= defined builtin::blessed($fh)B= $fh->isa("Chj::IO::File")
ABresult
0X0
100
111
190 19 $fh
191 } else {
192 0 fh_to_fh $fh
193 }
194 }
195  
196 # --------------------------------------------------
197 # Open filehandles from paths:
198  
199 0 ▶sub xopen {
Subroutine xopen: not called CC=1 SCAR=6.9
Pod documented
200 0 unshift @_, 'Chj::IO::File';
201 0 goto &Chj::IO::File::xopen
202 ; # (evil?, should it use ->can to remain OO based?)
203 }
204  
205 208 ▶sub xopen_read {
Subroutine xopen_read: called CC=6 SCAR=23.5
Pod documented
206 208 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 208 0 208
207 208 ▶ if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) { } 0 208 208
elsif (@_ == 1 and $_[0] eq '-') { } 0 208 208
208 0 ▶ croak "xopen_read: mode $1 not allowed"
Branch: 1 branch
BranchTrueFalseTotal
unless $2 0 0 0
209 unless $2; # XXX isn't this wong? Too many parens above?
210 ▶ } elsif (@_ == 1 and $_[0] eq '-') {
Condition33%1/3
@_ == 1 and $_[0] eq "-"
!ll&&!rl&&r
02080
MC/DC0%0/2
@_ == 1 and $_[0] eq "-"
@_ == 1 $_[0] eq "-"
Truth table33%1/3
A and B
A= @_ == 1B= $_[0] eq "-"
ABresult
0X0
100
111
211 0 @_ = ("<-")
212 } else {
213 208 unshift @_, "<";
214 }
215 208 unshift @_, 'Chj::IO::File';
216 208 goto &Chj::IO::File::xopen;
217 }
218  
219 # XX ok to simply use the 3-argument open and never allow 2-open
220 # strings at all? See how I seem to have gotten it wrong anyway, above!
221 11 ▶sub perhaps_xopen_read {
Subroutine perhaps_xopen_read: called CC=2 SCAR=7.0
Pod undocumented
222 11 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 11 0 11
223 11 unshift @_, "<";
224 11 unshift @_, 'Chj::IO::File';
225 11 goto &Chj::IO::File::perhaps_xopen;
226 }
227  
228 0 ▶sub perhaps_open_read {
Subroutine perhaps_open_read: not called CC=2 SCAR=17.9
Pod undocumented
229 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
230 0 unshift @_, "<";
231 0 unshift @_, 'Chj::IO::File';
232 0 goto &Chj::IO::File::perhaps_open;
233 }
234  
235 18 ▶sub xopen_write {
Subroutine xopen_write: called CC=7 SCAR=27.8
Pod documented
236 18 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 18 0 18
237 18 ▶ if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) { } 0 18 18
elsif (@_ == 1 and $_[0] eq '-') { } 0 18 18
238 0 ▶ croak "xopen_write: mode $1 not allowed" unless $3 or $4;
Branch: 1 branch
BranchTrueFalseTotal
unless $3 or $4 0 0 0
Condition0%0/3
$3 or $4
l!l&&r!l&&!r
000
MC/DC0%0/2
$3 or $4
$3 $4
Truth table0%0/3
A or B
A= $3B= $4
ABresult
1X1
011
000
239 ▶ } elsif (@_ == 1 and $_[0] eq '-') {
Condition33%1/3
@_ == 1 and $_[0] eq "-"
!ll&&!rl&&r
0180
MC/DC0%0/2
@_ == 1 and $_[0] eq "-"
@_ == 1 $_[0] eq "-"
Truth table33%1/3
A and B
A= @_ == 1B= $_[0] eq "-"
ABresult
0X0
100
111
240 0 @_ = (">-")
241 } else {
242 18 unshift @_, ">";
243 }
244 18 unshift @_, 'Chj::IO::File';
245 18 goto &Chj::IO::File::xopen;
246 }
247  
248 0 ▶sub xopen_append {
Subroutine xopen_append: not called CC=6 SCAR=37.4
Pod documented
249 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
250 0 ▶ if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) { } 0 0 0
elsif (@_ == 1 and $_[0] eq '-') { } 0 0 0
251 0 ▶ croak "xopen_append: mode $1 not allowed" unless $3;
Branch: 1 branch
BranchTrueFalseTotal
unless $3 0 0 0
252 ▶ } elsif (@_ == 1 and $_[0] eq '-') {
Condition0%0/3
@_ == 1 and $_[0] eq "-"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and $_[0] eq "-"
@_ == 1 $_[0] eq "-"
Truth table0%0/3
A and B
A= @_ == 1B= $_[0] eq "-"
ABresult
0X0
100
111
253 0 @_ = (" >> -")
254 } else {
255 0 unshift @_, " >> ";
256 }
257 0 unshift @_, 'Chj::IO::File';
258 0 goto &Chj::IO::File::xopen;
259 }
260  
261 0 ▶sub xopen_update {
Subroutine xopen_update: not called CC=7 SCAR=40.3
Pod documented
262 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
263 0 ▶ if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] =~ /^((<)|( >> )|(>)|(\+<)|(\+>))/) { } 0 0 0
elsif (@_ == 1 and $_[0] eq '-') { } 0 0 0
264 0 ▶ croak "xopen_update: mode $1 not allowed" unless $5 or $6;
Branch: 1 branch
BranchTrueFalseTotal
unless $5 or $6 0 0 0
Condition0%0/3
$5 or $6
l!l&&r!l&&!r
000
MC/DC0%0/2
$5 or $6
$5 $6
Truth table0%0/3
A or B
A= $5B= $6
ABresult
1X1
011
000
265 ▶ } elsif (@_ == 1 and $_[0] eq '-') {
Condition0%0/3
@_ == 1 and $_[0] eq "-"
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and $_[0] eq "-"
@_ == 1 $_[0] eq "-"
Truth table0%0/3
A and B
A= @_ == 1B= $_[0] eq "-"
ABresult
0X0
100
111
266 0 @_ = ("+<-")
267 } else {
268 0 unshift @_, "+<";
269 }
270 0 unshift @_, 'Chj::IO::File';
271 0 goto &Chj::IO::File::xopen;
272 }
273  
274 51 our $devnull;
275  
276 0 ▶sub devnull {
Subroutine devnull: not called CC=2 SCAR=17.9
Pod undocumented
277 0 ▶ $devnull ||= do {
Condition0%0/3
$Chj::xopen::devnull ||= do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/null", &O_RDWR) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$Chj::xopen::devnull ||= do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/null", &O_RDWR) }
$Chj::xopen::devnull do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/null", &O_RDWR) }
Truth table0%0/3
A ||= B
A= $Chj::xopen::devnullB= do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/null", &O_RDWR) }
ABresult
1X1
011
000
278 0 require POSIX;
279 0 import POSIX "O_RDWR";
280 0 Chj::IO::File->xsysopen("/dev/null", &O_RDWR) ## no critic
281 }
282 }
283 51 our $devzero;
284  
285 0 ▶sub devzero {
Subroutine devzero: not called CC=2 SCAR=17.9
Pod undocumented
286 0 ▶ $devzero ||= do {
Condition0%0/3
$Chj::xopen::devzero ||= do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/zero", &O_RDWR) }
l!l&&r!l&&!r
000
MC/DC0%0/2
$Chj::xopen::devzero ||= do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/zero", &O_RDWR) }
$Chj::xopen::devzero do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/zero", &O_RDWR) }
Truth table0%0/3
A ||= B
A= $Chj::xopen::devzeroB= do { require POSIX; "POSIX"->import("O_RDWR"); "Chj::IO::File"->xsysopen("/dev/zero", &O_RDWR) }
ABresult
1X1
011
000
287 0 require POSIX;
288 0 import POSIX "O_RDWR";
289 0 Chj::IO::File->xsysopen("/dev/zero", &O_RDWR) ## no critic
290 }
291 }
292  
293 51 1
« lib/FP/Repl/Stack.pm ↑ Summary lib/FP/Repl/WithRepl.pm »