Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Determine absolute URL given a relative URL and the location where it was found.

by ehdonhon (Curate)
on Oct 30, 2003 at 20:41 UTC ( #303405=snippet: print w/replies, xml ) Need Help??

So you are scraping URL's out of a web page (using a CPAN module, of course), and they take all sorts of different forms: some absolute, some relative. The relative ones may or may not begin with a slash, etc.. Your job is to figure out where all of these links really go. URI::URL is your friend.

use strict;
use URI::URL;
use constant BASE => '';

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";


Here's the output:

BASE is ->

/index.html -> ->

search/ ->

library.html ->
  • Comment on Determine absolute URL given a relative URL and the location where it was found.
  • Download Code
Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://303405]
[Discipulus]: if you have a jpeg data: or as wise Corion said IM ca n read directly your handle
[Corion]: Meh. I get the feeling I should post a call-to-action for people to test their modules with the upcoming 5.26 (or 5.25.latest), as it seems that the smoke testers don't really weed out modules that fail without . in @INC
[Corion]: But without instructions on how to easily test things yourself, I don't think such a call to arms is helpful
[Corion]: (this post on / p5p makes it seem to me that smoke testers seem to have a blind spot there)
[Corion]: Hmmm. Maybe setting $ENV{PERL5LIB}=" some path without dot"; perl Makefile.PL; make test already is enough to locally test your module...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2017-03-27 08:02 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (317 votes). Check out past polls.