in reply to Re: How to get the unique canonical path of a given path?
in thread How to get the unique canonical path of a given path?

Hi, thanks for the suggestion but not quite. I don't want to resolve any links in the path. I thought of adding:
unless ($path =~ /^\//) { $path = catdir(getcwd,$path); }
What do you think?

Replies are listed 'Best First'.
Re^3: How to get the unique canonical path of a given path?
by Fletch (Bishop) on Jul 13, 2022 at 21:58 UTC

    I don’t think you’re going to find much off the shelf because the posix/*nix way of "canonical" path is to expand out and replace symlinks (see Pathname Resolution). I think closest you might get would be rolling something using File::Spec and splitdir to unroll (maybe with glob for tilde handling beforehand), process for dot(s), then concatenate the remaining elements back.

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re^3: How to get the unique canonical path of a given path?
by hippo (Archbishop) on Jul 13, 2022 at 21:26 UTC
    I don't want to resolve any links in the path.

    Well then, I don't understand what you mean because AFAICS realpath does not do that*. If you can provide an SSCCE (ideally in the form of a test) showing precisely what output you want for a given input and how realpath fails in that regard perhaps we can guide you further.

    * Update: yes, it does do that - see replies.


    🦛

      realpath absolutely does resolve links.

      $ perl -MPath::Tiny -E 'say path("/home/ikegami/tmp")->realpath' /tmp/ikegami

      There's also an example in the OP.

      It's the only way to safely remove «..». Well, technically you only need to resolve links to the left of the «..».

        Ah yes, you are quite correct both in the result and the rationale. I'm left wondering what on earth it was I tried with this yesterday to come to the opposite, erroneous conclusion.


        🦛