要查看是否有文件中使用它之前就存在,我們可以使用:如何查看Perl中是否存在目錄?
if (-e "filename.cgi")
{
#proceed with your code
}
但如何indentify一個目錄存在與否?
要查看是否有文件中使用它之前就存在,我們可以使用:如何查看Perl中是否存在目錄?
if (-e "filename.cgi")
{
#proceed with your code
}
但如何indentify一個目錄存在與否?
使用-d
(full list of file tests)
if (-d "cgi-bin") {
# directory called cgi-bin exists
}
elsif (-e "cgi-bin") {
# cgi-bin exists but is not a directory
}
else {
# nothing called cgi-bin exists
}
作爲一個說明,-e
沒有文件和目錄進行區分。要檢查是否存在並且是純文件,請使用-f
。
如果您使用-d,那麼您不需要-e(-d爲不存在的目錄返回false)。 – 2010-12-20 04:53:50
@Peter - 雖然在技術上是正確的,但在生產代碼中,通常最好先執行-e後跟-d,這樣錯誤消息就可以更有針對性。就生產中的問題解決而言,「不存在」和「作爲非目錄存在」是有區別的。 – DVK 2010-12-20 15:00:51
大約一半的Perl問題涉及非常基本的主題。如果你讀了一些書,你可以節省一些時間。 [Perl語言入門④](http://oreilly.com.cn/book.php?bn=978-7-5641-0636-2),[學習Perl⑤](http://oreilly.com/catalog/ 9780596520113 /) – daxim 2010-12-20 08:49:36
我猜他花了很少的時間來問這些問題,並且他們是非常有用的搜索結果。不要成爲一個學習風格偏執。 – 2012-09-13 21:27:39
@Adam我認爲daxim的觀點只是OP的perl問題(包括這一個)都是非常基礎的,所以閱讀perl的概述可能會節省一些時間,並提出很多具體問題的答案很容易找到其他方式 – GreenGiant 2013-09-25 23:11:43