File::Path - Create or remove directory trees
2.18 - released November 4 2020.
use File::Path qw(make_path remove_tree);
@created = make_path('foo/bar/baz', '/zug/zwang');
@created = make_path('foo/bar/baz', '/zug/zwang', {
verbose => 1,
mode => 0711,
});
make_path('foo/bar/baz', '/zug/zwang', {
chmod => 0777,
});
$removed_count = remove_tree('foo/bar/baz', '/zug/zwang', {
verbose => 1,
error => \my $err_list,
safe => 1,
});
# legacy (interface promoted before v2.00)
@created = mkpath('/foo/bar/baz');
@created = mkpath('/foo/bar/baz', 1, 0711);
@created = mkpath(['/foo/bar/baz', 'blurfl/quux'], 1, 0711);
$removed_count = rmtree('foo/bar/baz', 1, 1);
$removed_count = rmtree(['foo/bar/baz', 'blurfl/quux'], 1, 1);
# legacy (interface promoted before v2.06)
@created = mkpath('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
$removed_count = rmtree('foo/bar/baz', '/zug/zwang', { verbose => 1, mode => 0711 });
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:
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:
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.
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.
If present, will cause make_path
to print the name of each directory as it is created. By default nothing is printed.
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.