in reply to image in <img> tag is not refreshed

Your problem has nothing to do with Perl.

Use a different file name for your .jpg each time. This not only solves your problem but also the race condition that exists if two users are accessing your site at the same time.

It does create a need to garbage collect your tempory image directory from time to time.

Alternatively make the <img> tag point to another CGI script that creates the JPEG entity on-the-fly and never saves it as a file.

Did I mention that this has nothing to do with Perl?