Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to URL resolve (operations on path etc.) by bliako

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (4)
As of 2024-04-20 02:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found