Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

stat weirdness?

by z3d (Scribe)
on Feb 25, 2004 at 14:57 UTC ( [id://331667]=perlquestion: print w/replies, xml ) Need Help??

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

Looking for some answers to my "HUH?!?" of the day. I have a script (don't we all?) that needs to perform an action on some files/directories. No biggie. But what I've found is that if there are symlinks in the tree, it performs them against the symlink targets. For instance, say i have /tmp/mike/cat, symlinked to /tmp/dogfood. It seems that perl (5.6.1 and 5.8.2, tested both) will return true on both the -l and the -d tests when scanning /tmp/mike and hitting cat. Is this just a quirk of perl? (Would expect perl to say it's either a dir or a sym, not evaluate both the source and the target). I can explain better if needed, hate posting this, but it *seems* like a bug in the implimentation of the stat tests.

"I have never written bad code. There are merely unanticipated features."

Replies are listed 'Best First'.
Re: stat weirdness?
by Abigail-II (Bishop) on Feb 25, 2004 at 15:12 UTC
    Is this just a quirk of perl?
    No. It's not just a quirk of perl. Perl does what the 'test' utility does as well (and many shells have this baked in). I don't have the POSIX standard handy at the moment, but I expect that POSIX requires this behaviour as well.


      To quote the page of the POSIX standard pertaining to stat:

      If the named file is a symbolic link, the stat() function shall continue pathname resolution using the contents of the symbolic link, and shall return information pertaining to the resulting file if the file exists.

      So the behavior that the OP is experiencing should be expected.

      The first rule of Perl club is - use Perl
      ith rule of Perl club is - follow rule i - 1 for i > 1

      Abigail, antirice, thank you both. Now I'm wishing there was a "remove this node thank you" button. Not sure why I never ran into this aspect before - definitely an ignorance issue on my part. I appreciate the quick responses :)

      "I have never written bad code. There are merely unanticipated features."
Re: stat weirdness?
by ysth (Canon) on Feb 25, 2004 at 16:55 UTC
    To work on the symlink instead of following it, do an lstat instead of stat. To use the tests, do an lstat and then e.g. -d _
      They should make it easy to find this stuff, like perldoc -f or something (that is a knock against me, btw). Thanks ysth,

      "I have never written bad code. There are merely unanticipated features."

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://331667]
Approved by Theo
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-07-21 00:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found

    erzuuli‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.