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