use URI, and in particular, use URI::URL; The documentation is pretty self explanatory.
update: somebody asked, so here is an example
perl -MURI::URL -e"$URI::ABS_REMOTE_LEADING_DOTS=1; die URI::URL->new(
+'../../foo.html','http://foo.org/a/b/c/')->abs"
http://foo.org/a/foo.html
#or if that doesn't work for you
# the path method will do nicely
use URI::URL;
$URI::ABS_REMOTE_LEADING_DOTS=1;
my $ur = new URI::URL('/foo/../bar/baz.html','http://foo.org/');
my @path = ();
for my $seg($ur->path_components()) {
next unless $seg;
if($seg eq '..') {
pop @path;
} else {
push @path, $seg;
}
}
print join '/',@path;
update:a slightly improved (interesting) example
#!/usr/bin/perl -w
#use strict;
use URI::URL;
$URI::ABS_REMOTE_LEADING_DOTS=1;
my $ur = new URI::URL('/foo/../bar/baz.html','http://foo.org/');
my $path = "";
my @path;
for my $seg($ur->path_segments()) {
unless($seg) { # it began with a /
$path .= "/";
next;
}
if($seg eq '..') {
pop @path;
} else {
push @path, $seg;
}
}
$\="\n";
$path .= join '/',@path;
print $path;
$ur = new URI::URL($path,'http://foo.org/');
print $ur->abs();
print $ur->canonical();
print 'this is interesting';
print URLo($path.'?ab=cd;ef=gh;#foy','http://foy.org');
# note no trailing slash on foy.org
sub URLo{
use CGI qw/ a b big /;
my $url = URI::URL->new(shift);
my $www = shift;
my $ret = a({-href => $www}, $www);
my $dir = "";
for $dir ($url->path_segments()) {
next unless($dir); # it happens, like on /foo
$www .= '/';
$ret .= big( b('/') );
$www .= $dir;
$ret .= a({-href => $www}, $dir);
}
if(($dir = $url->query()) and $dir) {
$www .= '?'.$dir;
$ret .= big(b('?'));
$ret .= a({-href => $www}, $dir);
}
if(($dir = $url->frag()) and $dir) {
$www .= '#'.$dir;
$ret .= big(b('#'));
$ret .= a({-href => $www}, $dir);
}
return $ret;
}
__END__
/bar/baz.html
http://foo.org/bar/baz.html
/bar/baz.html
this is interesting
http://foy.org/bar/baz.html?ab=cd;ef=gh;#foy
|