Check out
YAFP (Yet Another Filtering Proxy). It's written in Perl and based on an example by
merlyn.
I found this using "filtering proxy perl" as keywords on Freshmeat.
This does not answer your question, because you want a hosting-side solution where post-parsing (probably) inserts the code. However, it can help you get rid of pop-ups client-side.
You might not realise it, but your question is not Perl related (btw, it's Perl, not PERL).
2;0 juerd@ouranos:~$ perl -e'undef christmas'
Segmentation fault
2;139 juerd@ouranos:~$