Use the
Expect module.
Expect is originally a tool built on-top of TK; designed
to interact programatically with text applications (command line and full-screen)
emulating a user. The perl Expect module encapsulates much
of that functionality and is perfect for the kind of task
you are trying to do.