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

NAME

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

SYNOPSIS

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

DESCRIPTION

49       The "Path::Class::Dir" class contains functionality for manipulating
50       directory names in a cross-platform way.
51

METHODS

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

AUTHOR

327       Ken Williams, ken@mathforum.org
328

SEE ALSO

330       Path::Class, Path::Class::File, File::Spec
331
332
333
334perl v5.8.8                       2007-02-12               Path::Class::Dir(3)
Impressum