Perl中的if-f <filename>有什么作用?

问题描述

| 我遇到了以下代码行:
if( -f <filename> ) { ... }
出现“ 1”以测试文件名是否存在,但我不确定。到目前为止,Google尚无帮助(Google很难找到“ -f \”),也没有我的Perl书籍。 任何人都可以请教吗?     

解决方法

        参见perlfunc。 它列出了所有Perl内置函数,包括\“ file test \”:
-X FILEHANDLE
-X EXPR
-X DIRHANDLE
-X
其中
-X
是以下之一:
-r: File is readable by effective uid/gid.
-w: File is writable by effective uid/gid.
-x: File is executable by effective uid/gid.
-o: File is owned by effective uid.

-R: File is readable by real uid/gid.
-W: File is writable by real uid/gid.
-X: File is executable by real uid/gid.
-O: File is owned by real uid.

-e: File exists.
-z: File has zero size (is empty).
-s: File has nonzero size (returns size in bytes).

-f: File is a plain file.
-d: File is a directory.
-l: File is a symbolic link.
-p: File is a named pipe (FIFO),or Filehandle is a pipe.
-S: File is a socket.
-b: File is a block special file.
-c: File is a character special file.
-t: Filehandle is opened to a tty.

-u: File has setuid bit set.
-g: File has setgid bit set.
-k: File has sticky bit set.

-T: File is an ASCII text file (heuristic guess).
-B: File is a \"binary\" file (opposite of -T).

-M: Script start time minus file modification time,in days.
-A: Same for access time.
-C: Same for inode change time (Unix,may differ for other platforms)
    ,        在Unix中,目录可以包含以下类型的文件: 普通(您将考虑的文件) 目录 命名管道 命名插座 ...
-f
测试提供的名称是否引用的文件是纯文件。如果文件不存在,它将返回undef(错误
ENOENT
),如果文件存在但不是普通文件(例如,如果是目录),它将返回一些其他假值。 -X运算符     ,        查找Perl文件测试运算符。     ,           
if (-f <filename> ) { ... }
似乎是我误解了这个问题,否则其他人都有了。 首先,让我们做一些假设。 假设您没有重载所有
-X
前缀一元运算符(不是全部而是全部), 我们还假设您尚未重载
<>
circumfix迭代运算符。 假定上面给出的两个给定都成立,则表示法“ 10”将做一个“ 11”(取决于“ 12”的值),然后将该结果传递回filetest运算符以进行后续评估,通常但不一定通过stat(2)。这意味着最好打开一个名称为ѭ13file的文件句柄,除非真正涉及深层巫术,否则此文件句柄才能起作用。 如果也会对
<$filename>
执行此操作,除了now15ѭ是间接句柄,而不是像
filename
这样的直接句柄。再次,
$/
被尊重。实际上,我会在所有术语中都使用适合于与其关联的文件句柄的文件名。例如:
our $fn = \"/etc/termcap\";
open($fn,\"<\",$fn) || die \"can\'t open $fn: $!\";
这样,“ 19”和“ 20”消息实际上告诉我文件的名称。例如:
while (<$fh>) {
     warn \"oops\" if whatever;
}
会告诉我行号和我选择的文件名。 现在,如果“ 22”的操作数偏离了允许的无括号的宾语对象的规则(即以零或多个美元符号开头的裸字),则它不是迭代运算符,而是通配符猝发器。即使没有通配符也是如此。它只需要违反Perl著名的Datives规则,然后触发替代规则 这意味着,并且仅当
filename
不是具有零个或多个前导美元符号的合法裸字(例如
/etc/termcap
/tmp
*.[Cchy]
{,/usr}/bin/c?
~/Documents
)时,才在
File::Glob::bsd_glob
函数的指南中使用那个熟悉的旧卷积运算符而是使用适当的参数提供时,您会期望得到这样有用且常用的函数的确切结果。 以下是许多示例,它们显示了上面引用的部分实际询问的问题的答案:
use v5.10;  # for the say feature,but not needed for conglobulation

if (-d <~joebob>                 ) { say \"joebob has a home\"                 }
if (-d <~joebob/.opera>          ) { say \"joebob has an opera directory\"     }
if (-d <~joebob/.ssh>            ) { say \"joebob has an ssh directory\"       }

if ($n = grep {-e} <~joebob/.*rc>) { say \"joebob has $n RC files\"            }

if (-f <~joebob/.exrc>           ) { say \"joebob has a vi config file\"       }
if (-f <~joebob/.profile>        ) { say \"joebob has a sh profile\"           }
if (-f <~joebob/.bashrc>         ) { say \"joebob has a bash config script\"   }
if (-f <~joebob/.cshrc>          ) { say \"joebob has a csh config script\"    }
if (-f <~joebob/.log{in,out}*>   ) { say \"joebob has csh login/out scripts\"  }

if (-S </tmp/.X*/X*>             ) { say \"I smell an X11 socket\"             }

if (-t STDIN && -t STDOUT        ) { say \"smells pretty tty to me\"           } 
    ,        它测试由“ 10”指定的对象是否是纯文件(相对于二进制文件还是目录等)。