我知道,如果一个档案是使用:
if(-d $filename)
but how can you test if it s not a directory?
我知道,如果一个档案是使用:
if(-d $filename)
but how can you test if it s not a directory?
你们是否想试图:
if (! -d $filename) ...
The result of -d
is, after all, something that can be treated as a boolean value, hence the logical not operator !
will work fine.
并且,如果你在“不是名录”之后再做些什么,请参看。 有许多事是,有时作为普通档案处理是没有意义的。
Keep in mind that s assuming you ve already validated that the filename exists. If you do a ! -d
on a non-existent filename, it will return true.
你们是否认为不存在任何东西是“不是目录”,而如果你想要确保存在,你也可以使用<代码>,这是一个哲学问题! -d,与-e
结合,例如:
if ((-e $filename) && (! -d $filename)) ...
如果是档案,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在第一次答复中明智地指出的那样)。
本着:
unless (-d $filename)
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) { ...
I am building a Web interface to monitor an embedded system. I have built a Perl script which runs remote commands and gathers output from that system. Now what I need is a Web interface which makes ...
How do I tell what type of value is in a Perl variable? $x might be a scalar, a ref to an array or a ref to a hash (or maybe other things).
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: "...
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 ...
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 ...
I would like to submit a form to a CGI script localy (w3c-markup-validator), but it is too slow using curl and apache, I want to use this CGI script more than 5,000 times in an another script. and ...
So I m running perl 5.10 on a core 2 duo macbook pro compiled with threading support: usethreads=define, useithreads=define. I ve got a simple script to read 4 gzipped files containing aroud 750000 ...
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 ...