in reply to POSIX::S_ISDIR() with $stat->mode values from Windows vs. Linux
Perl's stat() emulation for Windows returns at least three different values, see Re^3: Inline.pm and untainting.
You DO NOT NEED the POSIX macro emulations and those ugly decimal notation of file modes if you just want to test if a name belongs to a directory or a plain file, use perls -X functions, especially -d to test for a directory and -f to test for a plain file.
print "$somename is a real directory" if -d($somename); print "$somename is a real plain file" if -f($somename);
Note that those function usually test the link target of a symlink, except of course for -l. If you want "real" files or directories and not symlinks, either test explicitly for a symlink or use lstat(). Think twice before insisting on "real" files or directories. Most people expect symlinks to be completely transparent for applications, so don't violate that expectation except for a very good reason (like running with root privileges in a public writeable directory like /tmp).
# way 1: explicit test print "$somename is a real directory" if !-l($somename) && -d _; print "$somename is a real plain file" if !-l($somename) && -f _; # way 2: using lstat() lstat($somename); print "$somename is a real directory" if -d _; lstat($somename); print "$somename is a real plain file" if -f _;
Note that using a single underscore as argument reuses the struct stat of the last lstat()/stat()/-X, saving slow system calls.
Update:
For the stat() return values on Windows, and the reason why stat() is emulated that way unter Windows, see Re^3: Inline.pm and untainting.
Alexander
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: POSIX::S_ISDIR() with $stat->mode values from Windows vs. Linux
by jakobi (Pilgrim) on Oct 06, 2009 at 18:25 UTC |