2011-06-23 11 views
3

に-dテスト演算子を使用すると、私はこの声明に出くわした:私が誰かの古いコードをつもりですperlの

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

if(-d $tmpStr){ 
    printf("1"); 
}else{ 
    printf("2"); 
} 

私は-dが何をするかについて困惑している...任意のヘルプ?次の文字列がディレクトリである場合

+0

クイック回答ありがとうございます!それは理にかなっているようです! – Andrew

答えて

13

-dはtrueを返します。

-X in perlfuncを参照してください。

+5

これはstat(2)のシステムコールを実行し、成功した場合は、inodeタイプでディレクトリを調べます。システムコールは、他の理由で失敗する可能性がありますが、これは通常考えられません。たとえば 'glarch'が本当にディレクトリであったとしても、' bar'が現在のユーザに設定された実行ビットを持っていなければ、 '-d"/foo/bar/glarch "'はfalseになります。 '$!'か '%!'を確認してください。私が推測するすべてのものをサポートするシステムのために、@ tchrist @ – tchrist

+0

があります。 (;しかし良い点。 – Qtax

+3

re: "$' 'をチェックする:-dがundefを返す場合のみ – ysth

1

そのパスがディレクトリかどうかをテストします。それは文字列の比較ではありません。

これは文字列の比較です。

"Hello" ne "world" 
4

ディレクトリが存在するかどうかをチェックします。下記のようなfile test operationsがあります。

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) 
関連する問題