/usr/share/perl5/File/Fu/Base.pm is in libfile-fu-perl 0.0.8-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 | package File::Fu::Base;
$VERSION = v0.0.8;
use warnings;
use strict;
use Carp;
use File::stat ();
=head1 NAME
File::Fu::Base - nothing to see here
=head1 SYNOPSIS
=cut
use overload (
'=' => sub {shift->clone(@_)},
'""' => 'stringify',
'%=' => 'append',
'%' => sub {shift->clonedo('append', @_)},
# can't overload s/// or accomplish anything with prototypes
'&' => sub {shift->clonedo('map', @_)},
'&=' => 'map',
cmp => sub {"$_[0]" cmp "$_[1]"},
# invalid methods
'-' => sub {shift->error('-')},
'*' => sub {shift->error('*')},
'~' => sub {~ shift->stringify},
nomethod => sub {shift->error($_[2])},
);
=head2 clone
my $obj = $obj->clone;
=cut
sub clone {
my $self = shift;
my $clone = {%$self};
bless($clone, ref($self));
#carp("clone the ", overload::StrVal($self));
foreach my $item (values(%$clone)) {
my $ref = ref($item) or next;
if($ref eq 'ARRAY') {
#warn "clone [@$item]\n";
$item = [@$item];
}
elsif($ref eq 'HASH') {
$item = {%$item};
}
elsif(eval {$item->can('clone')}) {
$item = $item->clone
}
else {
croak("cannot deref $item");
}
}
#carp("now ", overload::StrVal($clone));
return($clone);
} # end subroutine clone definition
########################################################################
=head2 clonedo
$clone = $self->clonedo($action, @args);
=cut
sub clonedo {
my $self = shift;
my ($action, $arg, $rev) = @_;
#carp("clonedo $action", $rev ? ' backwards' : '');
if($rev) {
return($arg . $self->stringify) if($action eq 'append');
croak("$action is invalid in that order");
}
# perl doesn't know how to stringify
# TODO how can I tell when this is just a quoted string?
#if($action eq 'append' and $arg =~ m/\n/) { return($self->stringify . $arg); }
$self = $self->clone;
$self->$action($arg);
#carp("now ", overload::StrVal($self));
return($self);
} # end subroutine clonedo definition
########################################################################
=head2 error
$package->error($op);
=cut
sub error {
my $self = shift;
my ($op) = @_;
croak("$op is not a valid op for a ", ref($self), " object");
} # end subroutine error definition
########################################################################
=head1 Filetests
=head2 r w x o R W X O e z s f d l p S b c t u g k T B M A C
See perldoc -f -x
=cut
foreach my $test (split(//, 'rwxoRWXOezsfdlpSbctugkTBMAC')) {
my $subref = eval("sub {-$test shift}");
$@ and croak("I broke this -- $@");
no strict 'refs';
*{"$test"} = $subref;
}
=head1 File::Spec stuff
This needs to be redone.
=cut
use File::Spec; # GRR
=head2 is_absolute
=cut
sub is_absolute {
# XXX this is immutable, no?
File::Spec->file_name_is_absolute($_[0]->stringify);
}
=head2 relative
Get a relative name.
my $rel = $abs->relative;
Also, with optional relative-to directory:
my $rel = $abs->relative($to);
=cut
sub relative {
my $self = shift;
my $base = shift;
return $self->new(File::Spec->abs2rel($self->stringify,
defined($base) ? "$base" : ()
));
}
=head2 relative_to
Same as relative(), but requires the $dir argument.
my $rel = $abs->relative_to($dir);
=cut
sub relative_to {
my $self = shift;
my $base = shift or croak('relative_to() requires a $dir');
return $self->relative($base);
}
=head2 resolve
Fully resolve any symlinks;
my $path = $path->resolve;
=cut
sub resolve {
my $self = shift;
while(1) {
return $self unless($self->l);
my $to = $self->readlink;
return $to if($to->is_absolute);
$self = $self->new($self->dirname . $to);
}
} # end subroutine resolve definition
########################################################################
=head2 relative_symlink
Where $path and $linkname are both relative to the current directory.
$path->relative_symlink($linkname);
=cut
sub relative_symlink {
my $self = shift;
my ($link) = @_;
my $rel = $self->relative($self->new($link)->dirname);
return($rel->symlink($link));
} # end subroutine relative_symlink definition
########################################################################
=head2 utime
Update the file timestamps.
$file->utime($atime, $mtime);
Optionally, set both to the same time.
$file->utime($time);
Also see touch().
=cut
sub utime {
my $self = shift;
@_ or croak("not enough arguments to utime()");
my $at = shift;
my $mt = @_ ? shift(@_) : $at;
if($self->is_dir) {
$self = $self->bare;
}
utime($at, $mt, $self) or croak("cannot utime '$self' $!");
} # end subroutine utime definition
########################################################################
=head2 chmod
$path->chmod($mode);
=cut
sub chmod :method {
my $self = shift;
my ($mode) = @_;
chmod($mode, "$self") or croak("cannot chmod '$self' $!");
} # end subroutine chmod definition
########################################################################
=head2 rename
Calls the builtin rename() on the $path and returns a new object with
that name.
$path = $path->rename($newname);
=cut
sub rename :method {
my $self = shift;
my ($name) = @_;
rename($self, $name) or
croak("cannot rename '$self' to '$name' $!");
return($self->new($name));
} # end subroutine rename definition
########################################################################
=head1 Stat Object
The stat() and lstat() methods both return a File::stat object.
=head2 stat
my $st = $obj->stat;
=cut
sub stat {
my $self = shift;
my $st = File::stat::stat("$self") or
croak("cannot stat '$self' $!");
return($st);
} # end subroutine stat definition
########################################################################
=head2 lstat
Same as stat, but does not dereference symlinks.
my $st = $obj->lstat;
=cut
sub lstat {
my $self = shift;
if($self->is_dir and $self->l) {
$self = $self->bare;
}
my $st = File::stat::lstat("$self") or
croak("cannot lstat '$self' $!");
return($st);
} # end subroutine lstat definition
########################################################################
=head2 is_same
Returns true if the two paths are the same. This is by string equality,
then (if both paths exist) by device+inode equality.
$bool = $path->is_same($other);
=cut
sub is_same {
my $self = shift;
my ($other) = @_;
unless(ref $other) {
my $proto = ($self->is_file and $other =~ m#/$#) ?
$self->dir_class : $self;
$other = $proto->new($other);
}
return(1) if($self eq $other);
return(0) if($self->is_dir != $other->is_dir);
my $n = 0;
# TODO just check absolutely?
# this currently probably misses non-existent files where the dirname
# resolves to the same location.
my ($s1, $s2) = map({eval {$_->stat}} $self, $other);
return(0) unless($s1 and $s2);
return(
$s1->dev eq $s2->dev and
$s1->ino eq $s2->ino
);
} # end subroutine is_same definition
########################################################################
=head1 AUTHOR
Eric Wilhelm @ <ewilhelm at cpan dot org>
http://scratchcomputing.com/
=head1 BUGS
If you found this module on CPAN, please report any bugs or feature
requests through the web interface at L<http://rt.cpan.org>. I will be
notified, and then you'll automatically be notified of progress on your
bug as I make changes.
If you pulled this development version from my /svn/, please contact me
directly.
=head1 COPYRIGHT
Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
=head1 NO WARRANTY
Absolutely, positively NO WARRANTY, neither express or implied, is
offered with this software. You use this software at your own risk. In
case of loss, no person or entity owes you anything whatsoever. You
have been warned.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
# vi:ts=2:sw=2:et:sta
1;
|