English 中文(简体)
如果档案是NOT,则在Perl的目录中进行测试。
原标题:Test if file is NOT a directory in Perl

我知道,如果一个档案是使用:

if(-d $filename)

but how can you test if it s not a directory?

最佳回答
问题回答

如果是档案,BUT不是目录,则需要简单地结合2次测试(没有单一测试):

if (-e $file && !-d $file) { # a file but not a directory }

请注意:

  • 在得到应有的尊重的情况下,Paxdiablo的答复对问题的措辞方式是错误的(纯粹<代码>!-d没有满足用户的要求,即如果随机的物品/载体不是名录,NOT是不是目录的档案。 E.g. !-d NO_SUCH_FILE属实。

  • Greg的答复完全取决于原始用户是否打算在其“并非目录的档案”的定义中列入非解释性档案(例如,象征链接、名称的管道等)。 如果它们打算将所有这些特别的“档案”包括在内,那么Greg s的回答也是错误的,因为“-f”排除了这些档案(正如Greg在第一次答复中明智地指出的那样)。

The -f operator tests to see whether something is a "regular file". This excludes directories, but also excludes such things as pipes and device nodes.

if (-f $filename) { ...




相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签