1 package Path::Class::File;
7 use Path::Class::Entity;
8 use base qw(Path::Class::Entity);
13 my $self = shift->SUPER::new;
17 my ($volume, $dirs, $base) = $self->_spec->splitpath($file);
20 push @dirs, $self->_spec->catpath($volume, $dirs, '');
23 $self->{dir} = @dirs ? Path::Class::Dir->new(@dirs) : undef;
24 $self->{file} = $base;
30 my ($self, $type) = @_;
31 local $Path::Class::Foreign = $self->_spec_class($type);
32 my $foreign = ref($self)->SUPER::new;
33 $foreign->{dir} = $self->{dir}->as_foreign($type) if defined $self->{dir};
34 $foreign->{file} = $self->{file};
40 return $self->{file} unless defined $self->{dir};
41 return $self->_spec->catfile($self->{dir}->stringify, $self->{file});
46 return $self->{dir} if defined $self->{dir};
47 return Path::Class::Dir->new($self->_spec->curdir);
49 BEGIN { *parent = \&dir; }
53 return '' unless defined $self->{dir};
54 return $self->{dir}->volume;
57 sub basename { shift->{file} }
58 sub open { IO::File->new(@_) }
60 sub openr { $_[0]->open('r') or die "Can't read $_[0]: $!" }
61 sub openw { $_[0]->open('w') or die "Can't write $_[0]: $!" }
67 utime $now, $now, $self;
74 my ($self, %args) = @_;
75 my $fh = $self->openr;
77 if ($args{chomped} or $args{chomp}) {
78 chomp( my @data = <$fh> );
79 return wantarray ? @data : join '', @data;
82 local $/ unless wantarray;
87 my $file = shift->stringify;
88 return unlink $file unless -e $file; # Sets $! correctly
98 Path::Class::File - Objects representing files
102 use Path::Class qw(file); # Export a short constructor
104 my $file = file('foo', 'bar.txt'); # Path::Class::File object
105 my $file = Path::Class::File->new('foo', 'bar.txt'); # Same thing
107 # Stringifies to 'foo/bar.txt' on Unix, 'foo\bar.txt' on Windows, etc.
108 print "file: $file\n";
110 if ($file->is_absolute) { ... }
111 if ($file->is_relative) { ... }
113 my $v = $file->volume; # Could be 'C:' on Windows, empty string
114 # on Unix, 'Macintosh HD:' on Mac OS
116 $file->cleanup; # Perform logical cleanup of pathname
117 $file->resolve; # Perform physical cleanup of pathname
119 my $dir = $file->dir; # A Path::Class::Dir object
121 my $abs = $file->absolute; # Transform to absolute path
122 my $rel = $file->relative; # Transform to relative path
126 The C<Path::Class::File> class contains functionality for manipulating
127 file names in a cross-platform way.
133 =item $file = Path::Class::File->new( <dir1>, <dir2>, ..., <file> )
135 =item $file = file( <dir1>, <dir2>, ..., <file> )
137 Creates a new C<Path::Class::File> object and returns it. The
138 arguments specify the path to the file. Any volume may also be
139 specified as the first argument, or as part of the first argument.
140 You can use platform-neutral syntax:
142 my $dir = file( 'foo', 'bar', 'baz.txt' );
144 or platform-native syntax:
146 my $dir = dir( 'foo/bar/baz.txt' );
148 or a mixture of the two:
150 my $dir = dir( 'foo/bar', 'baz.txt' );
152 All three of the above examples create relative paths. To create an
153 absolute path, either use the platform native syntax for doing so:
155 my $dir = dir( '/var/tmp/foo.txt' );
157 or use an empty string as the first argument:
159 my $dir = dir( '', 'var', 'tmp', 'foo.txt' );
161 If the second form seems awkward, that's somewhat intentional - paths
162 like C</var/tmp> or C<\Windows> aren't cross-platform concepts in the
163 first place, so they probably shouldn't appear in your code if you're
164 trying to be cross-platform. The first form is perfectly fine,
165 because paths like this may come from config files, user input, or
168 =item $file->stringify
170 This method is called internally when a C<Path::Class::File> object is
171 used in a string context, so the following are equivalent:
173 $string = $file->stringify;
178 Returns the volume (e.g. C<C:> on Windows, C<Macintosh HD:> on Mac OS,
179 etc.) of the object, if any. Otherwise, returns the empty string.
181 =item $file->basename
183 Returns the name of the file as a string, without the directory
188 Returns a boolean value indicating whether this object represents a
189 directory. Not surprisingly, C<Path::Class::File> objects always
190 return false, and C<Path::Class::Dir> objects always return true.
192 =item $file->is_absolute
194 Returns true or false depending on whether the file refers to an
195 absolute path specifier (like C</usr/local/foo.txt> or C<\Windows\Foo.txt>).
197 =item $file->is_absolute
199 Returns true or false depending on whether the file refers to a
200 relative path specifier (like C<lib/foo.txt> or C<.\Foo.txt>).
204 Performs a logical cleanup of the file path. For instance:
206 my $file = file('/foo//baz/./foo.txt')->cleanup;
207 # $file now represents '/foo/baz/foo.txt';
211 Performs a physical cleanup of the file path. For instance:
213 my $dir = dir('/foo/baz/../foo.txt')->resolve;
214 # $dir now represents '/foo/foo.txt', assuming no symlinks
216 This actually consults the filesystem to verify the validity of the
219 =item $dir = $file->dir
221 Returns a C<Path::Class::Dir> object representing the directory
222 containing this file.
224 =item $dir = $file->parent
226 A synonym for the C<dir()> method.
228 =item $abs = $file->absolute
230 Returns a C<Path::Class::File> object representing C<$file> as an
231 absolute path. An optional argument, given as either a string or a
232 C<Path::Class::Dir> object, specifies the directory to use as the base
233 of relativity - otherwise the current working directory will be used.
235 =item $rel = $file->relative
237 Returns a C<Path::Class::File> object representing C<$file> as a
238 relative path. An optional argument, given as either a string or a
239 C<Path::Class::Dir> object, specifies the directory to use as the base
240 of relativity - otherwise the current working directory will be used.
242 =item $foreign = $file->as_foreign($type)
244 Returns a C<Path::Class::File> object representing C<$file> as it would
245 be specified on a system of type C<$type>. Known types include
246 C<Unix>, C<Win32>, C<Mac>, C<VMS>, and C<OS2>, i.e. anything for which
247 there is a subclass of C<File::Spec>.
249 Any generated objects (subdirectories, files, parents, etc.) will also
252 =item $foreign = Path::Class::File->new_foreign($type, @args)
254 Returns a C<Path::Class::File> object representing a file as it would
255 be specified on a system of type C<$type>. Known types include
256 C<Unix>, C<Win32>, C<Mac>, C<VMS>, and C<OS2>, i.e. anything for which
257 there is a subclass of C<File::Spec>.
259 The arguments in C<@args> are the same as they would be specified in
262 =item $fh = $file->open($mode, $permissions)
264 Passes the given arguments, including C<$file>, to C<< IO::File->new >>
265 (which in turn calls C<< IO::File->open >> and returns the result
266 as an C<IO::File> object. If the opening
267 fails, C<undef> is returned and C<$!> is set.
269 =item $fh = $file->openr()
273 $fh = $file->open('r') or die "Can't read $file: $!";
275 =item $fh = $file->openw()
279 $fh = $file->open('w') or die "Can't write $file: $!";
283 Sets the modification and access time of the given file to right now,
284 if the file exists. If it doesn't exist, C<touch()> will I<make> it
285 exist, and - YES! - set its modification and access time to now.
289 In a scalar context, returns the contents of C<$file> in a string. In
290 a list context, returns the lines of C<$file> (according to how C<$/>
291 is set) as a list. If the file can't be read, this method will throw
294 If you want C<chomp()> run on each line of the file, pass a true value
295 for the C<chomp> or C<chomped> parameters:
297 my @lines = $file->slurp(chomp => 1);
299 =item $file->remove()
301 This method will remove the file in a way that works well on all
302 platforms, and returns a boolean value indicating whether or not the
303 file was successfully removed.
305 C<remove()> is better than simply calling Perl's C<unlink()> function,
306 because on some platforms (notably VMS) you actually may need to call
307 C<unlink()> several times before all versions of the file are gone -
308 the C<remove()> method handles this process for you.
310 =item $st = $file->stat()
312 Invokes C<< File::stat::stat() >> on this file and returns a
313 C<File::stat> object representing the result.
315 =item $st = $file->lstat()
317 Same as C<stat()>, but if C<$file> is a symbolic link, C<lstat()>
318 stats the link instead of the file the link points to.
324 Ken Williams, ken@mathforum.org
328 Path::Class, Path::Class::Dir, File::Spec