It might be simpler to get
WWW::Mechanize::Shell to write a
WWW::Mechanize program for you. It's pretty slick. Mechanize makes a "virtual browser" that hides most of the boring details of a browsing session behind the scenes. The Shell lets you drive it with a series of easy-to-type commands, and can optionally keep a browser synced with the current rendering. When you're done, you say "spit out a program", and it spits out a Mechanize program to repeat the same steps.
-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.