Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

by tybalt89 (Monsignor)
on Jul 16, 2022 at 14:28 UTC ( [id://11145549]=note: print w/replies, xml ) Need Help??


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

Are these answers correct?

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11145493 use warnings; for my $path ( qw( /a/b/c/d/../../../e /a/../b/./c//d ../invalid_do_not_change ./tmp /tmp/../../../tmp A//B A/B/ A/./B A/foo/../B ) ) { local $_ = $path; 1 while s{ /+(?=/) | # multiple /// ^/..(?=/) | # stay at root /\z | # remove trailing / (?<=/)(?!\.\./)[^/]+/\.\./ | # remove 'name/../' (?<![^/])\./ # remove ./ }{}x; printf "%30s -> %s\n", $path, $_; }

Outputs:

/a/b/c/d/../../../e -> /a/e /a/../b/./c//d -> /b/c/d ../invalid_do_not_change -> ../invalid_do_not_change ./tmp -> tmp /tmp/../../../tmp -> /tmp A//B -> A/B A/B/ -> A/B A/./B -> A/B A/foo/../B -> A/B

Replies are listed 'Best First'.
Re^2: How to get the unique canonical path of a given path?
by pryrt (Abbot) on Jul 16, 2022 at 15:47 UTC

    Nevermind; ignore my objection from the spoiler. I just tried the cd /tmp/../../../tmp and found it did go to the /tmp directory. And perl -le 'use autodie; open my $fh, ">", "/tmp/../../../tmp/worked.txt"; print {$fh} "it worked";' works as expected as well, so apparently that weird notation is a perfectly-valid syntax. Sorry.

      It works because on a *nix system the root directory is pretty much defined by " .. is the same as . "

Re^2: How to get the unique canonical path of a given path?
by ikegami (Patriarch) on Jul 17, 2022 at 18:58 UTC

    [Nothing to see here]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11145549]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-25 14:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found