blib/lib/File/chdir.pm

filter: Statementstmt 88.1% 97 / 110 Branchbran 71.4% 10 / 14 Conditioncond 60.0% 3 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 87.1% 27 / 31 Podpod n/a totaltotal 84.0% 137 / 163 SCAR 21.0
CC
8
Cov
85%
EXISTS
6.0
SPLICE
3.0
_chdir
2.0
CRAP 8.2
↑ Summary
1 package File::chdir;
2 6 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use vars qw($VERSION @ISA @EXPORT $CWD @CWD);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: a more sensible way to change directories
6 6 our $VERSION = '0.1009'; # VERSION
7  
8 6 require Exporter;
9 6 @ISA = qw(Exporter);
10 6 @EXPORT = qw(*CWD);
11  
12 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 6 ▶use Cwd 3.16;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use File::Spec::Functions 3.27 qw/canonpath splitpath catpath splitdir catdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 6 ▶tie $CWD, 'File::chdir::SCALAR' or die "Can't tie \$CWD";
Branch: 1 branch
BranchTrueFalseTotal
tie $File::chdir::CWD, "File::chdir::SCALAR" or die "Can't tie \$CWD" 0 6 6
17 6 ▶tie @CWD, 'File::chdir::ARRAY' or die "Can't tie \@CWD";
Branch: 1 branch
BranchTrueFalseTotal
tie @File::chdir::CWD, "File::chdir::ARRAY" or die "Can't tie \@CWD" 0 6 6
18  
19 sub _abs_path {
20 # Otherwise we'll never work under taint mode.
21 258 ▶ my($cwd) = Cwd::getcwd =~ /(.*)/s;
Subroutine _abs_path: called CC=1 SCAR=0.0
22 # Run through File::Spec, since everything else uses it
23 258 return canonpath($cwd);
24 }
25  
26 # splitpath but also split directory
27 sub _split_cwd {
28 219 ▶ my ($vol, $dir) = splitpath(_abs_path, 1);
Subroutine _split_cwd: called CC=1 SCAR=0.0
29 219 my @dirs = splitdir( $dir );
30 219 shift @dirs; # get rid of leading empty "root" directory
31 219 return ($vol, @dirs);
32 }
33  
34 # catpath, but take list of directories
35 # restore the empty root dir and provide an empty file to avoid warnings
36 sub _catpath {
37 46 ▶ my ($vol, @dirs) = @_;
Subroutine _catpath: called CC=1 SCAR=0.0
38 46 return catpath($vol, catdir(q{}, @dirs), q{});
39 }
40  
41 sub _chdir {
42 # Untaint target directory
43 63 ▶ my ($new_dir) = $_[0] =~ /(.*)/s;
Subroutine _chdir: called CC=2 SCAR=6.9
44  
45 63 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
46 63 ▶ if ( ! CORE::chdir($new_dir) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (chdir $new_dir) 2 61 63
47 2 croak "Failed to change directory to '$new_dir': $!";
48 };
49 61 return 1;
50 }
51  
52 {
53 package File::chdir::SCALAR;
54 6 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
55  
56 BEGIN {
57 6 ▶ *_abs_path = \&File::chdir::_abs_path;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 6 *_chdir = \&File::chdir::_chdir;
59 6 *_split_cwd = \&File::chdir::_split_cwd;
60 6 *_catpath = \&File::chdir::_catpath;
61 }
62  
63 sub TIESCALAR {
64 6 ▶ bless [], $_[0];
Subroutine TIESCALAR: called CC=1 SCAR=0.0
65 }
66  
67 # To be safe, in case someone chdir'd out from under us, we always
68 # check the Cwd explicitly.
69 sub FETCH {
70 39 ▶ return _abs_path;
Subroutine FETCH: called CC=1 SCAR=0.0
71 }
72  
73 sub STORE {
74 27 ▶ return unless defined $_[1];
Subroutine STORE: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] 10 17 27
75 17 _chdir($_[1]);
76 }
77 }
78  
79  
80 {
81 6 package File::chdir::ARRAY;
82 6 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 BEGIN {
85 6 ▶ *_abs_path = \&File::chdir::_abs_path;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 6 *_chdir = \&File::chdir::_chdir;
87 6 *_split_cwd = \&File::chdir::_split_cwd;
88 6 *_catpath = \&File::chdir::_catpath;
89 }
90  
91 sub TIEARRAY {
92 6 ▶ bless {}, $_[0];
Subroutine TIEARRAY: called CC=1 SCAR=0.0
93 }
94  
95 sub FETCH {
96 70 ▶ my($self, $idx) = @_;
Subroutine FETCH: called CC=1 SCAR=0.0
97 70 my ($vol, @cwd) = _split_cwd;
98 70 return $cwd[$idx];
99 }
100  
101 sub STORE {
102 31 ▶ my($self, $idx, $val) = @_;
Subroutine STORE: called CC=2 SCAR=6.9
103  
104 31 my ($vol, @cwd) = _split_cwd;
105 31 ▶ if( $self->{Cleared} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"Cleared"}) 7 24 31
106 7 @cwd = ();
107 7 $self->{Cleared} = 0;
108 }
109  
110 31 $cwd[$idx] = $val;
111 31 my $dir = _catpath($vol,@cwd);
112  
113 31 _chdir($dir);
114 30 return $cwd[$idx];
115 }
116  
117 sub FETCHSIZE {
118 103 ▶ my ($vol, @cwd) = _split_cwd;
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
119 103 return scalar @cwd;
120 }
121 ▶ sub STORESIZE {}
Subroutine STORESIZE: not called CC=1 SCAR=0.0
122  
123 sub PUSH {
124 7 ▶ my($self) = shift;
Subroutine PUSH: called CC=1 SCAR=0.0
125  
126 7 my $dir = _catpath(_split_cwd, @_);
127 7 _chdir($dir);
128 7 return $self->FETCHSIZE;
129 }
130  
131 sub POP {
132 4 ▶ my($self) = shift;
Subroutine POP: called CC=1 SCAR=0.0
133  
134 4 my ($vol, @cwd) = _split_cwd;
135 4 my $popped = pop @cwd;
136 4 my $dir = _catpath($vol,@cwd);
137 4 _chdir($dir);
138 4 return $popped;
139 }
140  
141 sub SHIFT {
142 0 ▶ my($self) = shift;
Subroutine SHIFT: not called CC=1 SCAR=6.9
143  
144 0 my ($vol, @cwd) = _split_cwd;
145 0 my $shifted = shift @cwd;
146 0 my $dir = _catpath($vol,@cwd);
147 0 _chdir($dir);
148 0 return $shifted;
149 }
150  
151 sub UNSHIFT {
152 0 ▶ my($self) = shift;
Subroutine UNSHIFT: not called CC=1 SCAR=6.9
153  
154 0 my ($vol, @cwd) = _split_cwd;
155 0 my $dir = _catpath($vol, @_, @cwd);
156 0 _chdir($dir);
157 0 return $self->FETCHSIZE;
158 }
159  
160 sub CLEAR {
161 7 ▶ my($self) = shift;
Subroutine CLEAR: called CC=1 SCAR=0.0
162 7 $self->{Cleared} = 1;
163 }
164  
165 sub SPLICE {
166 4 ▶ my $self = shift;
Subroutine SPLICE: called CC=3 SCAR=11.1
167 4 ▶ my $offset = shift || 0;
Condition50%1/2
shift() || 0
l!l
40
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
168 4 ▶ my $len = shift || $self->FETCHSIZE - $offset;
Condition66%2/3
shift() || $self->FETCHSIZE - $offset
l!l&&r!l&&!r
220
MC/DC0%0/2
shift() || $self->FETCHSIZE - $offset
shift() $self->FETCHSIZE - $offset
Truth table66%2/3
A || B
A= shift()B= $self->FETCHSIZE - $offset
ABresult
1X1
011
000
169 4 my @new_dirs = @_;
170  
171 4 my ($vol, @cwd) = _split_cwd;
172 4 my @orig_dirs = splice @cwd, $offset, $len, @new_dirs;
173 4 my $dir = _catpath($vol, @cwd);
174 4 _chdir($dir);
175 4 return @orig_dirs;
176 }
177  
178 ▶ sub EXTEND { }
Subroutine EXTEND: called CC=1 SCAR=0.0
179 sub EXISTS {
180 0 ▶ my($self, $idx) = @_;
Subroutine EXISTS: not called CC=2 SCAR=17.9
181 0 ▶ return $self->FETCHSIZE >= $idx ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->FETCHSIZE >= $idx ? : 0 0 0
182 }
183  
184 sub DELETE {
185 3 ▶ my($self, $idx) = @_;
Subroutine DELETE: called CC=2 SCAR=6.9
186 3 ▶ croak "Can't delete except at the end of \@CWD"
Branch: 1 branch
BranchTrueFalseTotal
if $idx < $self->FETCHSIZE - 1 1 2 3
187 if $idx < $self->FETCHSIZE - 1;
188 2 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
189 2 $self->POP;
190 }
191 }
192  
193 6 1;
194  
195 __END__
↑ Summary