对 perl 中一个非常简单的 if / else 语句感到困惑

发布于 2024-11-13 00:52:40 字数 304 浏览 2 评论 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.";
}

这是我从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

穿透光 2024-11-20 00:52:40

这似乎是一个错误..来自 perldoc perlfunc

删除文件列表。论成功,
它返回文件的数量
删除成功。失败时,它
返回 false 并设置 $! (错误号):

如果返回值为0,则删除了0个文件。

正确的写法是:

if (unlink($file)) { print "Success!" }
else { print "Unlink failed: $!" }

That seems like a mistake.. from perldoc perlfunc:

Deletes a list of files. On success,
it returns the number of files it
successfully deleted. On failure, it
returns false and sets $! (errno):

If the return value is 0, you deleted 0 files.

The correct way to write it would be:

if (unlink($file)) { print "Success!" }
else { print "Unlink failed: $!" }
感情废物 2024-11-20 00:52:40

事实上,我认为这是一个错误。来自 perldoc for unlink

成功时,它返回成功删除的文件数。失败时,它返回 false 并设置 $! (错误号)

可能是自动驾驶仪上的 shell 编程。

Actually, I think that's a bug. From the perldoc for unlink:

On success, it returns the number of files it successfully deleted. On failure, it returns false and sets $! (errno)

Probably a shell programming on autopilot.

你的呼吸 2024-11-20 00:52:40

该代码段可能只是错误的,因为 unlink 返回数字已成功删除文件。

The snippet can simply be wrong, cause unlink returns the number of files successfully deleted.

耀眼的星火 2024-11-20 00:52:40

好吧,别介意我的第一反应。这是来自取消链接的 perldoc:

删除文件列表。成功后,它会返回成功删除的文件数。失败时,它返回 false 并设置 $! (错误号)

证明不存在标准之类的东西。不幸的是,这也证明无论谁编写了该代码,都有一个错误。就我个人而言,我预计 0 意味着成功。如果您查看 C 系统调用 unlink,零表示成功。完全疯狂,我告诉你。

OK, nevermind my first response. This is from the perldoc for unlink:

Deletes a list of files. On success, it returns the number of files it successfully deleted. On failure, it returns false and sets $! (errno)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文