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

I've got a bunch of symlinks in a directory which (intentionally) don't have as much info in the filename as the original files. I need to find out the name of the original file, but I can't see how to do this in perl. I also would like to make the solution as portable as possible (it may have to run on windows)

I can see a similar topic came up in the Broken symlinks discussion, but the only solution that displayed the destination links was using perl2find and wasn't portable.

I've had a bit of a poke around on cpan, but I can't see anything appropriate, and I'd rather not use a non-standard lib anyway.

Does anyone have any suggestions?

Replies are listed 'Best First'.
Re: tracing symlinks
by Boldra (Curate) on May 08, 2001 at 14:09 UTC
    Ok the absurdity of my question only just hit me.

    Symlinks on windows???

    Go on laugh. I'll use system(ls) like cLive ;) suggested in the chatterbox.
        That would work, except that it does "die" on a system without symlinks.

        I think , however, a combination of this and the '-l' test would do the trick, wrapping the readlink function in an eval (even suggested as such in the eval POD to handle platform specific features).

        Something along the lines of:

        my $filename = "something"; while ( -l $filename ) { eval { $filename = readlink $filename } }


        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
Re: tracing symlinks
by Cybercosis (Monk) on May 08, 2001 at 19:23 UTC
    WELL... I'd go with readlink() on *NIX systems, but if it MUST run on windows and cope with windows links, then if you open the .lnk file, there should be a complete path to whatever you're looking in about 200-300 bytes in. If you just snarf the whole file (it's a links, it shouldn't be all that big) and then regex the bugger (UNTESTED!):
    $file =~ /([list|of|acceptable|drive|letters]\:.+)/

    That ought to (sorta) work.

    ~Cybercosis