Class/File.pm

filter: Statementstmt 87.1% 108 / 124 Branchbran 59.7% 43 / 72 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 82.7% 24 / 29 Podpod 95.2% 20 / 21 totaltotal 78.2% 205 / 262 CC 42 SCAR 18.4
CC
42
Cov
77%
copy_to
15.6
slurp
11.1
spew
8.3
CRAP 6.3
↑ Summary Class/Dir.pm »
1 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 package Path::Class::File;
4 {
5 7 $Path::Class::File::VERSION = '0.37';
6 }
7  
8 7 ▶use Path::Class::Dir;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use parent qw(Path::Class::Entity);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 7 ▶use IO::File ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 158 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
15 158 my $self = shift->SUPER::new;
16 158 my $file = pop();
17 158 my @dirs = @_;
18  
19 158 my ($volume, $dirs, $base) = $self->_spec->splitpath($file);
20  
21 158 ▶ if (length $dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $dirs) 24 134 158
22 24 push @dirs, $self->_spec->catpath($volume, $dirs, '');
23 }
24  
25 158 ▶ $self->{dir} = @dirs ? $self->dir_class->new(@dirs) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@dirs ? : 154 4 158
26 158 $self->{file} = $base;
27  
28 158 return $self;
29 }
30  
31 153 ▶sub dir_class { "Path::Class::Dir" }
Subroutine dir_class: called CC=1 SCAR=0.0
Pod documented
32  
33 18 ▶sub as_foreign {
Subroutine as_foreign: called CC=2 SCAR=7.0
Pod documented
34 18 my ($self, $type) = @_;
35 18 local $Path::Class::Foreign = $self->_spec_class($type);
36 18 my $foreign = ref($self)->SUPER::new;
37 18 ▶ $foreign->{dir} = $self->{dir}->as_foreign($type) if defined $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dir"} 18 0 18
38 18 $foreign->{file} = $self->{file};
39 18 return $foreign;
40 }
41  
42 302 ▶sub stringify {
Subroutine stringify: called CC=2 SCAR=6.9
Pod documented
43 302 my $self = shift;
44 302 ▶ return $self->{file} unless defined $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"dir"} 4 298 302
45 298 return $self->_spec->catfile($self->{dir}->stringify, $self->{file});
46 }
47  
48 21 ▶sub dir {
Subroutine dir: called CC=2 SCAR=6.9
Pod documented
49 21 my $self = shift;
50 21 ▶ return $self->{dir} if defined $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"dir"} 20 1 21
51 1 return $self->dir_class->new($self->_spec->curdir);
52 }
53 7 ▶BEGIN { *parent = \&dir; }
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55 0 ▶sub volume {
Subroutine volume: not called CC=2 SCAR=17.9
Pod documented
56 0 my $self = shift;
57 0 ▶ return '' unless defined $self->{dir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"dir"} 0 0 0
58 0 return $self->{dir}->volume;
59 }
60  
61 8 ▶sub components {
Subroutine components: called CC=2 SCAR=7.1
Pod documented
62 8 my $self = shift;
63 8 ▶ croak "Arguments are not currently supported by File->components()" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 8 8
64 8 return ($self->dir->components, $self->basename);
65 }
66  
67 18 ▶sub basename { shift->{file} }
Subroutine basename: called CC=1 SCAR=0.0
Pod documented
68 42 ▶sub open { IO::File->new(@_) }
Subroutine open: called CC=1 SCAR=0.0
Pod documented
69  
70 0 ▶sub openr { $_[0]->open('r') or croak "Can't read $_[0]: $!" }
Subroutine openr: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->open("r") or croak("Can't read $_[0]: $!") 0 0 0
71 19 ▶sub openw { $_[0]->open('w') or croak "Can't write to $_[0]: $!" }
Subroutine openw: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->open("w") or croak("Can't write to $_[0]: $!") 19 0 19
72 0 ▶sub opena { $_[0]->open('a') or croak "Can't append to $_[0]: $!" }
Subroutine opena: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->open("a") or croak("Can't append to $_[0]: $!") 0 0 0
73  
74 19 ▶sub touch {
Subroutine touch: called CC=2 SCAR=7.6
Pod documented
75 19 my $self = shift;
76 19 ▶ if (-e $self) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self) { } 0 19 19
77 0 utime undef, undef, $self;
78 } else {
79 19 $self->openw;
80 }
81 }
82  
83 13 ▶sub slurp {
Subroutine slurp: called CC=10 SCAR=24.1
Pod documented
84 13 my ($self, %args) = @_;
85 13 ▶ my $iomode = $args{iomode} || 'r';
Condition100%2/2
$args{'iomode'} || 'r'
l!l
58
MC/DC100%1/1
$args{'iomode'} || 'r'
$args{'iomode'}
Truth table100%2/2
A
A= $args{'iomode'}
Aresult
11
00
86 13 ▶ my $fh = $self->open($iomode) or croak "Can't read $self: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->open($iomode) or croak("Can't read ${self}: $!") 13 0 13
87  
88 13 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 6 7 13
89 6 my @data = <$fh>;
90 6 ▶ chomp @data if $args{chomped} or $args{chomp};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"chomped"} or $args{"chomp"} 4 2 6
Condition66%2/3
$args{"chomped"} or $args{"chomp"}
l!l&&r!l&&!r
042
MC/DC50%1/2
$args{"chomped"} or $args{"chomp"}
$args{"chomped"} $args{"chomp"}
Truth table66%2/3
A or B
A= $args{"chomped"}B= $args{"chomp"}
ABresult
1X1
011
000
91  
92 6 ▶ if ( my $splitter = $args{split} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $splitter = $args{"split"}) 2 4 6
93 2 @data = map { [ split $splitter, $_ ] } @data;
94 }
95  
96 6 return @data;
97 }
98  
99  
100 croak "'split' argument can only be used in list context"
101 7 ▶ if $args{split};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"split"} 0 7 7
102  
103  
104 7 ▶ if ($args{chomped} or $args{chomp}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"chomped"} or $args{"chomp"}) 0 7 7
Condition33%1/3
$args{"chomped"} or $args{"chomp"}
l!l&&r!l&&!r
007
MC/DC0%0/2
$args{"chomped"} or $args{"chomp"}
$args{"chomped"} $args{"chomp"}
Truth table33%1/3
A or B
A= $args{"chomped"}B= $args{"chomp"}
ABresult
1X1
011
000
105 0 chomp( my @data = <$fh> );
106 0 return join '', @data;
107 }
108  
109  
110 7 local $/;
111 7 return <$fh>;
112 }
113  
114 6 ▶sub spew {
Subroutine spew: called CC=8 SCAR=21.1
Pod documented
115 6 my $self = shift;
116 6 my %args = splice( @_, 0, @_-1 );
117  
118 6 ▶ my $iomode = $args{iomode} || 'w';
Condition100%2/2
$args{'iomode'} || 'w'
l!l
42
MC/DC100%1/1
$args{'iomode'} || 'w'
$args{'iomode'}
Truth table100%2/2
A
A= $args{'iomode'}
Aresult
11
00
119 6 ▶ my $fh = $self->open( $iomode ) or croak "Can't write to $self: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->open($iomode) or croak("Can't write to ${self}: $!") 6 0 6
120  
121 6 ▶ if (ref($_[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'ARRAY') { } 1 5 6
122 # Use old-school for loop to avoid copying.
123 1 ▶ for (my $i = 0; $i < @{ $_[0] }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[0];}) 4 1 5
124 4 ▶ print $fh $_[0]->[$i]
Branch: 1 branch
BranchTrueFalseTotal
print $fh $_[0][$i] or croak("Can't write to ${self}: $!") 4 0 4
125 or croak "Can't write to $self: $!";
126 }
127 }
128 else {
129 5 ▶ print $fh $_[0]
Branch: 1 branch
BranchTrueFalseTotal
print $fh $_[0] or croak("Can't write to ${self}: $!") 5 0 5
130 or croak "Can't write to $self: $!";
131 }
132  
133 6 ▶ close $fh
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("Can't write to ${self}: $!") 6 0 6
134 or croak "Can't write to $self: $!";
135  
136 6 return;
137 }
138  
139 2 ▶sub spew_lines {
Subroutine spew_lines: called CC=2 SCAR=6.9
Pod documented
140 2 my $self = shift;
141 2 my %args = splice( @_, 0, @_-1 );
142  
143 2 my $content = $_[0];
144  
145 # If content is an array ref, appends $/ to each element of the array.
146 # Otherwise, if it is a simple scalar, just appends $/ to that scalar.
147  
148 $content
149 = ref( $content ) eq 'ARRAY'
150 2 ▶ ? [ map { $_, $/ } @$content ]
Branch: 1 branch
BranchTrueFalseTotal
ref $content eq 'ARRAY' ? : 1 1 2
151 : "$content$/";
152  
153 2 return $self->spew( %args, $content );
154 }
155  
156 7 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
157 7 my $file = shift->stringify;
158 7 ▶ return unlink $file unless -e $file; # Sets $! correctly
Branch: 1 branch
BranchTrueFalseTotal
unless -e $file 0 7 7
159 7 ▶ 1 while unlink $file;
Branch: 1 branch
BranchTrueFalseTotal
unlink $file and do { '???' } 7 7 14
160 7 return not -e $file;
161 }
162  
163 2 ▶sub copy_to {
Subroutine copy_to: called CC=10 SCAR=27.5
Pod documented
164 2 my ($self, $dest) = @_;
165 2 ▶ if ( eval{ $dest->isa("Path::Class::File")} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (eval { do { $dest->isa('Path::Class::File') } }) { } 1 1 2
elsif (eval { do { $dest->isa('Path::Class::Dir') } }) { } 1 0 1
elsif (ref $dest) { } 0 0 0
166 1 $dest = $dest->stringify;
167 1 ▶ croak "Can't copy to file $dest: it is a directory" if -d $dest;
Branch: 1 branch
BranchTrueFalseTotal
if -d $dest 0 1 1
168 1 } elsif ( eval{ $dest->isa("Path::Class::Dir") } ) {
169 1 $dest = $dest->stringify;
170 1 ▶ croak "Can't copy to directory $dest: it is a file" if -f $dest;
Branch: 1 branch
BranchTrueFalseTotal
if -f $dest 0 1 1
171 1 ▶ croak "Can't copy to directory $dest: no such directory" unless -d $dest;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dest 0 1 1
172 } elsif ( ref $dest ) {
173 0 croak "Don't know how to copy files to objects of type '".ref($self)."'";
174 }
175  
176 2 require Perl::OSType;
177 2 ▶ if ( !Perl::OSType::is_os_type('Unix') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not Perl::OSType::is_os_type('Unix')) { } 0 2 2
178  
179 0 require File::Copy;
180 0 ▶ return unless File::Copy::cp($self->stringify, "${dest}");
Branch: 1 branch
BranchTrueFalseTotal
unless File::Copy::cp($self->stringify, "$dest") 0 0 0
181  
182 } else {
183  
184 2 ▶ return unless (system('cp', $self->stringify, "${dest}") == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless system("cp", $self->stringify, "$dest") == 0 0 2 2
185  
186 }
187  
188 2 return $self->new($dest);
189 }
190  
191 1 ▶sub move_to {
Subroutine move_to: called CC=3 SCAR=11.3
Pod documented
192 1 my ($self, $dest) = @_;
193 1 require File::Copy;
194 1 ▶ if (File::Copy::move($self->stringify, "${dest}")) {
Branch: 1 branch
BranchTrueFalseTotal
if (File::Copy::move($self->stringify, "$dest")) { } 1 0 1
195  
196 1 my $new = $self->new($dest);
197  
198 1 $self->{$_} = $new->{$_} foreach (qw/ dir file /);
199  
200 1 return $self;
201  
202 } else {
203  
204 0 return;
205  
206 }
207 }
208  
209 12 ▶sub traverse {
Subroutine traverse: called CC=1 SCAR=0.0
Pod documented
210 12 my $self = shift;
211 12 my ($callback, @args) = @_;
212 12 ▶ return $self->$callback(sub { () }, @args);
Subroutine __ANON__: called CC=1 SCAR=0.0
213 }
214  
215 0 ▶sub traverse_if {
Subroutine traverse_if: not called CC=1 SCAR=6.9
Pod undocumented
216 0 my $self = shift;
217 0 my ($callback, $condition, @args) = @_;
218 0 ▶ return $self->$callback(sub { () }, @args);
Subroutine __ANON__: not called CC=1 SCAR=6.9
219 }
220  
221 7 1;
222 __END__
↑ Summary Class/Dir.pm »