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

I know this module is outdated, but there is no URL module at cpan that someone recommended I use. All I want to do with the module is get all the page links and make the relative links "/foo/foo.htm" into absolute hrefs "http://www.foo.com/foo/foo2/foo/foo.htm" The code posted at CPAN on how to do this confused me, as there is a portion there where they use this
url("http:foo")->abs("http://host/a/b") ==> "http:foo" url("http:foo")->abs("http://host/a/b", 1) ==> "http:/host/a/foo"
it seems to me that would do the same thing, only there is extra commas and ) and 1. I dont think thats how it should be used and it seems that it should have made the address "http://host/a/b/foo" instead of what it did. Maybe im just not getting it. Thanks

Update

Okay, i went to cpan and did find the new one, apparently its URI with an I, i thought it was a lowercase L. Anyway, the code still doesnt make sense to me, but i will be using the new URI. Thanks again

20040227 Edit by BazB: Changed title from 'URL::URI'

Replies are listed 'Best First'.
Re: Converting relative URLs to absolute URLs
by waswas-fng (Curate) on Feb 27, 2004 at 05:10 UTC
    use URI::URL; print url("foo/test.html")->abs("http://www.sun.com/");'
    output:
    http://www.sun.com/foo/test.html

    make more sense now?


    -Waswas
Re: Converting relative URLs to absolute URLs
by zentara (Cardinal) on Feb 27, 2004 at 16:23 UTC
    Here's a more formal example from the archives:
    #!/usr/bin/perl use strict; use URI::URL; use constant BASE => 'http://www.pair.com/pair/support/index.html'; print "BASE is ", BASE, "\n\n"; while ( chomp(my $path = <DATA>) ) { &tryit( $path ); } sub tryit { my $relative = shift; my $path = URI::URL->new($relative)->abs( BASE, 1 ); print "$relative ->\n\t$path\n\n"; } __DATA__ http://www.pair.com /index.html https://www.pairnic.com/faq.m search/ library.html

    I'm not really a human, but I play one on earth. flash japh
      How do I use URI::URL to find out whether the link is relative or absolute? I am feeding it parsed links from simplelinkextor, $_ is the url of the page the links where taken from, and it's done in a loop. I understand the print url("foo/test.html")->abs("http://www.sun.com/");' part but how do I know that foo doesn't have a slash in front of it or that sun.com has a slash at the end? I can't find URI documentation, and there are no examples at CPAN. Thanks
        it is no problem, see
        use URI::URL; print url("foo/test.html")->abs("http://www.sun.com/"),"\n"; print url("/foo/test.html")->abs("http://www.sun.com/"),"\n"; print url("foo/test.html")->abs("http://www.sun.com"),"\n"; print url("http://www.sun.com/foo/test.html")->abs("http://www.sun.com +/"),"\n";