Search code examples
perlfiledir

mkpath File::Path does not seem to work as expected


I am using mkpath to create a directory.

mkpath('/home/myhome') or die "Create of directory  failed: $!";

This works if the directory does not exists, yet fails if it does exist. From the perldocs it said it was similar to mkdir -p unix. However, that command does not fail if directory already exists.

What should I be looking at to create a directory with multiple paths quietly? v5.8.8


Solution

  • mkpath throws an exception on error, so you want the following:

    mkpath('/home/myhome');
    

    If you want a custom message, you can use the following:

    eval { mkpath('/home/myhome'); 1 }
       or die "Can't create home directory: $@\n";
    

    Note the use of $@, not $!.