Seems like this came up just a day ago...
$? is being interpolated as a Perl variable and its value is what's making it to the shell. You probably just need to escape the sigil:
$expect->send( "grep xyz *; echo RET_VALUE:\$?" );
...or if there's no need for interpolation anywhere else in your string literal, use single (non-interpolating) quotes:
$expect->send( 'grep xyz *;echo RET_VALUE:$?' );
Update: I just realized why it seemed so familiar: You already asked a question that got you a nearly identical answer, yesterday, in this thread: Incorrect value 0 reported in command execution return value variable $?, when trying with expect->send command. Why? ...At least you got answers that confirmed one another. Seek a third opinion and you'll really have covered your @$$ ;)
Dave
In reply to Re: Issue while retrieving the unix command return status when executed with perl CPAN module expect.pm
by davido
in thread Issue while retrieving the unix command return status when executed with perl CPAN module expect.pm
by Davewhite
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |