Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Turn relative url's to absolute with URI

by lampros21_7 (Scribe)
on Oct 23, 2005 at 23:19 UTC ( #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 = ""; my $uri = URI->new(""); 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 = ""; 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 = ""; my @links = qw/a b c/; @links = map {URI->new_abs($_, $base_url)->as_string()} @links; print Dumper(\@links);

    Both gives:

    $VAR1 = [ '', '', '' ];
Re: Turn relative url's to absolute with URI
by herveus (Parson) on Oct 23, 2005 at 23:35 UTC

    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;

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

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://502340]
Approved by herveus
Discipulus yaaawn..
[Corion]: DST does that to everybody...
Corion attempts to find who to address to better target his drive-by patches
[Corion]: This time it's for the Duktape embedded Javascript engine, which would be nice to have IMO, but doesn't compile on Windows currently
[Corion]: I imagine it would be fun to have WWW::Mechanize:: Duktape, but I haven't seen a web browser written in Javascript yet, except the ones distributed with PhantomJs

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2017-03-27 11:05 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (319 votes). Check out past polls.