tags:

views:

76

answers:

1

I'm new to perl but was wondering if anyone know of a script that was similar to the following PHP version which works great!

private function resolve_href ( $base, $href ) {
    if (!$href)
        return $base;
    $rel_parsed = parse_url($href);
    if (array_key_exists('scheme', $rel_parsed))
        return $href;
    $base_parsed = parse_url("$base ");
    if (!array_key_exists('path', $base_parsed))
        $base_parsed = parse_url("$base/ ");
    if ($href{0} === "/")
        $path = $href;
    else
        $path = dirname($base_parsed['path']) . "/$href";
    $path = preg_replace('~/\./~', '/', $path);
    $parts = array();
    foreach ( explode('/', preg_replace('~/+~', '/', $path)) as $part ) { 
        if ($part === "..")
            array_pop($parts);
        elseif ($part!="") 
            $parts[] = $part;
    }
    $dir =  ( ( array_key_exists('scheme', $base_parsed)) ? $base_parsed['scheme'] . '://' . $base_parsed['host'] : "" ) . "/" . implode("/", $parts);
    return str_replace( "\/", '', $dir );
}

Any help is much appreciated

+9  A: 

See URI:

#!/usr/bin/perl

use strict; use warnings;
use URI;

my $u = URI->new_abs('../foobar', 'http://foo.com/bar/poo/');
print $u->canonical;

Output:

http://foo.com/bar/foobar
Sinan Ünür
:-) converting now!
Phil Jackson
@Sinan Ünür: +1. It is awesome how fast and accurate your responses are.
drewk
doesn't really do what I was asking but thanks anyway.
Phil Jackson
Genius! Thankyou
Phil Jackson
@Sinan Ünür is there a away to use this to determine the url of a 301 redirect i.e. `$response->header('Location');` may return `./mod_rewrite_url` or maybe `http://domain.com` ?
Phil Jackson
@Phil That is a different question (related to LWP) which I think has been asked on Stackoverflow. If you can't find it, post it as a separate question.
Sinan Ünür
I'll look into it and then post if needed. Thanks for everything.
Phil Jackson