Description :
This module provides a convenient way to create directories of arbitrary depth and to delete an entire directory subtree from the filesystem.
The following functions are provided:
* make_path( $dir1, $dir2, .... )
* make_path( $dir1, $dir2, ...., \\%opts )
The \'make_path\' function creates the given directories if they don\'t exist before, much like the Unix command \'mkdir -p\'.
The function accepts a list of directories to be created. Its behaviour may be tuned by an optional hashref appearing as the last parameter on the call.
The function returns the list of directories actually created during the call; in scalar context the number of directories created.
The following keys are recognised in the option hash:
* mode => $num
The numeric permissions mode to apply to each created directory (defaults to \'0777\'), to be modified by the current \'umask\'. If the directory already exists (and thus does not need to be created), the permissions will not be modified.
\'mask\' is recognised as an alias for this parameter.
* chmod => $num
Takes a numeric mode to apply to each created directory (not modified by the current \'umask\'). If the directory already exists (and thus does not need to be created), the permissions will not be modified.
* verbose => $bool
If present, will cause \'make_path\' to print the name of each directory as it is created. By default nothing is printed.
* error => \\$err
If present, it should be a reference to a scalar. This scalar will be made to reference an array, which will be used to store any errors that are encountered. See the \"ERROR HANDLING\" section for more information.
If this parameter is not used, certain error conditions may raise a fatal error that will cause the program to halt, unless trapped in an \'eval\' block.
* owner => $owner
* user => $owner
* uid => $owner
If present, will cause any created directory to be owned by \'$owner\'. If the value is numeric, it will be interpreted as a uid; otherwise a username is assumed. An error will be issued if the username cannot be mapped to a uid, the uid does not exist or the process lacks the privileges to change ownership.
Ownership of directories that already exist will not be changed.
\'user\' and \'uid\' are aliases of \'owner\'.
* group => $group
If present, will cause any created directory to be owned by the group \'$group\'. If the value is numeric, it will be interpreted as a gid; otherwise a group name is assumed. An error will be issued if the group name cannot be mapped to a gid, the gid does not exist or the process lacks the privileges to change group ownership.
Group ownership of directories that already exist will not be changed.
make_path \'/var/tmp/webcache\', {owner=>\'nobody\', group=>\'nogroup\'};
* mkpath( $dir )
* mkpath( $dir, $verbose, $mode )
* mkpath( [$dir1, $dir2,...], $verbose, $mode )
* mkpath( $dir1, $dir2,..., \\%opt )
The \'mkpath()\' function provide the legacy interface of \'make_path()\' with a different interpretation of the arguments passed. The behaviour and return value of the function is otherwise identical to \'make_path()\'.
* remove_tree( $dir1, $dir2, .... )
* remove_tree( $dir1, $dir2, ...., \\%opts )
The \'remove_tree\' function deletes the given directories and any files and subdirectories they might contain, much like the Unix command \'rm -rf\' or the Windows commands \'rmdir /s\' and \'rd /s\'.
The function accepts a list of directories to be removed. (In point of fact, it will also accept filesystem entries which are not directories, such as regular files and symlinks. But, as its name suggests, its intent is to remove trees rather than individual files.)
\'remove_tree()\'\'s behaviour may be tuned by an optional hashref appearing as the last parameter on the call. If an empty string is passed to \'remove_tree\', an error will occur.
*NOTE:* For security reasons, we strongly advise use of the hashref-as-final-argument syntax -- specifically, with a setting of the \'safe\' element to a true value.
remove_tree( $dir1, $dir2, ...., { safe => 1, ... # other key-value pairs }, );
The function returns the number of files successfully deleted.
The following keys are recognised in the option hash:
* verbose => $bool
If present, will cause \'remove_tree\' to print the name of each file as it is unlinked. By default nothing is printed.
* safe => $bool
When set to a true value, will cause \'remove_tree\' to skip the files for which the process lacks the required privileges needed to delete files, such as delete privileges on VMS. In other words, the code will make no attempt to alter file permissions. Thus, if the process is interrupted, no filesystem object will be left in a more permissive mode.
* keep_root => $bool
When set to a true value, will cause all files and subdirectories to be removed, except the initially specified directories. This comes in handy when cleaning out an application\'s scratch directory.
remove_tree( \'/tmp\', {keep_root => 1} );
* result => \\$res
If present, it should be a reference to a scalar. This scalar will be made to reference an array, which will be used to store all files and directories unlinked during the call. If nothing is unlinked, the array will be empty.
remove_tree( \'/tmp\', {result => \\my $list} ); print \"unlinked $_\ \" for AATT$list;
This is a useful alternative to the \'verbose\' key.
* error => \\$err
If present, it should be a reference to a scalar. This scalar will be made to reference an array, which will be used to store any errors that are encountered. See the \"ERROR HANDLING\" section for more information.
Removing things is a much more dangerous proposition than creating things. As such, there are certain conditions that \'remove_tree\' may encounter that are so dangerous that the only sane action left is to kill the program.
Use \'error\' to trap all that is reasonable (problems with permissions and the like), and let it die if things get out of hand. This is the safest course of action.
* rmtree( $dir )
* rmtree( $dir, $verbose, $safe )
* rmtree( [$dir1, $dir2,...], $verbose, $safe )
* rmtree( $dir1, $dir2,..., \\%opt )
The \'rmtree()\' function provide the legacy interface of \'remove_tree()\' with a different interpretation of the arguments passed. The behaviour and return value of the function is otherwise identical to \'remove_tree()\'.
*NOTE:* For security reasons, we strongly advise use of the hashref-as-final-argument syntax, specifically with a setting of the \'safe\' element to a true value.
rmtree( $dir1, $dir2, ...., { safe => 1, ... # other key-value pairs }, );
|