1Path::Class::Dir(3)   User Contributed Perl Documentation  Path::Class::Dir(3)
2
3
4

NAME

6       Path::Class::Dir - Objects representing directories
7

VERSION

9       version 0.23
10

SYNOPSIS

12         use Path::Class qw(dir);  # Export a short constructor
13
14         my $dir = dir('foo', 'bar');       # Path::Class::Dir object
15         my $dir = Path::Class::Dir->new('foo', 'bar');  # Same thing
16
17         # Stringifies to 'foo/bar' on Unix, 'foo\bar' on Windows, etc.
18         print "dir: $dir\n";
19
20         if ($dir->is_absolute) { ... }
21         if ($dir->is_relative) { ... }
22
23         my $v = $dir->volume; # Could be 'C:' on Windows, empty string
24                               # on Unix, 'Macintosh HD:' on Mac OS
25
26         $dir->cleanup; # Perform logical cleanup of pathname
27         $dir->resolve; # Perform physical cleanup of pathname
28
29         my $file = $dir->file('file.txt'); # A file in this directory
30         my $subdir = $dir->subdir('george'); # A subdirectory
31         my $parent = $dir->parent; # The parent directory, 'foo'
32
33         my $abs = $dir->absolute; # Transform to absolute path
34         my $rel = $abs->relative; # Transform to relative path
35         my $rel = $abs->relative('/foo'); # Relative to /foo
36
37         print $dir->as_foreign('Mac');   # :foo:bar:
38         print $dir->as_foreign('Win32'); #  foo\bar
39
40         # Iterate with IO::Dir methods:
41         my $handle = $dir->open;
42         while (my $file = $handle->read) {
43           $file = $dir->file($file);  # Turn into Path::Class::File object
44           ...
45         }
46
47         # Iterate with Path::Class methods:
48         while (my $file = $dir->next) {
49           # $file is a Path::Class::File or Path::Class::Dir object
50           ...
51         }
52

DESCRIPTION

54       The "Path::Class::Dir" class contains functionality for manipulating
55       directory names in a cross-platform way.
56

METHODS

