in reply to perl expect

$cmd = `ls`;

What is this line supposed to do? Have you printed $cmd to verify your assumption?

Maybe you wanted to use single quotes instead?

$cmd = 'ls';

See also Quote and Quote-like operators.