To begin, I am curious, is this a homework question? If so, we cannot provide you code for this as it is against this sites posting policy.
As for what you are wanting to do, I can say that I find the
WWW::Mechanize module to be particularly useful when needing to interact with websites. You may want to check it out, unless someone else has any idea.
Regards,
Jeff