detecting links

J

jsowers

Is there any simple way to determine if a reference returned via readdir
is a soft- or hardlink rather than a directory or filename.
None of my perl books seem to show any way of telling.

Thanks
 
U

Uri Guttman

j> Thanks, but not quite the answer. readlink will return the name of
j> the directory/file which the link points to. I need something
j> equivalent to the '-d' to determine if an item is a directory.
j> Basically, I want to avoid following the link when walking a
j> directory tree.

perldoc -f -X

uri
 
T

Tad McClellan

Thanks, but not quite the answer.


Huh? It looks like the answer to me...

readlink will return the name of the
directory/file which
the link points to.


Or undef if it is not a symbolic link.

I need something equivalent to the '-d' to determine
if an item is a directory.


That is not the same question, and you already know the answer
to that question.

Basically, I want to avoid following the link when walking a directory
tree.


Select those that are not symbolic and are directories (untested):

my @dirs_not_links = grep { !readlink() and -d } @directories;
 
S

Steve Grazzini

Joe Smith said:
Simply test for symlink _before_ testing for directoryness.

if (-l $item) {
(-d _) and print "Symlink to a directory\n";
^^^^
This will never be true.
(-f _) and print "Symlink to a file\n";
(-e _) or print "Broken symlink; target does not exist\n";
^^^^
And this will always be true.

You'll need to do a regular stat() inside the block -- otherwise
the special _ filehandle will refer to the results of the lstat()
and you know already that the *symlink* exists and is neither
file nor directory.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top