Thank you all for your suggestions. I find that provided that I change the port of MozRepl of one of the FireFox windows and assign the new port to the instance of WWW::Mechanize::Firefox, my script can control the Firefox window.
$mech = WWW::Mechanize::Firefox -> new (
launch => 'firefox',
repl => "localhost:$instance_port"
)