NAME
Path::Class::Dir - Objects representing directories
SYNOPSIS
use Path::Class qw(dir); # Export a short constructor
my $dir = dir('foo', 'bar'); # Path::Class::Dir object
my $dir = Path::Class::Dir->new('foo', 'bar'); # Same thing
# Stringifies to 'foo/bar' on Unix, 'foo\bar' on Windows, etc.
print "dir: $dir\n";
if ($dir->is_absolute) { ... }
if ($dir->is_relative) { ... }
my $v = $dir->volume; # Could be 'C:' on Windows, empty string
# on Unix, 'Macintosh HD:' on Mac OS
$dir->cleanup; # Perform logical cleanup of pathname
$dir->resolve; # Perform physical cleanup of pathname
my $file = $dir->file('file.txt'); # A file in this directory
my $subdir = $dir->subdir('george'); # A subdirectory
my $parent = $dir->parent; # The parent directory, 'foo'
my $abs = $dir->absolute; # Transform to absolute path
my $rel = $abs->relative; # Transform to relative path
my $rel = $abs->relative('/foo'); # Relative to /foo
print $dir->as_foreign('Mac'); # :foo:bar:
print $dir->as_foreign('Win32'); # foo\bar
# Iterate with IO::Dir methods:
my $handle = $dir->open;
while (my $file = $handle->read) {
$file = $dir->file($file); # Turn into Path::Class::File object
...
}
# Iterate with Path::Class methods:
while (my $file = $dir->next) {
# $file is a Path::Class::File or Path::Class::Dir object
...
}
DESCRIPTION
The Path::Class::Dir class contains functionality for manipulating directory names in a cross-platform way.
METHODS
- $dir = Path::Class::Dir->new( <dir1>, <dir2>, ... )
- $dir = dir( <dir1>, <dir2>, ... )
-
Creates a new
Path::Class::Dirobject and returns it. The arguments specify names of directories which will be joined to create a single directory object. A volume may also be specified as the first argument, or as part of the first argument. You can use platform-neutral syntax:my $dir = dir( 'foo', 'bar', 'baz' );or platform-native syntax:
my $dir = dir( 'foo/bar/baz' );or a mixture of the two:
my $dir = dir( 'foo/bar', 'baz' );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' );or use an empty string as the first argument:
my $dir = dir( '', 'var', 'tmp' );If the second form seems awkward, that's somewhat intentional - paths like
/var/tmpor\Windowsaren't cross-platform concepts in the first place (many non-Unix platforms don't have a notion of a "root directory"), so they probably shouldn't appear in your code if you're trying to be cross-platform. The first form is perfectly natural, because paths like this may come from config files, user input, or whatever.As a special case, since it doesn't otherwise mean anything useful and it's convenient to define this way,
Path::Class::Dir->new()(ordir()) refers to the current directory (File::Spec->curdir). To get the current directory as an absolute path, dodir()->absolute.Finally, as another special case
dir(undef)will return undef, since that's usually an accident on the part of the caller, and returning the root directory would be a nasty surprise just asking for trouble a few lines later. - $dir->stringify
-
This method is called internally when a
Path::Class::Dirobject is used in a string context, so the following are equivalent:$string = $dir->stringify; $string = "$dir"; - $dir->volume
-
Returns the volume (e.g.
C:on Windows,Macintosh HD:on Mac OS, etc.) of the directory object, if any. Otherwise, returns the empty string. - $dir->is_dir
-
Returns a boolean value indicating whether this object represents a directory. Not surprisingly,
Path::Class::Fileobjects always return false, andPath::Class::Dirobjects always return true. - $dir->is_absolute
-
Returns true or false depending on whether the directory refers to an absolute path specifier (like
/usr/localor\Windows). - $dir->is_relative
-
Returns true or false depending on whether the directory refers to a relative path specifier (like
lib/fooor./dir). - $dir->cleanup
-
Performs a logical cleanup of the file path. For instance:
my $dir = dir('/foo//baz/./foo')->cleanup; # $dir now represents '/foo/baz/foo'; - $dir->resolve
-
Performs a physical cleanup of the file path. For instance:
my $dir = dir('/foo//baz/../foo')->resolve; # $dir now represents '/foo/foo', assuming no symlinksThis actually consults the filesystem to verify the validity of the path.
- $file = $dir->file( <dir1>, <dir2>, ..., <file> )
-
Returns a
Path::Class::Fileobject representing an entry in$diror one of its subdirectories. Internally, this just callsPath::Class::File->new( @_ ). - $subdir = $dir->subdir( <dir1>, <dir2>, ... )
-
Returns a new
Path::Class::Dirobject representing a subdirectory of$dir. - $parent = $dir->parent
-
Returns the parent directory of
$dir. Note that this is the logical parent, not necessarily the physical parent. It really means we just chop off entries from the end of the directory list until we cain't chop no more. If the directory is relative, we start using the relative forms of parent directories.The following code demonstrates the behavior on absolute and relative directories:
$dir = dir('/foo/bar'); for (1..6) { print "Absolute: $dir\n"; $dir = $dir->parent; } $dir = dir('foo/bar'); for (1..6) { print "Relative: $dir\n"; $dir = $dir->parent; } ########### Output on Unix ################ Absolute: /foo/bar Absolute: /foo Absolute: / Absolute: / Absolute: / Absolute: / Relative: foo/bar Relative: foo Relative: . Relative: .. Relative: ../.. Relative: ../../.. - @list = $dir->children
-
Returns a list of
Path::Class::Fileand/orPath::Class::Dirobjects listed in this directory, or in scalar context the number of such objects. Obviously, it is necessary for$dirto exist and be readable in order to find its children.Note that the children are returned as subdirectories of
$dir, i.e. the children of foo will be foo/bar and foo/baz, not bar and baz.Ordinarily
children()will not include the self and parent entries.and..(or their equivalents on non-Unix systems), because that's like I'm-my-own-grandpa business. If you do want all directory entries including these special ones, pass a true value for theallparameter:@c = $dir->children(); # Just the children @c = $dir->children(all => 1); # All entriesIn addition, there's a
no_hiddenparameter that will exclude all normally "hidden" entries - on Unix this means excluding all entries that begin with a dot (.):@c = $dir->children(no_hidden => 1); # Just normally-visible entries - $abs = $dir->absolute
-
Returns a
Path::Class::Dirobject representing$diras an absolute path. An optional argument, given as either a string or aPath::Class::Dirobject, specifies the directory to use as the base of relativity - otherwise the current working directory will be used. - $rel = $dir->relative
-
Returns a
Path::Class::Dirobject representing$diras a relative path. An optional argument, given as either a string or aPath::Class::Dirobject, specifies the directory to use as the base of relativity - otherwise the current working directory will be used. - $boolean = $dir->subsumes($other)
-
Returns true if this directory spec subsumes the other spec, and false otherwise. Think of "subsumes" as "contains", but we only look at the specs, not whether
$diractually contains$otheron the filesystem.The
$otherargument may be aPath::Class::Dirobject, aPath::Class::Fileobject, or a string. In the latter case, we assume it's a directory.# Examples: dir('foo/bar' )->subsumes(dir('foo/bar/baz')) # True dir('/foo/bar')->subsumes(dir('/foo/bar/baz')) # True dir('foo/bar' )->subsumes(dir('bar/baz')) # False dir('/foo/bar')->subsumes(dir('foo/bar')) # False - $boolean = $dir->contains($other)
-
Returns true if this directory actually contains
$otheron the filesystem.$otherdoesn't have to be a direct child of$dir, it just has to be subsumed. - $foreign = $dir->as_foreign($type)
-
Returns a
Path::Class::Dirobject representing$diras it would be specified on a system of type$type. Known types includeUnix,Win32,Mac,VMS, andOS2, i.e. anything for which there is a subclass ofFile::Spec.Any generated objects (subdirectories, files, parents, etc.) will also retain this type.
- $foreign = Path::Class::Dir->new_foreign($type, @args)
-
Returns a
Path::Class::Dirobject representing$diras it would be specified on a system of type$type. Known types includeUnix,Win32,Mac,VMS, andOS2, i.e. anything for which there is a subclass ofFile::Spec.The arguments in
@argsare the same as they would be specified innew(). - @list = $dir->dir_list([OFFSET, [LENGTH]])
-
Returns the list of strings internally representing this directory structure. Each successive member of the list is understood to be an entry in its predecessor's directory list. By contract,
Path::Class->new( $dir->dir_list )should be equivalent to$dir.The semantics of this method are similar to Perl's
spliceorsubstrfunctions; they returnLENGTHelements starting atOFFSET. IfLENGTHis omitted, returns all the elements starting atOFFSETup to the end of the list. IfLENGTHis negative, returns the elements fromOFFSETonward except for-LENGTHelements at the end. IfOFFSETis negative, it counts backwardOFFSETelements from the end of the list. IfOFFSETandLENGTHare both omitted, the entire list is returned.In a scalar context,
dir_list()with no arguments returns the number of entries in the directory list;dir_list(OFFSET)returns the single element at that offset;dir_list(OFFSET, LENGTH)returns the final element that would have been returned in a list context. - $fh = $dir->open()
-
Passes
$dirtoIO::Dir->openand returns the result as anIO::Dirobject. If the opening fails,undefis returned and$!is set. - $dir->mkpath($verbose, $mode)
-
Passes all arguments, including
$dir, toFile::Path::mkpath()and returns the result (a list of all directories created). - $dir->rmtree($verbose, $cautious)
-
Passes all arguments, including
$dir, toFile::Path::rmtree()and returns the result (the number of files successfully deleted). - $dir->remove()
-
Removes the directory, which must be empty. Returns a boolean value indicating whether or not the directory was successfully removed. This method is mainly provided for consistency with
Path::Class::File'sremove()method. - $dir_or_file = $dir->next()
-
A convenient way to iterate through directory contents. The first time
next()is called, it willopen()the directory and read the first item from it, returning the result as aPath::Class::DirorPath::Class::Fileobject (depending, of course, on its actual type). Each subsequent call tonext()will simply iterate over the directory's contents, until there are no more items in the directory, and then the undefined value is returned. For example, to iterate over all the regular files in a directory:while (my $file = $dir->next) { next unless -f $file; my $fh = $file->open('r') or die "Can't read $file: $!"; ... }If an error occurs when opening the directory (for instance, it doesn't exist or isn't readable),
next()will throw an exception with the value of$!. - $dir->recurse( callback => sub {...} )
-
Iterates through this directory and all of its children, and all of its children's children, etc., calling the
callbacksubroutine for each entry. This is a lot like what theFile::Findmodule does, and of courseFile::Findwill work fine onPath::Classobjects, but the advantage of therecurse()method is that it will also feed your callback routinePath::Classobjects rather than just pathname strings.The
recurse()method requires acallbackparameter specifying the subroutine to invoke for each entry. It will be passed thePath::Classobject as its first argument.recurse()also accepts two boolean parameters,depthfirstandpreorderthat control the order of recursion. The default is a preorder, breadth-first search, i.e.depthfirst => 0, preorder => 1. At the time of this writing, all combinations of these two parameters are supported exceptdepthfirst => 0, preorder => 0. - $st = $file->stat()
-
Invokes
File::stat::stat()on this directory and returns aFile::statobject representing the result. - $st = $file->lstat()
-
Same as
stat(), but if$fileis a symbolic link,lstat()stats the link instead of the directory the link points to. - $class = $file->file_class()
-
Returns the class which should be used to create file objects.
Generally overriden whenever this class is subclassed.
AUTHOR
Ken Williams, kwilliams@cpan.org
SEE ALSO
Path::Class, Path::Class::File, File::Spec