perl - Perl 中没有 $ 或 @ 的 _ (单下划线)是什么?

标签 perl

我有一些这样的代码:

if (!-d _ || !-o _ || ($vncUserDirUnderTmp && ($mode & 0777) != 0700)) {
    raise Exception("$prog: Wrong type or access mode of $vncUserDir.\n")
}

我可以在 Perl 中找到有关 @_$_ 的信息,但不能单独找到有关 _ 的信息。 这里的-d _-o _是什么意思?

最佳答案

filetest (-X)一起使用时运算符stat使用之前文件测试的结构

If any of the file tests (or either the stat or lstat operator) is given the special filehandle consisting of a solitary underline, then the stat structure of the previous file test (or stat operator) is used, saving a system call.
...
Example:

stat($filename);  
print "Readable\n" if -r _;  
print "Writable\n" if -w _;
...

所以在你的例子中!-d _测试文件是否最后 stat -ed 不是目录。

更新  

不寻常的_确实是一个 typeglob *_但运算符(operator)期望文件句柄 *可以省略,例如 <*STDIN>可以写成<STDIN> 。在符号表中找到

print *{$main::{_}}{IO}, "\n";   # -->  IO::Handle=IO(0x2311970)

在一行中 *_仅在调用stat后才进行设置和_已制作。

关于perl - Perl 中没有 $ 或 @ 的 _ (单下划线)是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46215052/

相关文章:

perl - 将带引号的字符串传递给system(),使引号保持完整

regex - perl 中的 s[][] 表示法

perl - 如何在 Perl 中拦截 HTTP 请求?

arrays - perl文件操作

perl - 大阵列上的 grep 或拼接

regex - 用正则表达式匹配基于度数的地理坐标

perl - 关于相等运算符的警告

Perl - 将目录从其他机器复制到本地机器

perl - 在 Perl 中,如何访问另一个包中定义的标量?

windows - 使用 SSH 从 Linux Box 执行 Perl 脚本导致 "The local device name is already in use"