Like someone said above, image comparision is not an easy subject. You're best bet is to write it in C, and call it from perl.
To pre-process the images (before you compare), you should look into color normalization. It's a simple formula actually(the image is a NxM matrix or 2d array).
foreach pixel {
(pixel - min)*255/max
}
If you've got RGB you'll have to do that for each of the color values.
After you do that, you'll probobly wanna do edge detection using a
laplachian filter. After this it gets really tricky, but learning those two things will get you pointed in the right direction.