man Path::Class::File () - Objects representing files

NAME

Path::Class::File - Objects representing files

SYNOPSIS

  use Path::Class qw(file);  # Export a short constructor

  my $file = file('foo', 'bar.txt');  # Path::Class::File object
  my $file = Path::Class::File->new('foo', 'bar.txt'); # Same thing

  # Stringifies to 'foo/bar.txt' on Unix, 'foo\bar.txt' on Windows, etc.
  print "file: $file\n";

  if ($file->is_absolute) { ... }

  my $v = $file->volume; # Could be 'C:' on Windows, empty string
                         # on Unix, 'Macintosh HD:' on Mac OS

  $file->cleanup; # Perform logical cleanup of pathname

  my $dir = $file->dir;  # A Path::Class::Dir object

  my $abs = $file->absolute; # Transform to absolute path
  my $rel = $file->relative; # Transform to relative path

DESCRIPTION

The CWPath::Class::File class contains functionality for manipulating file names in a cross-platform way.

METHODS

$file = Path::Class::File->new( <dir1>, <dir2>, ..., <file> )
$file = file( <dir1>, <dir2>, ..., <file> )
Creates a new CWPath::Class::File object and returns it. The arguments specify the path to the file. Any volume may also be specified as the first argument, or as part of the first argument. You can use platform-neutral syntax:
  my $dir = file( 'foo', 'bar', 'baz.txt' );
or platform-native syntax:
  my $dir = dir( 'foo/bar/baz.txt' );
or a mixture of the two:
  my $dir = dir( 'foo/bar', 'baz.txt' );
All three of the above examples create relative paths. To create an absolute path, either use the platform native syntax for doing so:
  my $dir = dir( '/var/tmp/foo.txt' );
or use an empty string as the first argument:
  my $dir = dir( '', 'var', 'tmp', 'foo.txt' );
If the second form seems awkward, that's somewhat intentional - paths like CW/var/tmp or CW\Windows aren't cross-platform concepts in the first place, so they probably shouldn't appear in your code if you're trying to be cross-platform. The first form is perfectly fine, because paths like this may come from config files, user input, or whatever.
$file->stringify
This method is called internally when a CWPath::Class::File object is used in a string context, so the following are equivalent:
  $string = $file->stringify;
  $string = "$file";
$file->volume
Returns the volume (e.g. CWC: on Windows, CWMacintosh HD: on Mac OS, etc.) of the object, if any. Otherwise, returns the empty string.
$file->basename
Returns the name of the file as a string, without the directory portion (if any).
$file->is_dir
Returns a boolean value indicating whether this object represents a directory. Not surprisingly, CWPath::Class::File objects always return false, and CWPath::Class::Dir objects always return true.
$file->is_absolute
Returns true or false depending on whether the file refers to an absolute path specifier (like CW/usr/local/foo.txt or CW\Windows\Foo.txt).
$file->cleanup
Performs a logical cleanup of the file path. For instance:
  my $file = file('/foo//baz/./foo.txt')->cleanup;
  # $file now represents '/foo/baz/foo.txt';
Returns a CWPath::Class::Dir object representing the directory containing this file. A synonym for the CWdir() method. Returns a CWPath::Class::File object representing CW$file as an absolute path. An optional argument, given as either a string or a CWPath::Class::Dir object, specifies the directory to use as the base of relativity - otherwise the current working directory will be used. Returns a CWPath::Class::File object representing CW$file as a relative path. An optional argument, given as either a string or a CWPath::Class::Dir object, specifies the directory to use as the base of relativity - otherwise the current working directory will be used. Returns a CWPath::Class::File object representing CW$file as it would be specified on a system of type CW$type. Known types include CWUnix, CWWin32, CWMac, CWVMS, and CWOS2, i.e. anything for which there is a subclass of CWFile::Spec. Any generated objects (subdirectories, files, parents, etc.) will also retain this type. Returns a CWPath::Class::File object representing a file as it would be specified on a system of type CW$type. Known types include CWUnix, CWWin32, CWMac, CWVMS, and CWOS2, i.e. anything for which there is a subclass of CWFile::Spec. The arguments in CW@args are the same as they would be specified in CWnew(). Passes the given arguments, including CW$file, to CWIO::File->new (which in turn calls CWIO::File->open and returns the result as an CWIO::File object. If the opening fails, CWundef is returned and CW$! is set. A shortcut for
 $fh = $file->open('r') or die "Can't read $file: $!";
A shortcut for
 $fh = $file->open('w') or die "Can't write $file: $!";
$file->slurp()
In a scalar context, returns the contents of CW$file in a string. In a list context, returns the lines of CW$file (according to how CW$/ is set) as a list. If the file can't be read, this method will throw an exception. If you want CWchomp() run on each line of the file, pass a true value for the CWchomp or CWchomped parameters:
  my @lines = $file->slurp(chomp => 1);
$file->remove()
This method will remove the file in a way that works well on all platforms, and returns a boolean value indicating whether or not the file was successfully removed. CWremove() is better than simply calling Perl's CWunlink() function, because on some platforms (notably VMS) you actually may need to call CWunlink() several times before all versions of the file are gone - the CWremove() method handles this process for you. Invokes CWFile::stat::stat() on this file and returns a CWFile::stat object representing the result. Same as CWstat(), but if CW$file is a symbolic link, CWlstat() stats the link instead of the file the link points to.

AUTHOR

Ken Williams, ken@mathforum.org

SEE ALSO

Path::Class, Path::Class::Dir, File::Spec