What color would you like that wheel you're about to reinvent? chrome? wood? rubber?
There's no need to push this down into the perl side of things, when Apache has facilities built-in that can handle this at the request level. Look into mod_rewrite, and more specifically, the URL Rewriting Guide, specifically the "Blocked Inline-Images" section about 80% of the way down. I use this method on several of my sites, and it works wonderfully (it's not just for images).
To summarize:
<Directory /path/to/domain.org/>
AllowOverride None
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://domain.org/.*$ [NC]
RewriteRule .*\.(png|gz|zip|)$ http://domain.org/no.png [R]
</Directory>