lib/Chj/IO/Dir.pm

filter: Statementstmt 23.6% 27 / 114 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 33.3% 8 / 24 Podpod 0.0% 0 / 15 totaltotal 15.9% 35 / 220 CC 27 SCAR 28.3
CC
27
Cov
16%
nread
42.0
xnread
42.0
xread
30.0
CRAP 17.0
« lib/Chj/Util/AskYN.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Cost.pm »
1 #
2 # Copyright (c) 2003-2021 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::IO::Dir
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 See L<Chj::xopendir>.
19  
20 =head1 NOTE
21  
22 This is alpha software! Read the status section in the package README
23 or on the L<website|http://functional-perl.org/>.
24  
25 =cut
26  
27 package Chj::IO::Dir;
28  
29 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 25 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 25 ▶use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 25 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 25 ▶use Chj::singlequote ();
Subroutine BEGIN: called CC=1 SCAR=0.0
36 25 ▶use POSIX qw(EEXIST EBADF ENOENT);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 25 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 25 my %metadata; # -> [ is_open, path ]
40 25 $foo::foo = \%metadata;
41  
42 0 ▶sub path {
Subroutine path: not called CC=1 SCAR=6.9
Pod undocumented
43 0 my $self = shift;
44 0 $metadata{ pack "I", $self }[1]
45 }
46  
47 0 ▶sub xopendir {
Subroutine xopendir: not called CC=2 SCAR=17.9
Pod undocumented
48 0 my $class = shift;
49 0 my $hdl = gensym;
50 0 $! = undef;
51 0 ▶ if (opendir $hdl, $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::opendir $hdl, $_[0]) { } 0 0 0
52 0 bless $hdl, $class;
53 0 $metadata{ pack "I", $hdl } = [1, $_[0]];
54 0 return $hdl;
55 } else {
56 0 croak "xopendir " . Chj::singlequote::singlequote_many(@_) . ": $!";
57 }
58 }
59  
60 # *new = \&xopendir; really? no.
61  
62 0 ▶sub opendir {
Subroutine opendir: not called CC=2 SCAR=17.9
Pod undocumented
63 0 my $class = shift;
64 0 my $hdl = gensym;
65 0 $! = undef;
66 0 ▶ if (opendir $hdl, $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::opendir $hdl, $_[0]) { } 0 0 0
67 0 bless $hdl, $class;
68 0 $metadata{ pack "I", $hdl } = [1, $_[0]];
69 0 return $hdl;
70 } else {
71 undef
72 0 }
73 }
74  
75 0 ▶sub perhaps_opendir {
Subroutine perhaps_opendir: not called CC=2 SCAR=17.9
Pod undocumented
76 0 my $class = shift;
77 0 $! = undef;
78 0 ▶ if (defined(my $fh = $class->opendir(@_))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fh = $class->opendir(@_))) { } 0 0 0
79 0 $fh
80 } else {
81 ()
82 0 }
83 }
84  
85 # (adapted copy of perhaps_xopen of File.pm)
86 # die on all errors except ENOENT
87 0 ▶sub perhaps_xopendir {
Subroutine perhaps_xopendir: not called CC=3 SCAR=24.8
Pod undocumented
88 0 my $proto = shift;
89 0 ▶ if (my ($fh) = $proto->perhaps_opendir(@_)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my($fh) = $proto->perhaps_opendir(@_)) { } 0 0 0
elsif ($! == 2) { } 0 0 0
90 0 $fh
91 } elsif ($! == ENOENT) {
92 ()
93 0 } else {
94 0 croak "xopen @_: $!";
95 }
96 }
97  
98 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my $class = shift;
100 0 my $self = gensym;
101 0 bless $self, $class
102 }
103  
104 0 ▶sub read {
Subroutine read: not called CC=1 SCAR=6.9
Pod undocumented
105 0 my $self = shift;
106 0 $! = undef;
107 0 readdir $self
108 }
109  
110 0 ▶sub xread {
Subroutine xread: not called CC=5 SCAR=34.0
Pod undocumented
111 0 my $self = shift;
112 0 $! = undef;
113  
114 # ^ Needed, CORE::readdir will not set it to 0. Thus maybe it will
115 # not even set any error? Hm, well, at least on end of dir it sets
116 # it to Bad file descriptor.
117 0 ▶ if (wantarray) { ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
118 0 my $res = [CORE::readdir $self];
119  
120 # we *hope* that [ ] will never copy until the end as opposed
121 # to @res = which *might* (well probably (or I think IIRC I've
122 # even tested and confirmed it) does) copy all elements.
123 0 ▶ if ($!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($!) 0 0 0
124 0 croak "xread: $!";
125 }
126 0 @$res
127 } else {
128 0 my $res = CORE::readdir $self;
129 0 ▶ if ($! and $! != EBADF) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! and $! != 9) 0 0 0
Condition0%0/3
$! and $! != 9
!ll&&!rl&&r
000
MC/DC0%0/2
$! and $! != 9
$! $! != 9
Truth table0%0/3
A and B
A= $!B= $! != 9
ABresult
0X0
100
111
130 0 croak "xread: $!";
131  
132 #croak "xread: $! (".($!+0).")"; ## exception objects would still be coool
133 }
134 $res
135 0 }
136 }
137  
138 0 ▶sub nread { # ignore . and .. entries
Subroutine nread: not called CC=6 SCAR=37.4
Pod undocumented
139 0 my $self = shift;
140 0 $! = undef;
141 0 ▶ if (wantarray) { ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
142 0 ▶ grep { $_ ne '.' and $_ ne '..' } readdir $self
Condition0%0/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
143 } else {
144 0 ▶ while (defined(my $item = readdir $self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = readdir $self)) 0 0 0
145 0 ▶ return $item unless $item eq '.' or $item eq '..';
Branch: 1 branch
BranchTrueFalseTotal
unless $item eq "." or $item eq ".." 0 0 0
Condition0%0/3
$item eq "." or $item eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$item eq "." or $item eq ".."
$item eq "." $item eq ".."
Truth table0%0/3
A or B
A= $item eq "."B= $item eq ".."
ABresult
1X1
011
000
146 }
147 undef
148 0 }
149 }
150  
151 0 ▶sub xnread {
Subroutine xnread: not called CC=6 SCAR=37.4
Pod undocumented
152 0 my $self = shift;
153 0 $! = undef;
154 0 ▶ if (wantarray) { ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
155 0 ▶ my $res = [grep { $_ ne '.' and $_ ne '..' } readdir $self];
Condition0%0/3
$_ ne '.' && $_ ne '..'
!ll&&!rl&&r
000
MC/DC0%0/2
$_ ne '.' && $_ ne '..'
$_ ne '.' $_ ne '..'
Truth table0%0/3
A && B
A= $_ ne '.'B= $_ ne '..'
ABresult
0X0
100
111
156 0 @$res
157 } else {
158 0 ▶ while (defined(my $item = readdir $self)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $item = readdir $self)) 0 0 0
159 0 ▶ return $item unless $item eq '.' or $item eq '..';
Branch: 1 branch
BranchTrueFalseTotal
unless $item eq "." or $item eq ".." 0 0 0
Condition0%0/3
$item eq "." or $item eq ".."
l!l&&r!l&&!r
000
MC/DC0%0/2
$item eq "." or $item eq ".."
$item eq "." $item eq ".."
Truth table0%0/3
A or B
A= $item eq "."B= $item eq ".."
ABresult
1X1
011
000
160 }
161 undef
162 0 }
163 }
164  
165 0 ▶sub telldir {
Subroutine telldir: not called CC=1 SCAR=6.9
Pod undocumented
166 0 my $self = shift;
167 0 $! = undef;
168 0 CORE::telldir $self
169 }
170  
171 0 ▶sub seekdir {
Subroutine seekdir: not called CC=2 SCAR=17.9
Pod undocumented
172 0 my $self = shift;
173 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
174 0 my ($pos) = @_;
175 0 $! = undef;
176 0 CORE::seekdir $self, $pos
177 }
178  
179 0 ▶sub xseekdir {
Subroutine xseekdir: not called CC=3 SCAR=24.8
Pod undocumented
180 0 my $self = shift;
181 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
182 0 my ($pos) = @_;
183 0 $! = undef;
184 0 ▶ CORE::seekdir $self, $pos or croak "xseekdir (UNTESTED): $!"; ##
Branch: 1 branch
BranchTrueFalseTotal
CORE::seekdir $self, $pos or croak("xseekdir (UNTESTED): $!") 0 0 0
185 }
186  
187 0 ▶sub xrewind {
Subroutine xrewind: not called CC=2 SCAR=17.9
Pod undocumented
188 0 my $self = shift;
189 0 $! = undef;
190 0 ▶ CORE::seekdir $self, 0 or croak "xrewind (UNTESTED): $!"; ##
Branch: 1 branch
BranchTrueFalseTotal
CORE::seekdir $self, 0 or croak("xrewind (UNTESTED): $!") 0 0 0
191 }
192  
193 0 ▶sub xclose {
Subroutine xclose: not called CC=2 SCAR=17.9
Pod undocumented
194 0 my $self = shift;
195  
196 #(maybe check metadata is_open first? not really useful)
197 0 $! = undef;
198 0 ▶ closedir $self or croak "xclose: $!";
Branch: 1 branch
BranchTrueFalseTotal
closedir $self or croak("xclose: $!") 0 0 0
199 0 $metadata{ pack "I", $self }[0] = 0
200 }
201  
202 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
203 0 my $self = shift;
204 0 local ($@, $!, $?, $_);
205 0 ▶ if ($metadata{ pack "I", $self }[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($metadata{pack "I", $self}[0]) 0 0 0
206 0 ▶ closedir $self or carp "$self DESTROY: $!";
Branch: 1 branch
BranchTrueFalseTotal
closedir $self or carp("$self DESTROY: $!") 0 0 0
207 }
208 0 delete $metadata{ pack "I", $self };
209 }
210  
211 25 1;
« lib/Chj/Util/AskYN.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/Cost.pm »