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

This code should IMHO substitute "/style.css" with "http://freshmeat.net/style.css". i.e. add an hostname to all hrefs (if they don't have one).
however, it does not work. Any ideas anyone ?
sub blahify(\$$) { my $bla = ${shift()}; my $url = shift; my @x = (); my $host = $url; my $there = $url; $host =~ s%(http://[^/]*?/).*%$1%i; $there=~ s%(http://[^/]*?/.*/).*%$1%i; while($bla =~ m%(href="[^"]*")%ig) { my $boo = $1; next if $boo=~ m%href="http://%i; if( $boo =~ m%href="(/.*?)"%i) { push @x, $1; next; } printf STDERR "bad href: $boo\n"; } foreach (@x) { print "killing $_\n"; $bla =~ s/$_/$host$_/i; } my $foo = $x[0]; print "ALERT\n"if($bla=~/$foo/); return $bla; }

Replies are listed 'Best First'.
Re: adding hostnames to absolute links
by chromatic (Archbishop) on Oct 21, 2000 at 00:27 UTC
    Seems like a lot of extra work:
    $doctext =~ s/(a href=")([^"]+)/$1 . link_check($2)/eg; sub link_check { my $link = shift; # $hostname is a global or block lexical return $link if ($link =~ /^http://$hostname/); $link =~ s!(http://[^/]+)?!$hostname!; return $link; }
    Disclaimer: this is untested.

    Update: Changed the second $1 on the RHS to $2, as originally intended.

Re: adding hostnames to absolute links
by merlyn (Sage) on Oct 21, 2000 at 02:17 UTC
Re: adding hostnames to absolute links
by Fastolfe (Vicar) on Oct 24, 2000 at 19:00 UTC
    I'm not 100% sure what problem you're trying to address with this, but you may just have to change the <base> tag for the page, which would have the effect of changing the base location for all of your relative URL's in the HTML itself:
    <base href='http://www.example.com' src='http://www.example.com' >