File::Path - Create or remove directory trees This document describes version 2.00_06 of File::Path, released 2007-07-04. use File::Path; # modern mkpath( 'foo/bar/baz', '/zug/zwang', {verbose => 1} ); rmtree( 'foo/bar/baz', '/zug/zwang', { verbose => 1, error => \my $err_list } ); # traditional mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711); rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1); The "mkpath" function provides a convenient way to create directories, even if your "mkdir" kernel call won't create more than one level of directory at a time. Similarly, the "rmtree" function provides a convenient way to delete a subtree from the directory structure, much like the Unix command "rm -r". Both functions may be called in one of two ways, the traditional, compatible with code written since the dawn of time, and modern, that offers a more flexible and readable idiom. New code should use the modern interface.

FUNCTIONS
The modern way of calling "mkpath" and "rmtree" is with an optional hash reference at the end of the parameter list that holds various keys that can be used to control the function's behaviour, following a plain list of directories upon which to operate. "mkpath" The following keys are recognised as as parameters to "mkpath". It returns the list of files actually created during the call. my @created = mkpath( qw(/tmp /flub /home/nobody), {verbose => 1, mode => 0750}, ); print "created $_\n" for @created; The numeric mode to use when creating the directories (defaults to 07777), to be modified by the current "umask". ("mask" is recognised as an alias for this parameter). If present, will cause "mkpath" to print the name of each directory as it is created. By default nothing is printed. If present, will be interpreted as a reference to a list, and will be used to store any errors that are encountered. See the ERROR HANDLING section below to find out more. If this parameter is not used, any errors encountered will raise a fatal error that need to be trapped in an "eval" block, or the program will halt. "rmtree" If present, will cause "rmtree" to print the name of each file as it is unlinked. By default nothing is printed. When set to a true value, will cause "rmtree" to skip any files to which you do not have delete access (if running under VMS) or write access (if running under another OS). This will change in the future when a criterion for 'delete permission' under OSs other than VMS is settled. When set to a true value, will cause everything except the specified base directories to be unlinked. This comes in handy when cleaning out an application's scratch directory. rmtree( '/tmp', {keep_root => 1} ); If present, will be interpreted as a reference to a list, and will be used to store the list of all files and directories unlinked during the call. If nothing is unlinked, a reference to an empty list is returned (rather than "undef"). rmtree( '/tmp', {result => \my $list} ); print "unlinked $_\n" for @$list; If present, will be interpreted as a reference to a list, and will be used to store any errors that are encountered. See the ERROR HANDLING section below to find out more. If this parameter is not used, any errors encountered will raise a fatal error that need to be trapped in an "eval" block, or the program will halt.

TRADITIONAL INTERFACE
The old interface for "mkpath" and "rmtree" take a reference to a list of directories (to create or remove), followed by a series of positional numeric modal parameters that control their behaviour. This design made it difficult to add additional functionality, as well as posed the problem of what to do when you don't care how the initial positional parameters are specified but only the last one needs to be specified. The calls themselves are also less self-documenting. "mkpath" takes three arguments: The name of the path to create, or a reference to a list of paths to create, a boolean value, which if TRUE will cause "mkpath" to print the name of each directory as it is created (defaults to FALSE), and the numeric mode to use when creating the directories (defaults to 0777), to be modified by the current umask. It returns a list of all directories (including intermediates, determined using the Unix '/' separator) created. In scalar context it returns the number of directories created. If a system error prevents a directory from being created, then the "mkpath" function throws a fatal error with "Carp::croak". This error can be trapped with an "eval" block: eval { mkpath($dir) }; if ($@) { print "Couldn't create $dir: $@"; } In the traditional form, "rmtree" takes three arguments: the root of the subtree to delete, or a reference to a list of roots. All of the files and directories below each root, as well as the roots themselves, will be deleted. a boolean value, which if TRUE will cause "rmtree" to print a message each time it examines a file, giving the name of the file, and indicating whether it's using "rmdir" or "unlink" to remove it, or that it's skipping it. (defaults to FALSE) a boolean value, which if TRUE will cause "rmtree" to skip any files to which you do not have delete access (if running under VMS) or write access (if running under another OS). This will change in the future when a criterion for 'delete permission' under OSs other than VMS is settled. (defaults to FALSE) It returns the number of files, directories and symlinks successfully deleted. Symlinks are simply deleted and not followed. Note also that the occurrence of errors in "rmtree" using the traditional interface can be determined only by trapping diagnostic messages using $SIG{__WARN__}; it is not apparent from the return value. (The modern interface may use the "error" parameter to record any problems encountered.

