in reply to Expect question

I don't remember ever having to be that paranoid about spaces, as the (cleaned up and therefore not tested) code below shows.
print $expect_instance "$username}\r"; ( $which, $why, $match, $before, $after) = $expect_instance->expect( $ +timeout, 'Password:'); if( ! $which ) { myerror( 'send the password:'.$why );
$before is what came before the match, $match is what it matched on (useful if you passed in more than one match candidate), and $after is what came after the match. Note that $before and $after are strings and they may contain newlines.

I think you are getting hung up on the spaces when either they do not matter or you could just deal with them later on in your perl code. Though it's been a few years since I touched Expect.pm so maybe it is more rigid than before.