标题: perl打开不存在的文件路径时,怎么自动创建目录? [打印本页] 作者: rrrrrrrr8 时间: 2011-10-13 15:23 标题: perl打开不存在的文件路径时,怎么自动创建目录? 有什么方法?
比如
open FILE,">dir1\\dir2\\file1" or die "$!";
当前目录下没有dir1,它就抛:No such file or directory at test.pl line 39.作者: rrrrrrrr8 时间: 2011-10-13 16:27
看来没简单现成的方法,自己写一个:
sub createFilePath{
my $filePath = shift;
my $separator = shift || "\\";
my $pos = 0;
while (($pos = index($filePath, $separator, $pos)) > -1) {
my $path = substr $filePath,0,$pos;
unless (-e $path) {
mkdir $path or die $!;
} elsif( !(-d $path)) {
die "$path is not a directory."
} else {
}
$pos++;
}
}作者: marscld 时间: 2011-10-13 16:35
使用 File::Util的make_dir().