如何在一个Perl命令中创建目录和父目录?
问题描述:
在Perl中,如何创建一个子目录,同时创建父目录(如果不存在)像UNIX的mkdir -p命令?
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?
答
use File::Path qw(make_path);
make_path("path/to/sub/directory");
已弃用的mkpath和首选make_path起源于存档的Perl 5 Porters线程中的讨论 here 。
The deprecated mkpath and preferred make_path stemmed from a discussion in Perl 5 Porters thread that's archived here.
简而言之,Perl 5.10测试在makepath()接口的参数解析中变得尴尬。所以它被替换为一个更简单的版本,采用哈希作为最终参数来设置函数的选项。
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.