Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to generate an index of all files in my web directories that are linked with a href and img tags) on any of my html pages.

How can I fix relative paths to full paths?

For example, I might have the following variables:
$dir = "/web/john/summertrip/";
$filename = "index.html";
$linkedfile = "../help.html";

What I want to do with this is take $dir.$linkedfile ("/web/john/summertrip/../help.html") and fix the path to ("/web/john/help.html").

Any suggestions on the easiest way to do this?

Thanks.

Replies are listed 'Best First'.
(crazyinsomniac) Re: Fixing paths
by crazyinsomniac (Prior) on Apr 05, 2002 at 20:39 UTC
    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

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"