views:

913

answers:

2

In Perl, how can I create a subdirectory and, at the same time, create parent directories if they do not exist? Like UNIX's mkdir -p command?

+4  A: 

Use mkpath from the File::Path module:

use File::Path qw(mkpath);
mkpath("path/to/sub/directory");
skiphoppy
Researching this just now, I see that mkpath() is deprecated and the official interface is now make_path(). If someone writes up a great explanation as to what that buys us, I'll make that the accepted answer.
skiphoppy
+8  A: 
use File::Path qw(make_path);
make_path("path/to/sub/directory");

The deprecated mkpath and preferred make_path stemmed from a discussion in Perl 5 Porters thread that's archived here.

In a nutshell, Perl 5.10 testing turned up awkwardness in the argument parsing of the makepath() interface. So it was replaced with a simpler version that took a hash as the final argument to set options for the function.

clintp