ERROR HANDLING
If "mkpath" or "rmtree" encounter an error, a diagnostic message will be printed to "STDERR" via "carp" (for non-fatal errors), or via "croak" (for fatal errors). If this behaviour is not desirable, the "error" attribute may be used to hold a reference to a variable, which will be used to store the diagnostics. The result is a reference to a list of hash references. For each hash reference, the key is the name of the file, and the value is the error message (usually the contents of $!). An example usage looks like: rmpath( 'foo/bar', 'bar/rat', {error => \my $err} ); for my $diag (@$err) { my ($file, $message) = each %$diag; print "problem unlinking $file: $message\n"; } If no errors are encountered, $err will point to an empty list (thus there is no need to test for "undef"). If a general error is encountered (for instance, "rmtree" attempts to remove a directory tree that does not exist), the diagnostic key will be empty, only the value will be set: rmpath( '/no/such/path', {error => \my $err} ); for my $diag (@$err) { my ($file, $message) = each %$diag; if ($file eq '') { print "general error: $message\n"; } }

NOTES
HEURISTICS The functions detect (as far as possible) which way they are being called and will act appropriately. It is important to remember that the heuristic for detecting the old style is either the presence of an array reference, or two or three parameters total and second and third parameters are numeric. Hence... mkpath '486', '487', '488'; ... will not assume the modern style and create three directories, rather it will create one directory verbosely, setting the permission to 0750 (488 being the decimal equivalent of octal 750). Here, old style trumps new. It must, for backwards compatibility reasons. If you want to ensure there is absolutely no ambiguity about which way the function will behave, make sure the first parameter is a reference to a one-element list, to force the old style interpretation: mkpath ['486'], '487', '488'; and get only one directory created. Or add a reference to an empty parameter hash, to force the new style: mkpath '486', '487', '488', {}; ... and hence create the three directories. If the empty hash reference seems a little strange to your eyes, or you suspect a subsequent programmer might helpfully optimise it away, you can add a parameter set to a default value: mkpath '486', '487', '488', {verbose => 0}; RACE CONDITIONS There are race conditions internal to the implementation of "rmtree" making it unsafe to use on directory trees which may be altered or moved while "rmtree" is running, and in particular on any directory trees with any path components or subdirectories potentially writable by untrusted users. Additionally, if the "skip_others" parameter is not set (or the third parameter in the traditional inferface is not TRUE) and "rmtree" is interrupted, it may leave files and directories with permissions altered to allow deletion. "File::Path" blindly exports "mkpath" and "rmtree" into the current namespace. These days, this is considered bad style, but to change it now would break too much code. Nonetheless, you are invited to specify what it is you are expecting to use: use File::Path 'rmtree'; On Windows, if "mkpath" gives you the warning: No such file or directory, this may mean that you've exceeded your filesystem's maximum path length. Find::File::Rule When removing directory trees, if you want to examine each file before deciding whether to deleting it (and possibly leaving large swathes alone), File::Find::Rule offers a convenient and flexible approach. Please report all bugs on the RT queue: <http://rt.cpan.org/NoAuth/Bugs.html?Dist=File-Path> Tim Bunce <Tim.Bunce@ig.co.uk> and Charles Bailey <bailey@newman.upenn.edu>. Currently maintained by David Landgren <david@landgren.net>. This module is copyright (C) Charles Bailey, Tim Bunce and David Landgren 1995-2007. All rights reserved. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.