1. Perl
  2. Module
  3. here

File::Path - Create/delete multi - level directories

The File::Path module allows you to create and delete multi-level directories.

# Loading modules and importing functions
use File::Path qw/mkpath rmtree/;

You can create multiple-level directories with mkpath(* 1). The return value is a list of created directory names. For example, if you create a directory called'xxx/yyy/zzz', @created will be ('xxx', 'xxx/yyy', 'xxx/yyy/zzz'). If an error (* 2) occurs in mkpath, an exception will be thrown.

# Create a multi-level directory
@created = mkpath $dir;

You can specify the permission when creating the directory with the third argument. The default is 0777. (The second argument specifies whether to output the created directory name to standard output.)

@created = mkpath($dir, 0, 0644);

You can delete multiple levels of directories with rmtree. The return value is the number of files (including directories) that could be deleted. Rmtree raises a warning if the delete fails.

# Delete multi-level directories
$removed = rmtree $dir;

This is an example to create a multi-level directory with mktree.

# Create a multi-level directory
my $dir = 'xxx/yyy/zzz';
mkpath $dir;

This is an example to delete a multi-level directory with rmtree. All files and directories under the'xxx'directory will be deleted.

my $dir = 'xxx';
rmtree $dir;

This is an example that catches the warning generated by rmtree and converts it into an exception.

my $dir = 'xxx';
local $SIG{__ WARN__} = sub {die @_};
rmtree $dir;

Some of the File::Path APIs are new and some have new features, but for compatibility reasons we're only introducing the older APIs.

An error will occur if the directory cannot be created because you do not have write permission. No error will occur if the directory already exists.

Related

Related Informatrion