Confused by the very simple if / else statement in perl

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 the code I took from tizag. The part that I don’t understand is that the bool value for true is 1 and false is 0. So why, when I successfully delete the file, I check if it returns 0?

+3
source share
4 answers

This seems to be a mistake .. from perldoc perlfunc :

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

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

The correct way to write:

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

Actually, I think a mistake. From perldoc to unlock :

. false $! ()

.

+2

, unlink .

+1
source

Well, never mind, my first answer. This is from perldoc for unlink:

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

Proof that such a standard does not exist. Also, the proof that someone wrote this code, unfortunately, has an error. Personally, I would expect that 0 would mean success. If you look at S syscall unlink, zero means success. Absolute madness, I tell you.

0
source

All Articles