如何在一个 Perl 命令中创建目录和父目录?

发布于 2024-07-25 18:07:30 字数 68 浏览 4 评论 0原文

在 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

執念 2024-08-01 18:07:30
use File::Path qw(make_path);
make_path("path/to/sub/directory");

已弃用的 mkpath 和首选 make_path 源于 Perl 5 Porters 线程中的讨论,该线程已存档 此处

简而言之,Perl 5.10 测试发现 makepath() 接口的参数解析很尴尬。 因此它被一个更简单的版本所取代,该版本将哈希值作为设置函数选项的最终参数。

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.

征棹 2024-08-01 18:07:30

使用 File::Path 模块中的 mkpath:

use File::Path qw(mkpath);
mkpath("path/to/sub/directory");

Use mkpath from the File::Path module:

use File::Path qw(mkpath);
mkpath("path/to/sub/directory");
酒中人 2024-08-01 18:07:30

如果您正在寻找具有“mkdir -p”功能的 Perl 模块,但以下代码可以工作,请忽略:

my $dir = '/root/example/dir';

system ("mkdir -p $dir 2> /dev/null") == 0 
    or die "failed to create $dir. exiting...\n";

您可以使用模块,但必须将其安装在要移植代码的每台服务器上。 我通常更喜欢使用系统函数来完成 mkdir 这样的工作,因为当我只需要一次来创建目录时,导入和调用模块的开销较小。

Kindly ignore if you are looking for a Perl module with 'mkdir -p' functionality but the following code would work:

my $dir = '/root/example/dir';

system ("mkdir -p $dir 2> /dev/null") == 0 
    or die "failed to create $dir. exiting...\n";

You can use a module but then you have to install it on each server you are going to port your code on. I usually prefer to use system function for a work like mkdir because it's a lesser overhead to import and call a module when I need it only once to create a directory.

汹涌人海 2024-08-01 18:07:30

参考 http://perldoc.perl.org/File/Path.html

“ make_path 函数创建给定的目录(如果它们之前不存在[原文如此!]),非常类似于 Unix 命令 mkdir -p

ref http://perldoc.perl.org/File/Path.html

"The make_path function creates the given directories if they don't exists [sic!] before, much like the Unix command mkdir -p"

浅浅淡淡 2024-08-01 18:07:30

mkdir() 允许您在 Perl 脚本中创建目录。

示例:

use strict;
use warnings;

my $directory = "tmp";

unless(mkdir($directory, 0755)) {
        die "Unable to create $directory\n";

该程序创建一个名为“tmp”的目录,权限设置为0755(只有所有者才有写入该目录的权限;组成员和其他人只能查看文件和列出目录内容)。

mkdir() allows you to create directories in your Perl script.

Example:

use strict;
use warnings;

my $directory = "tmp";

unless(mkdir($directory, 0755)) {
        die "Unable to create $directory\n";

This program create a directory called "tmp" with permissions set to 0755 (only the owner has the permission to write to the directory; group members and others can only view files and list the directory contents).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文