malevy has asked for the wisdom of the Perl Monks concerning the following question:

Greetings all, Aside from the file not existing, are there any other conditions that would cause the -e file test to return a false for a file that did actually exist? TIA -Mike
  • Comment on What conditions cause the -e file test to return false

Replies are listed 'Best First'.
Re: What conditions cause the -e file test to return false
by ikegami (Patriarch) on Feb 27, 2009 at 16:17 UTC

    On my system, man -2 stat enumerates

    EACCES
    Search permission is denied for one of the directories in the path prefix of path. (See also path_resolution(2).)
    EBADF
    filedes is bad.
    EFAULT
    Bad address.
    ELOOP
    Too many symbolic links encountered while traversing the path.
    ENAMETOOLONG
    File name too long.
    ENOENT
    A component of the path path does not exist, or the path is an empty string.
    ENOMEM
    Out of memory (i.e. kernel memory).
    ENOTDIR
    A component of the path is not a directory.

    And I don't see anything there for read failures (incl communication failures with remote volumes).

    perl -MErrno=ENOENT -le' $found = -e shift; die("stat: $!\n") if !defined($found) && $! != ENOENT; print($found ? "found" : "not found"); ' file
Re: What conditions cause the -e file test to return false
by Sinistral (Monsignor) on Feb 27, 2009 at 16:21 UTC

    One I thought would trip it up is doing a chmod 000 on a file owned by root and testing it existed using a non-root user. Nope, you still get the -e returning true. (Which does seem to be the desired semantics, since the file does exist.

    Update: Ahh, here's a case. If the user running the script cannot cd into the directory containing the file, then -e fails.

      Do chmod 000 on the directory containing the file and it won't be found for non-root users.
      $ perl -MErrno=ENOENT -le' $found = -e shift; die("stat: $!\n") if !defined($found) && $! != ENOENT; print($found ? "found" : "not found"); ' foo/bar not found $ touch foo/bar $ perl -MErrno=ENOENT -le' $found = -e shift; die("stat: $!\n") if !defined($found) && $! != ENOENT; print($found ? "found" : "not found"); ' foo/bar found $ chmod a-x foo $ perl -MErrno=ENOENT -le' $found = -e shift; die("stat: $!\n") if !defined($found) && $! != ENOENT; print($found ? "found" : "not found"); ' foo/bar stat: Permission denied
Re: What conditions cause the -e file test to return false
by Jenda (Abbot) on Feb 27, 2009 at 23:55 UTC

    Most often the simple fact that the path is not what you think it is. Maybe the variable doesn't contain what you are SURE it must. Maybe it contains a little more than you think. A space at the end, a newline maybe. Maybe you forgot to double your backslashes (if you're using Windows). Maybe the current directory is not what you expected. If a -e $variable returns false while you think it should return true print "\$variable=>$variable<\ncwd=>" . getcwg() . "<\n";