58       $dir = Path::Class::Dir->new( <dir1>, <dir2>, ... )
59       $dir = dir( <dir1>, <dir2>, ... )
60           Creates a new "Path::Class::Dir" object and returns it.  The
61           arguments specify names of directories which will be joined to
62           create a single directory object.  A volume may also be specified
63           as the first argument, or as part of the first argument.  You can
64           use platform-neutral syntax:
65
66             my $dir = dir( 'foo', 'bar', 'baz' );
67
68           or platform-native syntax:
69
70             my $dir = dir( 'foo/bar/baz' );
71
72           or a mixture of the two:
73
74             my $dir = dir( 'foo/bar', 'baz' );
75
76           All three of the above examples create relative paths.  To create
77           an absolute path, either use the platform native syntax for doing
78           so:
79
80             my $dir = dir( '/var/tmp' );
81
82           or use an empty string as the first argument:
83
84             my $dir = dir( '', 'var', 'tmp' );
85
86           If the second form seems awkward, that's somewhat intentional -
87           paths like "/var/tmp" or "\Windows" aren't cross-platform concepts
88           in the first place (many non-Unix platforms don't have a notion of
89           a "root directory"), so they probably shouldn't appear in your code
90           if you're trying to be cross-platform.  The first form is perfectly
91           natural, because paths like this may come from config files, user
92           input, or whatever.
93
94           As a special case, since it doesn't otherwise mean anything useful
95           and it's convenient to define this way, "Path::Class::Dir->new()"
96           (or "dir()") refers to the current directory
97           ("File::Spec->curdir").  To get the current directory as an
98           absolute path, do "dir()->absolute".
99
100           Finally, as another special case "dir(undef)" will return undef,
101           since that's usually an accident on the part of the caller, and
102           returning the root directory would be a nasty surprise just asking
103           for trouble a few lines later.
104
105       $dir->stringify
106           This method is called internally when a "Path::Class::Dir" object
107           is used in a string context, so the following are equivalent:
108
109             $string = $dir->stringify;
110             $string = "$dir";
111
112       $dir->volume
113           Returns the volume (e.g. "C:" on Windows, "Macintosh HD:" on Mac
114           OS, etc.) of the directory object, if any.  Otherwise, returns the
115           empty string.
116
117       $dir->is_dir
118           Returns a boolean value indicating whether this object represents a
119           directory.  Not surprisingly, "Path::Class::File" objects always
120           return false, and "Path::Class::Dir" objects always return true.
121
122       $dir->is_absolute
123           Returns true or false depending on whether the directory refers to
124           an absolute path specifier (like "/usr/local" or "\Windows").
125
126       $dir->is_relative
127           Returns true or false depending on whether the directory refers to
128           a relative path specifier (like "lib/foo" or "./dir").
129
130       $dir->cleanup
131           Performs a logical cleanup of the file path.  For instance:
132
133             my $dir = dir('/foo//baz/./foo')->cleanup;
134             # $dir now represents '/foo/baz/foo';
135
136       $dir->resolve
137           Performs a physical cleanup of the file path.  For instance:
138
139             my $dir = dir('/foo//baz/../foo')->resolve;
140             # $dir now represents '/foo/foo', assuming no symlinks
141
142           This actually consults the filesystem to verify the validity of the
143           path.
144
145       $file = $dir->file( <dir1>, <dir2>, ..., <file> )
146           Returns a "Path::Class::File" object representing an entry in $dir
147           or one of its subdirectories.  Internally, this just calls
148           "Path::Class::File->new( @_ )".
149
150       $subdir = $dir->subdir( <dir1>, <dir2>, ... )
151           Returns a new "Path::Class::Dir" object representing a subdirectory
152           of $dir.
153
154       $parent = $dir->parent
155           Returns the parent directory of $dir.  Note that this is the
156           logical parent, not necessarily the physical parent.  It really
157           means we just chop off entries from the end of the directory list
158           until we cain't chop no more.  If the directory is relative, we
159           start using the relative forms of parent directories.
160
161           The following code demonstrates the behavior on absolute and
162           relative directories:
163
164             $dir = dir('/foo/bar');
165             for (1..6) {
166               print "Absolute: $dir\n";
167               $dir = $dir->parent;
168             }
169
170             $dir = dir('foo/bar');
171             for (1..6) {
172               print "Relative: $dir\n";
173               $dir = $dir->parent;
174             }
175
176             ########### Output on Unix ################
177             Absolute: /foo/bar
178             Absolute: /foo
179             Absolute: /
180             Absolute: /
181             Absolute: /
182             Absolute: /
183             Relative: foo/bar
184             Relative: foo
185             Relative: .
186             Relative: ..
187             Relative: ../..
188             Relative: ../../..
189
190       @list = $dir->children
191           Returns a list of "Path::Class::File" and/or "Path::Class::Dir"
192           objects listed in this directory, or in scalar context the number
193           of such objects.  Obviously, it is necessary for $dir to exist and
194           be readable in order to find its children.
195
196           Note that the children are returned as subdirectories of $dir, i.e.
197           the children of foo will be foo/bar and foo/baz, not bar and baz.
198
199           Ordinarily "children()" will not include the self and parent
200           entries "." and ".." (or their equivalents on non-Unix systems),
201           because that's like I'm-my-own-grandpa business.  If you do want
202           all directory entries including these special ones, pass a true
203           value for the "all" parameter:
204
205             @c = $dir->children(); # Just the children
206             @c = $dir->children(all => 1); # All entries
207
208           In addition, there's a "no_hidden" parameter that will exclude all
209           normally "hidden" entries - on Unix this means excluding all
210           entries that begin with a dot ("."):
211
212             @c = $dir->children(no_hidden => 1); # Just normally-visible entries
213
214       $abs = $dir->absolute
215           Returns a "Path::Class::Dir" object representing $dir as an
216           absolute path.  An optional argument, given as either a string or a
217           "Path::Class::Dir" object, specifies the directory to use as the
218           base of relativity - otherwise the current working directory will
219           be used.
220
221       $rel = $dir->relative
222           Returns a "Path::Class::Dir" object representing $dir as a relative
223           path.  An optional argument, given as either a string or a
224           "Path::Class::Dir" object, specifies the directory to use as the
225           base of relativity - otherwise the current working directory will
226           be used.
227
228       $boolean = $dir->subsumes($other)
229           Returns true if this directory spec subsumes the other spec, and
230           false otherwise.  Think of "subsumes" as "contains", but we only
231           look at the specs, not whether $dir actually contains $other on the
232           filesystem.
233
234           The $other argument may be a "Path::Class::Dir" object, a
235           "Path::Class::File" object, or a string.  In the latter case, we
236           assume it's a directory.
237
238             # Examples:
239             dir('foo/bar' )->subsumes(dir('foo/bar/baz'))  # True
240             dir('/foo/bar')->subsumes(dir('/foo/bar/baz')) # True
241             dir('foo/bar' )->subsumes(dir('bar/baz'))      # False
242             dir('/foo/bar')->subsumes(dir('foo/bar'))      # False
243
244       $boolean = $dir->contains($other)
245           Returns true if this directory actually contains $other on the
246           filesystem.  $other doesn't have to be a direct child of $dir, it
247           just has to be subsumed.
248
249       $foreign = $dir->as_foreign($type)
250           Returns a "Path::Class::Dir" object representing $dir as it would
251           be specified on a system of type $type.  Known types include
252           "Unix", "Win32", "Mac", "VMS", and "OS2", i.e. anything for which
253           there is a subclass of "File::Spec".
254
255           Any generated objects (subdirectories, files, parents, etc.) will
256           also retain this type.
257
258       $foreign = Path::Class::Dir->new_foreign($type, @args)
259           Returns a "Path::Class::Dir" object representing $dir as it would
260           be specified on a system of type $type.  Known types include
261           "Unix", "Win32", "Mac", "VMS", and "OS2", i.e. anything for which
262           there is a subclass of "File::Spec".
263
264           The arguments in @args are the same as they would be specified in
265           "new()".
266
267       @list = $dir->dir_list([OFFSET, [LENGTH]])
268           Returns the list of strings internally representing this directory
269           structure.  Each successive member of the list is understood to be
270           an entry in its predecessor's directory list.  By contract,
271           "Path::Class->new( $dir->dir_list )" should be equivalent to $dir.
272
273           The semantics of this method are similar to Perl's "splice" or
274           "substr" functions; they return "LENGTH" elements starting at
275           "OFFSET".  If "LENGTH" is omitted, returns all the elements
276           starting at "OFFSET" up to the end of the list.  If "LENGTH" is
277           negative, returns the elements from "OFFSET" onward except for
278           "-LENGTH" elements at the end.  If "OFFSET" is negative, it counts
279           backward "OFFSET" elements from the end of the list.  If "OFFSET"
280           and "LENGTH" are both omitted, the entire list is returned.
281
282           In a scalar context, "dir_list()" with no arguments returns the
283           number of entries in the directory list; "dir_list(OFFSET)" returns
284           the single element at that offset; "dir_list(OFFSET, LENGTH)"
285           returns the final element that would have been returned in a list
286           context.
287
288       $fh = $dir->open()
289           Passes $dir to "IO::Dir->open" and returns the result as an
290           "IO::Dir" object.  If the opening fails, "undef" is returned and $!
291           is set.
292
293       $dir->mkpath($verbose, $mode)
294           Passes all arguments, including $dir, to "File::Path::mkpath()" and
295           returns the result (a list of all directories created).
296
297       $dir->rmtree($verbose, $cautious)
298           Passes all arguments, including $dir, to "File::Path::rmtree()" and
299           returns the result (the number of files successfully deleted).
300
301       $dir->remove()
302           Removes the directory, which must be empty.  Returns a boolean
303           value indicating whether or not the directory was successfully
304           removed.  This method is mainly provided for consistency with
305           "Path::Class::File"'s "remove()" method.
306
307       $dir_or_file = $dir->next()
308           A convenient way to iterate through directory contents.  The first
309           time "next()" is called, it will "open()" the directory and read
310           the first item from it, returning the result as a
311           "Path::Class::Dir" or "Path::Class::File" object (depending, of
312           course, on its actual type).  Each subsequent call to "next()" will
313           simply iterate over the directory's contents, until there are no
314           more items in the directory, and then the undefined value is
315           returned.  For example, to iterate over all the regular files in a
316           directory:
317
318             while (my $file = $dir->next) {
319               next unless -f $file;
320               my $fh = $file->open('r') or die "Can't read $file: $!";
321               ...
322             }
323
324           If an error occurs when opening the directory (for instance, it
325           doesn't exist or isn't readable), "next()" will throw an exception
326           with the value of $!.
327
328       $dir->recurse( callback => sub {...} )
329           Iterates through this directory and all of its children, and all of
330           its children's children, etc., calling the "callback" subroutine
331           for each entry.  This is a lot like what the "File::Find" module
332           does, and of course "File::Find" will work fine on "Path::Class"
333           objects, but the advantage of the "recurse()" method is that it
334           will also feed your callback routine "Path::Class" objects rather
335           than just pathname strings.
336
337           The "recurse()" method requires a "callback" parameter specifying
338           the subroutine to invoke for each entry.  It will be passed the
339           "Path::Class" object as its first argument.
340
341           "recurse()" also accepts two boolean parameters, "depthfirst" and
342           "preorder" that control the order of recursion.  The default is a
343           preorder, breadth-first search, i.e. "depthfirst => 0, preorder =>
344           1".  At the time of this writing, all combinations of these two
345           parameters are supported except "depthfirst => 0, preorder => 0".
346
347       $st = $file->stat()
348           Invokes "File::stat::stat()" on this directory and returns a
349           "File::stat" object representing the result.
350
351       $st = $file->lstat()
352           Same as "stat()", but if $file is a symbolic link, "lstat()" stats
353           the link instead of the directory the link points to.
354
355       $class = $file->file_class()
356           Returns the class which should be used to create file objects.
357
358           Generally overridden whenever this class is subclassed.
359

AUTHOR

361       Ken Williams, kwilliams@cpan.org
362

SEE ALSO

364       Path::Class, Path::Class::File, File::Spec
365
366
367
368perl v5.12.3                      2011-04-22               Path::Class::Dir(3)
Impressum