File Coverage

blib/lib/Path/Class/File.pm
Criterion Covered Total %
statement 106 121 87.6
branch 36 62 58.0
condition 7 10 70.0
subroutine 26 31 83.8
pod 20 21 95.2
total 195 245 79.5


line stmt bran cond sub pod time code
1 7     7   25554 use strict;
  7         13  
  7         404  
2              
3             package Path::Class::File;
4             {
5             $Path::Class::File::VERSION = '0.35';
6             }
7              
8 7     7   5303 use Path::Class::Dir;
  7         19  
  7         237  
9 7     7   49 use parent qw(Path::Class::Entity);
  7         14  
  7         55  
10 7     7   453 use Carp;
  7         13  
  7         488  
11              
12 7     7   37 use IO::File ();
  7         13  
  7         114  
13 7     7   6711 use Perl::OSType ();
  7         3215  
  7         143  
14 7     7   6634 use File::Copy ();
  7         18410  
  7         2388  
15              
16             sub new {
17 157     157 1 1515 my $self = shift->SUPER::new;
18 157         428 my $file = pop();
19 157         303 my @dirs = @_;
20              
21 157         701 my ($volume, $dirs, $base) = $self->_spec->splitpath($file);
22              
23 157 100       542 if (length $dirs) {
24 23         79 push @dirs, $self->_spec->catpath($volume, $dirs, '');
25             }
26              
27 157 100       606 $self->{dir} = @dirs ? $self->dir_class->new(@dirs) : undef;
28 157         375 $self->{file} = $base;
29              
30 157         599 return $self;
31             }
32              
33 152     152 1 724 sub dir_class { "Path::Class::Dir" }
34              
35             sub as_foreign {
36 18     18 1 2021 my ($self, $type) = @_;
37 18         62 local $Path::Class::Foreign = $self->_spec_class($type);
38 18         101 my $foreign = ref($self)->SUPER::new;
39 18 50       125 $foreign->{dir} = $self->{dir}->as_foreign($type) if defined $self->{dir};
40 18         59 $foreign->{file} = $self->{file};
41 18         80 return $foreign;
42             }
43              
44             sub stringify {
45 300     300 1 8364 my $self = shift;
46 300 100       846 return $self->{file} unless defined $self->{dir};
47 296         850 return $self->_spec->catfile($self->{dir}->stringify, $self->{file});
48             }
49              
50             sub dir {
51 19     19 1 1390 my $self = shift;
52 19 100       120 return $self->{dir} if defined $self->{dir};
53 1         6 return $self->dir_class->new($self->_spec->curdir);
54             }
55 7     7   9260 BEGIN { *parent = \&dir; }
56              
57             sub volume {
58 0     0 1 0 my $self = shift;
59 0 0       0 return '' unless defined $self->{dir};
60 0         0 return $self->{dir}->volume;
61             }
62              
63             sub components {
64 8     8 1 55 my $self = shift;
65 8 50       19 die "Arguments are not currently supported by File->components()" if @_;
66 8         23 return ($self->dir->components, $self->basename);
67             }
68              
69 17     17 1 121 sub basename { shift->{file} }
70 42     42 1 1365 sub open { IO::File->new(@_) }
71              
72 0 0   0 1 0 sub openr { $_[0]->open('r') or croak "Can't read $_[0]: $!" }
73 19 50   19 1 52 sub openw { $_[0]->open('w') or croak "Can't write to $_[0]: $!" }
74 0 0   0 1 0 sub opena { $_[0]->open('a') or croak "Can't append to $_[0]: $!" }
75              
76             sub touch {
77 19     19 1 53 my $self = shift;
78 19 50       53 if (-e $self) {
79 0         0 utime undef, undef, $self;
80             } else {
81 19         58 $self->openw;
82             }
83             }
84              
85             sub slurp {
86 13     13 1 2886 my ($self, %args) = @_;
87 13   100     76 my $iomode = $args{iomode} || 'r';
88 13 50       40 my $fh = $self->open($iomode) or croak "Can't read $self: $!";
89              
90 13 100       133 if (wantarray) {
91 6         102 my @data = <$fh>;
92 6 100 66     35 chomp @data if $args{chomped} or $args{chomp};
93              
94 6 100       14 if ( my $splitter = $args{split} ) {
95 2         5 @data = map { [ split $splitter, $_ ] } @data;
  4         33  
96             }
97              
98 6         86 return @data;
99             }
100              
101              
102 7 50       28 croak "'split' argument can only be used in list context"
103             if $args{split};
104              
105              
106 7 50 33     53 if ($args{chomped} or $args{chomp}) {
107 0         0 chomp( my @data = <$fh> );
108 0         0 return join '', @data;
109             }
110              
111              
112 7         35 local $/;
113 7         329 return <$fh>;
114             }
115              
116             sub spew {
117 6     6 1 25 my $self = shift;
118 6         18 my %args = splice( @_, 0, @_-1 );
119              
120 6   100     34 my $iomode = $args{iomode} || 'w';
121 6 50       17 my $fh = $self->open( $iomode ) or croak "Can't write to $self: $!";
122              
123 6 100       65 if (ref($_[0]) eq 'ARRAY') {
124             # Use old-school for loop to avoid copying.
125 1         3 for (my $i = 0; $i < @{ $_[0] }; $i++) {
  5         10  
126 4         11 print $fh $_[0]->[$i];
127             }
128             }
129             else {
130 5         160 print $fh $_[0];
131             }
132              
133 6         304 return;
134             }
135              
136             sub spew_lines {
137 2     2 1 11 my $self = shift;
138 2         8 my %args = splice( @_, 0, @_-1 );
139              
140 2         3 my $content = $_[0];
141              
142             # If content is an array ref, appends $/ to each element of the array.
143             # Otherwise, if it is a simple scalar, just appends $/ to that scalar.
144              
145 2         7 $content
146             = ref( $content ) eq 'ARRAY'
147 2 100       7 ? [ map { $_, $/ } @$content ]
148             : "$content$/";
149              
150 2         7 return $self->spew( %args, $content );
151             }
152              
153             sub remove {
154 7     7 1 1399 my $file = shift->stringify;
155 7 50       122 return unlink $file unless -e $file; # Sets $! correctly
156 7         720 1 while unlink $file;
157 7         59 return not -e $file;
158             }
159              
160             sub copy_to {
161 2     2 1 30 my ($self, $dest) = @_;
162 2 100       21 if ( UNIVERSAL::isa($dest, Path::Class::File::) ) {
    50          
    0          
163 1         3 $dest = $dest->stringify;
164 1 50       13 die "Can't copy to file $dest: it is a directory" if -d $dest;
165             } elsif ( UNIVERSAL::isa($dest, Path::Class::Dir::) ) {
166 1         10 $dest = $dest->stringify;
167 1 50       23 die "Can't copy to directory $dest: it is a file" if -f $dest;
168 1 50       23 die "Can't copy to directory $dest: no such directory" unless -d $dest;
169             } elsif ( ref $dest ) {
170 0         0 die "Don't know how to copy files to objects of type '".ref($self)."'";
171             }
172              
173 2 50       18 if ( !Perl::OSType::is_os_type('Unix') ) {
174              
175 0 0       0 return unless File::Copy::cp($self->stringify, "${dest}");
176              
177             } else {
178              
179 2 50       46 return unless (system('cp', $self->stringify, "${dest}") == 0);
180              
181             }
182              
183 2         214 return $self->new($dest);
184             }
185              
186             sub move_to {
187 1     1 1 4 my ($self, $dest) = @_;
188 1 50       12 if (File::Copy::move($self->stringify, "${dest}")) {
189              
190 1         216 my $new = $self->new($dest);
191              
192 1         16 $self->{$_} = $new->{$_} foreach (qw/ dir file /);
193              
194 1         10 return $self;
195              
196             } else {
197              
198 0         0 return;
199              
200             }
201             }
202              
203             sub traverse {
204 12     12 1 13 my $self = shift;
205 12         16 my ($callback, @args) = @_;
206 12     12   55 return $self->$callback(sub { () }, @args);
  12         61  
207             }
208              
209             sub traverse_if {
210 0     0 0   my $self = shift;
211 0           my ($callback, $condition, @args) = @_;
212 0     0     return $self->$callback(sub { () }, @args);
  0            
213             }
214              
215             1;
216             __END__