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

Does Path::Tiny::realpath do what you want?

$ perl -MPath::Tiny -E 'say path(q#../script.sh#)->realpath' /home/script.sh

🦛

Replies are listed 'Best First'.
Re^2: How to get the unique canonical path of a given path?
by ovedpo15 (Pilgrim) on Jul 13, 2022 at 18:31 UTC
    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?

      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.

      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 «..».