| 1 | package File::Fu::File; | ||||||||||||||||||||||||||||
| 2 | 13 | $VERSION = v0.0.8; | |||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use IO::File (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 NAME | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | File::Fu::File - a filename object | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | use File::Fu; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | my $file = File::Fu->file("path/to/file"); | ||||||||||||||||||||||||||||
| 19 | $file %= '.extension'; | ||||||||||||||||||||||||||||
| 20 | $file->e and warn "$file exists"; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | $file->l and warn "$file is a link to ", $file->readlink; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 13 | ▶ | use base 'File::Fu::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 13 | ▶ | use Class::Accessor::Classy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 13 | lv 'file'; | |||||||||||||||||||||||||||
| 30 | 13 | ro 'dir'; aka dir => 'dirname', 'parent'; | |||||||||||||||||||||||||||
| 31 | 13 | ▶ | no Class::Accessor::Classy; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | #use overload (); | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | =head1 Constructor | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | =head2 new | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | my $file = File::Fu::File->new($path); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | my $file = File::Fu::File->new(@path); | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =cut | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 122 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 46 | 122 | my $package = shift; | |||||||||||||||||||||||||||
| 47 | 122 | ▶ | my $class = ref($package) || $package; | ||||||||||||||||||||||||||
|
Condition66%2/3
ref $package || $package
MC/DC0%0/2
ref $package || $package
ref $package
$package
Truth table66%2/3
A || B
A= ref $packageB= $package
| |||||||||||||||||||||||||||||
| 48 | 122 | my $self = {$class->_init(@_)}; | |||||||||||||||||||||||||||
| 49 | 122 | bless($self, $class); | |||||||||||||||||||||||||||
| 50 | 122 | return($self); | |||||||||||||||||||||||||||
| 51 | } # end subroutine new definition | ||||||||||||||||||||||||||||
| 52 | ######################################################################## | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | =head2 new_direct | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | my $file = File::Fu::File->new_direct( | ||||||||||||||||||||||||||||
| 57 | dir => $dir_obj, | ||||||||||||||||||||||||||||
| 58 | file => $name | ||||||||||||||||||||||||||||
| 59 | ); | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 196 | ▶ | sub new_direct { | ||||||||||||||||||||||||||
|
Subroutine
new_direct: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 196 | my $package = shift; | |||||||||||||||||||||||||||
| 65 | 196 | ▶ | my $class = ref($package) || $package; | ||||||||||||||||||||||||||
|
Condition33%1/3
ref $package || $package
MC/DC0%0/2
ref $package || $package
ref $package
$package
Truth table33%1/3
A || B
A= ref $packageB= $package
| |||||||||||||||||||||||||||||
| 66 | 196 | my $self = {@_}; | |||||||||||||||||||||||||||
| 67 | 196 | bless($self, $class); | |||||||||||||||||||||||||||
| 68 | 196 | return($self); | |||||||||||||||||||||||||||
| 69 | } # end subroutine new_direct definition | ||||||||||||||||||||||||||||
| 70 | ######################################################################## | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | =head1 Class Constants | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head2 dir_class | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | Return the corresponding dir class for this file object. Default: | ||||||||||||||||||||||||||||
| 77 | L<File::Fu::Dir>. | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | my $dc = $class->dir_class; | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =head2 is_dir | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | Always false for a file. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head2 is_file | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Always true for a file. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 13 | ▶ | use constant dir_class => 'File::Fu::Dir'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 13 | ▶ | use constant is_dir => 0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 13 | ▶ | use constant is_file => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | ######################################################################## | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =for internal head2 _init | ||||||||||||||||||||||||||||
| 98 | my %fields = $class->_init(@_); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 122 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 103 | 122 | my $class = shift; | |||||||||||||||||||||||||||
| 104 | 122 | ▶ | my @dirs = @_ or croak("file must have a name"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 122 | my $file = pop(@dirs); | |||||||||||||||||||||||||||
| 106 | 122 | ▶ | if($file =~ m#/#) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 33 | ▶ | croak("strange mix: ", join(',', @_, $file)) if(@dirs); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 33 | my %p = $class->dir_class->_init($file); | |||||||||||||||||||||||||||
| 109 | 33 | @dirs = @{$p{dirs}}; | |||||||||||||||||||||||||||
| 110 | 33 | $file = pop(@dirs); | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 122 | return(dir => $class->dir_class->new(@dirs), file => $file); | |||||||||||||||||||||||||||
| 114 | } # end subroutine _init definition | ||||||||||||||||||||||||||||
| 115 | ######################################################################## | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | =head1 Parts | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =head2 basename | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | Returns a new object representing only the file part of the name. | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | my $obj = $file->basename; | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =cut | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 84 | ▶ | sub basename { | ||||||||||||||||||||||||||
|
Subroutine
basename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 128 | 84 | my $self = shift; | |||||||||||||||||||||||||||
| 129 | 84 | $self->new($self->file); | |||||||||||||||||||||||||||
| 130 | } # end subroutine basename definition | ||||||||||||||||||||||||||||
| 131 | ######################################################################## | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head1 Methods | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 stringify | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | my $string = $file->stringify; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =cut | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | 502 | ▶ | sub stringify { | ||||||||||||||||||||||||||
|
Subroutine
stringify: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 502 | my $self = shift; | |||||||||||||||||||||||||||
| 143 | 502 | my $dir = $self->dir; | |||||||||||||||||||||||||||
| 144 | #warn "stringify(..., $_[1], $_[2])"; | ||||||||||||||||||||||||||||
| 145 | #Carp::carp("stringify ", overload::StrVal($self), " ($self->{file})"); | ||||||||||||||||||||||||||||
| 146 | 502 | ▶ | $dir = $dir->is_cwd ? '' : $dir->stringify; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 502 | return($dir . $self->file); | |||||||||||||||||||||||||||
| 148 | } # end subroutine stringify definition | ||||||||||||||||||||||||||||
| 149 | ######################################################################## | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | =head2 append | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | Append a string only to the filename part. | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | $file->append('.gz'); | ||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | $file %= '.gz'; | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | (Yeah... I tried to use .=, but overloading hates me.) | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | 3 | ▶ | sub append { | ||||||||||||||||||||||||||
|
Subroutine
append: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 164 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 165 | 3 | my ($tail) = @_; | |||||||||||||||||||||||||||
| 166 | 3 | $self->file .= $tail; | |||||||||||||||||||||||||||
| 167 | 3 | $self; | |||||||||||||||||||||||||||
| 168 | } # end subroutine append definition | ||||||||||||||||||||||||||||
| 169 | ######################################################################## | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =head2 map | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | $file->map(sub {...}); | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | $file &= sub {...}; | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | =cut | ||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 2 | ▶ | sub map :method { | ||||||||||||||||||||||||||
|
Subroutine
map: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 181 | 2 | my ($sub) = shift; | |||||||||||||||||||||||||||
| 182 | 2 | local $_ = $self->file; | |||||||||||||||||||||||||||
| 183 | 2 | $sub->(); | |||||||||||||||||||||||||||
| 184 | 2 | $self->file = $_; | |||||||||||||||||||||||||||
| 185 | 2 | $self; | |||||||||||||||||||||||||||
| 186 | } # end subroutine map definition | ||||||||||||||||||||||||||||
| 187 | ######################################################################## | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 absolute | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | Get an absolute name (without checking the filesystem.) | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | my $abs = $file->absolute; | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =cut | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 1 | ▶ | sub absolute { | ||||||||||||||||||||||||||
|
Subroutine
absolute: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 1 | my ($self) = shift; | |||||||||||||||||||||||||||
| 199 | 1 | return($self->dir->absolute->file($self->file)); | |||||||||||||||||||||||||||
| 200 | } # end subroutine absolutely definition | ||||||||||||||||||||||||||||
| 201 | ######################################################################## | ||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | =head2 absolutely | ||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | Get an absolute name (resolved on the filesytem.) | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | my $abs = $file->absolutely; | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | =cut | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | 13 | ▶ | sub absolutely { | ||||||||||||||||||||||||||
|
Subroutine
absolutely: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 13 | my $self = shift; | |||||||||||||||||||||||||||
| 213 | 13 | return($self->dir->absolutely->file($self->file)); | |||||||||||||||||||||||||||
| 214 | } # end subroutine absolutely definition | ||||||||||||||||||||||||||||
| 215 | ######################################################################## | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =head1 Doing stuff | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head2 open | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Open the file with $mode ('<', 'r', '>', 'w', etc) -- see L<IO::File>. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | my $fh = $file->open($mode, $permissions); | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Throws an error if anything goes wrong or if the resulting filehandle | ||||||||||||||||||||||||||||
| 226 | happens to be a directory. | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =cut | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | # TODO should probably have our own filehandle so we can close in the | ||||||||||||||||||||||||||||
| 231 | # destructor and croak there too? | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 75 | ▶ | sub open :method { | ||||||||||||||||||||||||||
|
Subroutine
open: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 234 | 75 | my $self = shift; | |||||||||||||||||||||||||||
| 235 | 75 | ▶ | my $fh = IO::File->new($self, @_) or croak("cannot open '$self' $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 236 | 74 | ▶ | -d $fh and croak("$self is a directory"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 74 | return($fh); | |||||||||||||||||||||||||||
| 238 | } # end subroutine open definition | ||||||||||||||||||||||||||||
| 239 | ######################################################################## | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head2 sysopen | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Interface to the sysopen() builtin. The value of $mode is a text string | ||||||||||||||||||||||||||||
| 245 | joined by '|' characters which must be valid O_* constants from Fcntl. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | my $fh = $file->sysopen($mode, $perms); | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | =cut | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | 0 | ▶ | sub sysopen :method { | ||||||||||||||||||||||||||
|
Subroutine
sysopen: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 252 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 253 | 0 | my ($mode, $perms) = @_; | |||||||||||||||||||||||||||
| 254 | 0 | my $m = 0; | |||||||||||||||||||||||||||
| 255 | 0 | foreach my $w (split /\|/, $mode) { | |||||||||||||||||||||||||||
| 256 | 0 | my $word = 'O_' . uc($w); | |||||||||||||||||||||||||||
| 257 | 0 | ▶ | my $x = Fcntl->can($word) or croak("'$word' not found in Fcntl"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 258 | 0 | $m |= $x->(); | |||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | 0 | my $fh = IO::Handle->new; | |||||||||||||||||||||||||||
| 262 | 0 | ▶ | sysopen($fh, "$self", $m, $perms || 0666) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$perms || 438
MC/DC0%0/1
$perms || 438
$perms
Truth table0%0/2
A
A= $perms
| |||||||||||||||||||||||||||||
| 263 | or croak("error on sysopen '$self' - $!"); | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | return($fh); | |||||||||||||||||||||||||||
| 266 | } # sysopen ############################################################ | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | =head2 piped_open | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | Opens a read pipe. The file is appended to @command. | ||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||
| 272 | my $fh = $file->piped_open(@command); | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | Example: useless use of cat. | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | my $fh = $file->piped_open('cat'); | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | This interface is deprecated (maybe) because it is limited to commands | ||||||||||||||||||||||||||||
| 279 | which take the $file as the last argument. See run() for the way of the | ||||||||||||||||||||||||||||
| 280 | future. | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | =cut | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 0 | ▶ | sub piped_open { | ||||||||||||||||||||||||||
|
Subroutine
piped_open: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 285 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 286 | 0 | my (@command) = @_; | |||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # TODO some way to decide where self goes in @command | ||||||||||||||||||||||||||||
| 289 | 0 | push(@command, $self); | |||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | # TODO closing STDIN and such before the fork? | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | # TODO here is where we need our own filehandle object again | ||||||||||||||||||||||||||||
| 294 | 0 | ▶ | my $pid = open(my $fh, '-|', @command) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | croak("cannot exec '@command' $!"); | ||||||||||||||||||||||||||||
| 296 | 0 | return($fh); | |||||||||||||||||||||||||||
| 297 | } # end subroutine piped_open definition | ||||||||||||||||||||||||||||
| 298 | ######################################################################## | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head2 run | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Treat C<$file> as a program and execute a pipe open. | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | my $fh = $file->run(@args); | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | If called in void context, runs C<system()> with autodie semantics and | ||||||||||||||||||||||||||||
| 307 | multi-arg form (suppresses shell interpolation.) | ||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||
| 309 | $file->run(@args); | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | No special treatment is made for whether $file is relative or not (the | ||||||||||||||||||||||||||||
| 312 | underlying C<system()>/C<exec()> will search your path.) Use | ||||||||||||||||||||||||||||
| 313 | File::Fu->which() to get an absolute path beforehand. | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | File::Fu->which('ls')->run('-l'); | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | =cut | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 0 | ▶ | sub run { | ||||||||||||||||||||||||||
|
Subroutine
run: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 320 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 321 | 0 | my (@args) = @_; | |||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 0 | ▶ | if(defined wantarray) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | # TODO use IPC::Run | ||||||||||||||||||||||||||||
| 325 | 0 | my $fh = IO::Handle->new; | |||||||||||||||||||||||||||
| 326 | 0 | my @command = ($self, @args); | |||||||||||||||||||||||||||
| 327 | 0 | ▶ | my $pid = open($fh, '-|', @command) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 328 | croak("cannot exec '@command' $!"); | ||||||||||||||||||||||||||||
| 329 | 0 | return($fh); | |||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||
| 331 | else { | ||||||||||||||||||||||||||||
| 332 | 0 | my $ret = system {$self} $self, @args; | |||||||||||||||||||||||||||
| 333 | 0 | ▶ | croak("error executing '$self'", $ret < 0 ? " $!" : '') if($ret); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | } # run ################################################################ | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | =head2 touch | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | Update the timestamp of a file (or create it.) | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | $file->touch; | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | =cut | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 68 | ▶ | sub touch { | ||||||||||||||||||||||||||
|
Subroutine
touch: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 346 | 68 | my $self = shift; | |||||||||||||||||||||||||||
| 347 | 68 | ▶ | if(-e $self) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | $self->utime(time); | |||||||||||||||||||||||||||
| 349 | } | ||||||||||||||||||||||||||||
| 350 | else { | ||||||||||||||||||||||||||||
| 351 | 68 | $self->open('>'); | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | 67 | return($self); | |||||||||||||||||||||||||||
| 354 | } # end subroutine touch definition | ||||||||||||||||||||||||||||
| 355 | ######################################################################## | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =head2 mkfifo | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | my $file = $file->mkfifo($mode); | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | =cut | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 0 | ▶ | sub mkfifo :method { | ||||||||||||||||||||||||||
|
Subroutine
mkfifo: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 364 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 365 | 0 | my ($mode) = @_; | |||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | 0 | ▶ | $mode ||= 0700; | ||||||||||||||||||||||||||
|
Condition0%0/2
$mode ||= 448
MC/DC0%0/1
$mode ||= 448
$mode
Truth table0%0/2
A
A= $mode
| |||||||||||||||||||||||||||||
| 368 | 0 | require POSIX; | |||||||||||||||||||||||||||
| 369 | 0 | ▶ | POSIX::mkfifo("$self", $mode) or croak("mkfifo '$self' failed $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 0 | return $self; | |||||||||||||||||||||||||||
| 372 | } # mkfifo ############################################################# | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head2 link | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | my $link = $file->link($name); | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =cut | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 1 | ▶ | sub link :method { | ||||||||||||||||||||||||||
|
Subroutine
link: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 381 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 382 | 1 | my ($name) = @_; | |||||||||||||||||||||||||||
| 383 | 1 | ▶ | link($self, $name) or croak("link '$self' to '$name' failed $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 1 | return($self->new($name)); | |||||||||||||||||||||||||||
| 385 | } # end subroutine link definition | ||||||||||||||||||||||||||||
| 386 | ######################################################################## | ||||||||||||||||||||||||||||
| 387 | |||||||||||||||||||||||||||||
| 388 | =head2 symlink | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | my $link = $file->symlink($linkname); | ||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||
| 392 | Note that symlinks are relative to where they live. | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | my $dir = File::Fu->dir("foo"); | ||||||||||||||||||||||||||||
| 395 | my $file = $dir+'file'; | ||||||||||||||||||||||||||||
| 396 | # $file->symlink($dir+'link'); is a broken link | ||||||||||||||||||||||||||||
| 397 | my $link = $file->basename->symlink($dir+'link'); | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =head2 relative_symlink | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | See L<File::Fu::Base/relative_symlink>. | ||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | =cut | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 4 | ▶ | sub symlink :method { | ||||||||||||||||||||||||||
|
Subroutine
symlink: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 406 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 407 | 4 | my ($name) = @_; | |||||||||||||||||||||||||||
| 408 | 4 | ▶ | symlink($self, $name) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | croak("symlink '$self' to '$name' failed $!"); | ||||||||||||||||||||||||||||
| 410 | 4 | return($self->new($name)); | |||||||||||||||||||||||||||
| 411 | } # end subroutine symlink definition | ||||||||||||||||||||||||||||
| 412 | ######################################################################## | ||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | # TODO | ||||||||||||||||||||||||||||
| 415 | # my $link = $file->dwimlink(absolute|relative|samedir => $linkname); | ||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | =head2 unlink | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | $file->unlink; | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =cut | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 15 | ▶ | sub unlink :method { | ||||||||||||||||||||||||||
|
Subroutine
unlink: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||||||||||
| 424 | 15 | my $self = shift; | |||||||||||||||||||||||||||
| 425 | 15 | ▶ | unlink("$self") or croak("unlink '$self' failed $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 426 | } # end subroutine unlink definition | ||||||||||||||||||||||||||||
| 427 | ######################################################################## | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | =head2 remove | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | A forced unlink (chmod the file if it is not writable.) | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | $file->remove; | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | =cut | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 0 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 438 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | 0 | ▶ | $self->chmod(0200) unless($self->w); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 441 | 0 | $self->unlink; | |||||||||||||||||||||||||||
| 442 | } # remove ############################################################# | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | =head2 readlink | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | my $to = $file->readlink; | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | =cut | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | 4 | ▶ | sub readlink :method { | ||||||||||||||||||||||||||
|
Subroutine
readlink: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 451 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 452 | 4 | my $name = readlink($self); | |||||||||||||||||||||||||||
| 453 | 4 | ▶ | defined($name) or croak("cannot readlink '$self' $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | 2 | return($self->new($name)); | |||||||||||||||||||||||||||
| 455 | } # end subroutine readlink definition | ||||||||||||||||||||||||||||
| 456 | ######################################################################## | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | ######################################################################## | ||||||||||||||||||||||||||||
| 459 | { # a closure for this variable | ||||||||||||||||||||||||||||
| 460 | 13 | my $has_slurp; | |||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | =head2 read | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | Read the entire file into memory (or swap!) | ||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | my @lines = $file->read; | ||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | my $file = $file->read; | ||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | If File::Slurp is available, options to read_file will be passed along. | ||||||||||||||||||||||||||||
| 471 | See L<File::Slurp/read_file>. | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =cut | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | 3 | ▶ | sub read :method { | ||||||||||||||||||||||||||
|
Subroutine
read: called CC=6 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 476 | 3 | my $self = shift; | |||||||||||||||||||||||||||
| 477 | 3 | my @args = @_; | |||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | 0 | ▶ | $has_slurp ||= eval {require File::Slurp; 1} || -1; | ||||||||||||||||||||||||||
|
Condition60%3/5
$has_slurp ||= eval {
do {
require File::Slurp;
1
}
} || -1
eval {
do {
require File::Slurp;
1
}
} || -1
MC/DC0%0/2
$has_slurp ||= eval {
do {
require File::Slurp;
1
}
} || -1
$has_slurp
eval {
do {
require File::Slurp;
1
}
}
Truth table33%1/3
A ||= B
A= $has_slurpB= eval {
do {
require File::Slurp;
1
}
}
| |||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||
| 481 | 3 | ▶ | if($has_slurp > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 482 | 0 | local $Carp::CarpLevel = 1; | |||||||||||||||||||||||||||
| 483 | 0 | return(File::Slurp::read_file("$self", @args, err_mode => 'croak')); | |||||||||||||||||||||||||||
| 484 | } | ||||||||||||||||||||||||||||
| 485 | else { | ||||||||||||||||||||||||||||
| 486 | 3 | ▶ | croak("must have File::Slurp for fancy reads") if(@args); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||
| 488 | 3 | my $fh = $self->open; | |||||||||||||||||||||||||||
| 489 | 3 | ▶ | local $/ = wantarray ? $/ : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 490 | 3 | return(<$fh>); | |||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||
| 492 | } # end subroutine read definition | ||||||||||||||||||||||||||||
| 493 | ######################################################################## | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =head2 write | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | Write the file's contents. Returns the $file object for chaining. | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | $file = $file->write($content); | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | If File::Slurp is available, $content may be either a scalar, scalar | ||||||||||||||||||||||||||||
| 502 | ref, or array ref. | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | $file->write($content, %args); | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =cut | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | 0 | ▶ | sub write { | ||||||||||||||||||||||||||
|
Subroutine
write: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 509 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 510 | 0 | my ($content, @args) = @_; | |||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 0 | ▶ | $has_slurp ||= eval {require File::Slurp; 1} || -1; | ||||||||||||||||||||||||||
|
Condition0%0/5
$has_slurp ||= eval {
do {
require File::Slurp;
1
}
} || -1
eval {
do {
require File::Slurp;
1
}
} || -1
MC/DC0%0/2
$has_slurp ||= eval {
do {
require File::Slurp;
1
}
} || -1
$has_slurp
eval {
do {
require File::Slurp;
1
}
}
Truth table0%0/3
A ||= B
A= $has_slurpB= eval {
do {
require File::Slurp;
1
}
}
| |||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | 0 | ▶ | if($has_slurp > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 515 | 0 | local $Carp::CarpLevel = 1; | |||||||||||||||||||||||||||
| 516 | 0 | File::Slurp::write_file("$self", | |||||||||||||||||||||||||||
| 517 | {@args, err_mode => 'croak'}, | ||||||||||||||||||||||||||||
| 518 | $content | ||||||||||||||||||||||||||||
| 519 | ); | ||||||||||||||||||||||||||||
| 520 | } | ||||||||||||||||||||||||||||
| 521 | else { | ||||||||||||||||||||||||||||
| 522 | 0 | ▶ | croak("must have File::Slurp for fancy writes") | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@args or ref $content
MC/DC0%0/2
@args or ref $content
@args
ref $content
Truth table0%0/3
A or B
A= @argsB= ref $content
| |||||||||||||||||||||||||||||
| 523 | if(@args or ref($content)); | ||||||||||||||||||||||||||||
| 524 | 0 | my $fh = $self->open('>'); | |||||||||||||||||||||||||||
| 525 | 0 | print $fh $content; | |||||||||||||||||||||||||||
| 526 | 0 | ▶ | close($fh) or croak("write '$self' failed: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||
| 528 | |||||||||||||||||||||||||||||
| 529 | 0 | return $self; | |||||||||||||||||||||||||||
| 530 | } # end subroutine write definition | ||||||||||||||||||||||||||||
| 531 | ######################################################################## | ||||||||||||||||||||||||||||
| 532 | } # File::Slurp closure | ||||||||||||||||||||||||||||
| 533 | ######################################################################## | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | =head2 copy | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | Copies $file to $dest (which can be a file or directory) and returns the | ||||||||||||||||||||||||||||
| 538 | name of the new file as an object. | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | my $new = $file->copy($dest); | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | Note that if $dest is already a File object, that existing object will | ||||||||||||||||||||||||||||
| 543 | be returned. | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | =cut | ||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||
| 547 | 13 | ▶ | sub copy { | ||||||||||||||||||||||||||
|
Subroutine
copy: not called CC=11 SCAR=48.1
Pod
documented
| |||||||||||||||||||||||||||||
| 548 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 549 | 0 | my ($dest) = shift; | |||||||||||||||||||||||||||
| 550 | 0 | my (%opts) = @_; | |||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | # decide if this is file-to-dir or file-to-file | ||||||||||||||||||||||||||||
| 553 | 0 | ▶ | if(-d $dest) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | 0 | $dest = $self->dir_class->new($dest)->file($self->basename); | |||||||||||||||||||||||||||
| 555 | } | ||||||||||||||||||||||||||||
| 556 | else { | ||||||||||||||||||||||||||||
| 557 | 0 | ▶ | $dest = $self->new($dest) unless(ref($dest)); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 558 | } | ||||||||||||||||||||||||||||
| 559 | 0 | ▶ | if($dest->e) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 560 | 0 | ▶ | croak("'$dest' and '$self' are the same file") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 561 | if($self->is_same($dest)); | ||||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | # TODO here's another good reason to have our own filehandle object: | ||||||||||||||||||||||||||||
| 565 | # This fh-copy should be in there. | ||||||||||||||||||||||||||||
| 566 | 0 | my $ifh = $self->open; | |||||||||||||||||||||||||||
| 567 | 0 | my $ofh = $dest->open('>'); | |||||||||||||||||||||||||||
| 568 | 0 | binmode($_) for($ifh, $ofh); | |||||||||||||||||||||||||||
| 569 | 0 | while(1) { | |||||||||||||||||||||||||||
| 570 | 0 | my $buf; | |||||||||||||||||||||||||||
| 571 | 0 | ▶ | defined(my $r = sysread($ifh, $buf, 1024)) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 572 | croak("sysread failed $!"); | ||||||||||||||||||||||||||||
| 573 | 0 | ▶ | $r or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | # why did File::Copy::copy do it like this? | ||||||||||||||||||||||||||||
| 575 | 0 | ▶ | for(my $t = my $w = 0; $w < $r; $w += $t) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 576 | 0 | ▶ | $t = syswrite($ofh, $buf, $r - $w, $w) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 577 | croak("syswrite failed $!"); | ||||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||
| 580 | 0 | ▶ | close($ofh) or croak("write '$dest' failed: $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 581 | # TODO some form of rollback? | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | # TODO handle opts | ||||||||||||||||||||||||||||
| 584 | #if($opts{preserve}) { | ||||||||||||||||||||||||||||
| 585 | # # TODO chmod/chown and such | ||||||||||||||||||||||||||||
| 586 | # $dest->utime($self->stat->mtime); | ||||||||||||||||||||||||||||
| 587 | #} | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 0 | return($dest); | |||||||||||||||||||||||||||
| 590 | } # copy ############################################################### | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | =head2 move | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | my $new = $file->move($dest); | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | =cut | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | 0 | ▶ | sub move { | ||||||||||||||||||||||||||
|
Subroutine
move: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 599 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 600 | 0 | my $new = $self->copy(@_); # TODO can use rename? | |||||||||||||||||||||||||||
| 601 | 0 | $self->unlink; | |||||||||||||||||||||||||||
| 602 | 0 | return($new); | |||||||||||||||||||||||||||
| 603 | } # move ############################################################### | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | ######################################################################## | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | Eric Wilhelm @ <ewilhelm at cpan dot org> | ||||||||||||||||||||||||||||
| 610 | |||||||||||||||||||||||||||||
| 611 | http://scratchcomputing.com/ | ||||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | =head1 BUGS | ||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | If you found this module on CPAN, please report any bugs or feature | ||||||||||||||||||||||||||||
| 616 | requests through the web interface at L<http://rt.cpan.org>. I will be | ||||||||||||||||||||||||||||
| 617 | notified, and then you'll automatically be notified of progress on your | ||||||||||||||||||||||||||||
| 618 | bug as I make changes. | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | If you pulled this development version from my /svn/, please contact me | ||||||||||||||||||||||||||||
| 621 | directly. | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | =head1 COPYRIGHT | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved. | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | =head1 NO WARRANTY | ||||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | Absolutely, positively NO WARRANTY, neither express or implied, is | ||||||||||||||||||||||||||||
| 630 | offered with this software. You use this software at your own risk. In | ||||||||||||||||||||||||||||
| 631 | case of loss, no person or entity owes you anything whatsoever. You | ||||||||||||||||||||||||||||
| 632 | have been warned. | ||||||||||||||||||||||||||||
| 633 | |||||||||||||||||||||||||||||
| 634 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 637 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | =cut | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | 13 | require File::Fu; | |||||||||||||||||||||||||||
| 642 | # vi:ts=2:sw=2:et:sta | ||||||||||||||||||||||||||||
| 643 | 13 | 1; | |||||||||||||||||||||||||||