I'm confused about the path you're taking here (pun intended). Are you still working on Creating a bash script "on the fly" from a few days ago? If so, then I have to say I think you're creating yourself an XY Problem. I've already pointed you to:

Also, I'm not sure what you're testing with (index($link,"/") == -1)? Note links to directories don't need to end oncontain a slash; you won't know what the link is pointing to until you check the filesystem. (Update: Looking at your code again, I see a couple more ways in which certain symlinks would break it.)

But what if the link is relative (for example ../../abc)?

You're discovering one of the many intricacies of resolving symlinks. As explained in the documentation of File::Spec's canonpath: "If /foo on your system is a symlink to /bar/baz, then /foo/../quux is actually /bar/quux, not /quux as a naive ../-removal would give you."

Sorry, but based on my interpretation of everything above, I would strongly suggest that you first learn more about symlinks and handling filenames before trying to implement this. Even though I did a lot of research and testing when I wrote the relink script, and I trust my own code, I'm still not sure I've fully understood every little detail and edge case that could happen. It's not an easy topic.

But anyway, I suggest that even before that, maybe you could explain why the above solutions are not acceptable to you, and why you need to reinvent this particular wheel? I'm sure we could come up with a better solution for your actual problem.

If you're still working on Creating a bash script "on the fly", i.e. you're just trying to replicate a directory structure, then could you explain why a solution such as rsync, or simply copying over the links, is not sufficient? (You do know you can get most *NIX tools to not follow symlinks, right?)

Minor edits.


In reply to Re: Getting all subpaths from a path by haukex
in thread Getting all subpaths from a path by ovedpo15

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.