in reply to Re^2: Unlinking a file when the file doesnt exist
in thread Unlinking a file when the file doesnt exist

Unfortunately, $! is garbage in that example.

From 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):

One of the examples at the perldoc page for unlink shows using $! with warn() in a foreach loop. Why shouldn't $! be used in this case?

  • Comment on Re^3: Unlinking a file when the file doesnt exist

Replies are listed 'Best First'.
Re^4: Unlinking a file when the file doesnt exist
by ikegami (Patriarch) on Mar 23, 2011 at 15:47 UTC

    I didn't say anything that contradicts that. It's perfectly fine to use $! immediately after unlink returns an error as I showed in the very post you quoted.

    In moritz's code, $! isn't being used immediately after unlink returns an error. In decreasing likeliness of occurrence,

    • It's sometimes used after stat (-e) successfully returns false. (Can change $!.)
    • It's sometimes used after stat (-e) returns an error. (Always sets $!.)
    • It's sometimes used after unlink returns success. (!!!)