I see two hard parts here: resolving relative links and avoiding loops. For resolving relative links, File::Spec's rel2abs might help; notice you can give a base directory where the relative path should start from. For avoiding loops, many OS's just keep a count and give up after some number of symlinks; you could also track specific links in a hash and stop chasing links if you end up someplace you've been before.

I'm not sure whether you care about parent directories being symlinks, too. If so, that's a third hard part, though it's certainly manageable.

Here's some code to chase symlinks and print out what it finds, using rel2abs to deal with relative links and a hash to check for loops. I think it could be adapted to your needs.

#!/usr/bin/perl use warnings; no warnings 'uninitialized'; use strict; use File::Spec qw(rel2abs); use File::Basename; chaselink($ARGV[0]); sub chaselink { my %seen = (); my $chase; $chase = sub { my($f,$d)=@_; print "\nChasing link '$f' in '$d'\n"; my $l = readlink($f); if (!defined($l)) { print "$f is not a link.\n"; return undef; } print "Relative link: $l from $d\n"; my $a = File::Spec->rel2abs($l,$d); print "Absolute link: $a\n"; if ($seen{$a}) { print "Found loop, giving up\n"; return undef; } $seen{$a}=1; $chase->($a,dirname($a)); }; $chase->(@_); }

In reply to Re: Following symlinks manually by sgifford
in thread Following symlinks manually by Tanktalus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.