This code works for me against a debian etch machine -- should be very similar to another flavor of Linux, though. (You also might consider the Net::SSH module..!):
#!/usr/bin/perl
use strict;
use warnings;
use Net::Telnet;
my $host = "somehost";
my $username = "test";
my $password = "password";
my $t = new Net::Telnet;
$t->dump_log('trace.log'); # detailed packet trace
$t->open($host) or die "could not connect to $host: $!\n";
$t->print($username);
$t->waitfor('/ssword[:] $/');
$t->print($password);
$t->waitfor('/\$ *$/');
$t->print("cp a b");
$t->getline(); # read prompt
my $stdout = $t->getline();
print "stdout $stdout\n";
$t->print("echo \$?");
$t->getline(); # read prompt
my $exitcode = $t->getline();
print "exit $exitcode\n";
exit($exitcode);