在perl中使用-d test运算符

poy*_*poy 7 perl

我正在浏览一些旧的代码,我发现了这个声明:

$tmpStr = "/some/file/location/";

if(-d $tmpStr){
   printf("1");
}else{
   printf("2");
}
Run Code Online (Sandbox Code Playgroud)

我对-d的作用感到困惑......有什么帮助吗?

Qta*_*tax 15

-d 如果以下字符串是目录,则返回true.

-Xperlfunc.

  • 它执行stat(2)系统调用,如果成功,则检查目录的inode类型.但是,系统调用可能由于其他原因而失败,人们通常不会想到这些原因.例如,如果`bar`没有为当前用户设置执行位,``-d"/ foo/bar/glarch"`将为false,即使`glarch`确实是一个目录.之后检查`$!`甚至'%!`以获得更好的细节. (6认同)
  • re:"Check` $!`":仅当-d返回undef时 (3认同)

小智 5

它检查目录是否存在.还有更多可用的文件测试操作,如下所示:

   1. -r File is readable by effective uid/gid.
   2. -w File is writable by effective uid/gid.
   3. -x File is executable by effective uid/gid.
   4. -o File is owned by effective uid.
   5.
   6. -R File is readable by real uid/gid.
   7. -W File is writable by real uid/gid.
   8. -X File is executable by real uid/gid.
   9. -O File is owned by real uid.
  10.
  11. -e File exists.
  12. -z File has zero size (is empty).
  13. -s File has nonzero size (returns size in bytes).
  14.
  15. -f File is a plain file.
  16. -d File is a directory.
  17. -l File is a symbolic link.
  18. -p File is a named pipe (FIFO), or Filehandle is a pipe.
  19. -S File is a socket.
  20. -b File is a block special file.
  21. -c File is a character special file.
  22. -t Filehandle is opened to a tty.
  23.
  24. -u File has setuid bit set.
  25. -g File has setgid bit set.
  26. -k File has sticky bit set.
  27.
  28. -T File is an ASCII text file (heuristic guess).
  29. -B File is a "binary" file (opposite of -T).
  30.
  31. -M Script start time minus file modification time, in days.
  32. -A Same for access time.
  33. -C Same for inode change time (Unix, may differ for other platforms)
Run Code Online (Sandbox Code Playgroud)