对 perl 中一个非常简单的 if / else 语句感到困惑
print "content-type: text/html \n\n"; #The header
$file = "newtext.txt";
if (unlink($file) == 0) {
print "File deleted successfully.";
} else {
print "File was not deleted.";
}
这是我从 tizag 获取的一些代码。我不明白的部分是 true 的 bool 值为 1,而 false 为 0。那么为什么当我成功删除文件时我正在检查它是否返回 0?
print "content-type: text/html \n\n"; #The header
$file = "newtext.txt";
if (unlink($file) == 0) {
print "File deleted successfully.";
} else {
print "File was not deleted.";
}
This is some code I picked up from tizag. The part I don't understand is that the bool value for true is 1, and false is 0. So why is it when I successfully delete the file I'm checking to see if it returns a 0?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这似乎是一个错误..来自 perldoc perlfunc:
如果返回值为0,则删除了0个文件。
正确的写法是:
That seems like a mistake.. from perldoc perlfunc:
If the return value is 0, you deleted 0 files.
The correct way to write it would be:
事实上,我认为这是一个错误。来自 perldoc for unlink:
可能是自动驾驶仪上的 shell 编程。
Actually, I think that's a bug. From the perldoc for unlink:
Probably a shell programming on autopilot.
该代码段可能只是错误的,因为
unlink
返回数字已成功删除文件。The snippet can simply be wrong, cause
unlink
returns the number of files successfully deleted.好吧,别介意我的第一反应。这是来自取消链接的 perldoc:
证明不存在标准之类的东西。不幸的是,这也证明无论谁编写了该代码,都有一个错误。就我个人而言,我预计 0 意味着成功。如果您查看 C 系统调用
unlink
,零表示成功。完全疯狂,我告诉你。OK, nevermind my first response. This is from the perldoc for unlink:
Proof that there is no such thing as a standard. Also proof that whomever wrote that code, unfortunately has a bug. Personally I would have expected 0 to mean success. If you look at the C syscall
unlink
, zero means success. Utter madness, I tell you.