You would need to run a Java applet on the browser in order to be able to send commands to an HTML page without replacing it. Your perl program wouldn't be a CGI, but a standalone server interacting with the Java applet. For this you can use the
NetServer::Generic module instead of dealing with sockets yourself.
On making the applet execute your javascript function you should check java.sun.com for documentation. I'm sure it's not hard to do what you want.