How to create an absolute url from two components in Perl?

Suppose I have:

my $a = "http://site.com";
my $part = "index.html";
my $full = join($a,$part);
print $full;
>> http://site.com/index.html

What do I need to use how jointo make my snippet work?

EDIT: I'm looking for something more general. What to do if it aends with a slash, but partbegins with one? I'm sure someone covered this in some module.

+3
source share
5 answers

I believe you are looking for a URI :: Split , for example:

use URI::Split qw(uri_join);
$uri = uri_join('http', 'site.com', 'index.html')
+10
source
use URI;
URI->new("index.html")->abs("http://site.com")

will create

"http://site.com/index.html"

URI-> abs will take care of the proper path union, following your uri specification,

So

URI->new("/bah")->abs("http://site.com/bar")

will create

"http://site.com/bah"

and

URI->new("index.html")->abs("http://site.com/barf")

will create

"http://site.com/barf/index.html"

and

URI->new("../uplevel/foo")->abs("http://site.com/foo/bar/barf")

will create

"http://site.com/foo/uplevel/foo"

alternatively, there is an abbreviation in the URI namespace that I just noticed:

URI->new_abs($url, $base_url)

So

URI->new_abs("index.html", "http://site.com")

will create

"http://site.com/index.html"

..

+3

"", .

my $a = "http://site.com";
my $part = "index.html";
my $full = "$a/$part";
print $full;
>> http://site.com/index.html

Update:

. CPAN , .

, . , - . URI, , , , . File::Spec .

my $a = 'http://site.com';
my @paths = qw( /foo/bar foo  //foo/bar );

# bad paths don't work:
print join "\n", "Bad URIs:", map "$a/$_", @paths;

my @cleaned = map s:^/+::, @paths;
print join "\n", "Cleaned URIs:", map "$a/$_", @paths;

, $path = /./foo/.././foo/../foo/bar;, . , cannonical path File::Spec.

/ URI, , ( , , ) URL- , URI , , , .

+2

Java java.net.URL, URI - , ( URL Subversion):

http://site.com/page/index.html
 +  images/background.jpg
=>  http://site.com/page/images/background.jpg

Perl:

use URI;
my $base = URI->new("http://site.com/page/index.html");
my $result = URI->new_abs("images/background.jpg", $base);
+1

Source: https://habr.com/ru/post/1724190/


All Articles