Perl:无条件的文件测试运算符

标签 perl testing operators

我有这个来自 Perl Cookbook 的简单代码递归打印所有目录和文件:

use File::Find;

@ARGV = qw(.) unless @ARGV;
find sub { print $File::Find::name, -d && '/', "\n" }, @ARGV;

我不懂print $File::Find::name, -d的语法。这要怎么解释?如果 -d 测试 $File::Find::name 是否是目录,因此 -d 是函数 print 的参数?或者 Perl 是否明确地将 standalone -d 解释为 if -d

最佳答案

不,-d 是一个独立的语句,它测试 $_。所以本质上是一样的

-d $_ && '/'

上面写着“如果文件是一个目录,则返回一个斜杠字符(打印)”。 sub 代码块由 File::Find 中的 find 函数使用,其中 $_ 包含当前文件的文件名。

逗号 , 分隔为 print 语句返回字符串的语句列表:

print $File::Find::name,   # print the files name
-d && '/',                 # if it is a dir, print /
"\n"                       # print a newline

documentation对于 -d(包含在 perldoc for -X 中,其中列出了所有文件测试)状态:

If the argument is omitted, tests $_ ...

这适用于 -X 下的所有文件测试。

&& 可以这样使用的原因是它比逗号运算符 , 具有更高的优先级。这记录在 perldoc perlop

关于Perl:无条件的文件测试运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68743839/

相关文章:

perl - 在 Perl 脚本中隔离命名空间

perl - 将 'shift' 分配给变量是什么意思?

testing - XCode9:XCUITest 给出 Runner UIAccessibility 错误

perl - 如何在不丢失颜色的情况下从 Perl 中的终端进行管道传输?

linux - 我怎么知道我在 perl 脚本中运行的是 64 位还是 32 位 linux?

sql - 如何确定负载测试结果在 Visual Studio 2010 中的保存位置?

php - PHP开发和生产环境

python - 运算符(operator)模块和 pandas

Java:数组并在添加值时使用++运算符

javascript - !!~(不是 not not tilde/bang bang tilde)如何改变 'contains/included' 数组方法调用的结果?