How do I create a directory and parent directories in one Perl command?

skiphoppy picture skiphoppy · Jun 26, 2009 · Viewed 21.1k times · Source

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?

Answer

Clinton Pierce picture Clinton Pierce · Jun 26, 2009
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.