As strange as it seems, I couldn't find here a code that cleanly modifies HREF attributes in A starting tags in a HTML page.
So here's one that I whipped up quickly to answer a question on fr.comp.lang.perl
It surely could be easily improved to include other links (<script>, <img src="...">, etc.), but you get the idea...
The only (slight) caveats is that the 'a' starting tag is always lowercased and the order of the attributes are lost. But that should not matter at all.
Also, this code won't print 'empty' attributes correctly (though I can't think right now of any empty attributes that are legal with 'a')
To use this script, you have to modify the $new_link variable, and then call the script with the URL of the page to be modified. Every <a href="..."> will have the $new_link added at the start of the href, and the old URL will be properly escaped.
It is probably useless as is, but with a minimum of tweaking, you can easily do what you want.
Actually, it might be a good thing to turn this little script into a module where you would only have to do the URL munging, without worrying about the whole parsing stuff...