Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Turn relative url's to absolute with URI

by lampros21_7 (Scribe)
on Oct 23, 2005 at 23:19 UTC ( [id://502340]=perlquestion: print w/replies, xml ) Need Help??

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

Hi to the fellow monks

I have an array of url's that i have extracted from a website which means there are relative url's and absolute ones in my array. I have found the URI module which seems better than the URI::URL one, and am trying to make all my url's absolute. This is my code:

my $base_url = "http://www.perlmonks.com/"; my $uri = URI->new("http://www.perlmonks.com"); foreach(@links) { # This loop is to make all the relative url's absol +ute $uri = URI->new_abs( $_, $base_url ) } print "@links \n";

I have tried this but to no avail. The elements of the array still come out in the same way as they used to, some relative and some absolute. From the URI documentation there doesn't seem another good way of doing this. I think this should be pretty simple as i have the absolute url but i can't seem to do it. Any ideas? Thanks.

Replies are listed 'Best First'.
Re: Turn relative url's to absolute with URI
by pg (Canon) on Oct 23, 2005 at 23:47 UTC

    Two things:

    • You never saved anything back to your @links.
    • There was no need to create that $uri variable unless you need it somewhere else.
    use Data::Dumper; use URI; use strict; use warnings; my $base_url = "http://www.abc.com/"; my @links = qw/a b c/; for my $index (0 .. $#links) { $links[$index] = URI->new_abs($links[$index], $base_url)->as_strin +g; } print Dumper(\@links);

    You can use map instead of for loop:

    use Data::Dumper; use URI; use strict; use warnings; my $base_url = "http://www.abc.com/"; my @links = qw/a b c/; @links = map {URI->new_abs($_, $base_url)->as_string()} @links; print Dumper(\@links);

    Both gives:

    $VAR1 = [ 'http://www.abc.com/a', 'http://www.abc.com/b', 'http://www.abc.com/c' ];
Re: Turn relative url's to absolute with URI
by herveus (Prior) on Oct 23, 2005 at 23:35 UTC
    Howdy!

    Your code doesn't seem to do what you want it to do...

    This begs for a map, as seen in this untested code:

    my @newlinks = map { URI->new_abs( $_, $base_url ) } @links;

    yours,
    Michael
Re: Turn relative url's to absolute with URI
by ikegami (Patriarch) on Oct 24, 2005 at 03:04 UTC

    You're assigning to the wrong variable. Change
    foreach (@links) { $uri = URI->new_abs( $_, $base_url ) }
    to
    foreach (@links) { $_ = URI->new_abs( $_, $base_url ) }</p

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://502340]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (7)
As of 2024-04-18 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found