bliako has asked for the wisdom of the Perl Monks concerning the following question:
Esteemed Monks,
I am trying to translate the following Node.js fragment to Perl (it takes a URL and sort of chdir to it, clever enough to remove endpoint if a file and query):
const url = require('url'); const x = 'http:/a.com/a/b/c/d/e.json?x=1&y=2'; const y = '../../../'; console.log(url.resolve(x, y));
result is : http:/a.com/a/
Now, if x='http:/a.com/a/b/c/d/e.json/?x=1&y=2'; (i.e. the path points to a "directory" rather than to a "file") the result is http:/a.com/a/b/ (as expected IMO)
I tried to do this with URI but failed:
use URI; my $x = 'http:/a.com/a/b/c/d/e.json?x=1&y=2'; my $y = '../../../'; my $u = URI->new($x); my $p = $u->path().'/'.$y; $u->path($p); print $u; <c> <p>With this result: <c>http:/a.com/a/b/c/d/e.json/../../../?x=1&y=2
The Node.js version seems to be able to understand that the last segment before the query is a "file" and removes it. Then it does the merging of the two paths including successful relative operations (../). What is the safest way to achieve that behaviour in Perl? The last resort would be to shell-out to Node.js ...
bw, bliako
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: URL resolve (operations on path etc.)
by haukex (Archbishop) on Jul 31, 2022 at 07:52 UTC | |
by bliako (Monsignor) on Jul 31, 2022 at 08:03 UTC | |
by ikegami (Patriarch) on Aug 01, 2022 at 22:50 UTC | |
by harangzsolt33 (Hermit) on Jul 31, 2022 at 11:31 UTC | |
Re: URL resolve (operations on path etc.)
by Your Mother (Archbishop) on Jul 31, 2022 at 17:27 UTC |