You have to reverse the return list, otherwise you'll refer to the previous value of
$?:
sub executeCommand_wrong {
my $command = join ' ', @_;
($? >> 8, $_ = qx{$command 2>&1});
}
sub executeCommand_correct {
my $command = join ' ', @_;
($_ = qx{$command 2>&1}, $? >> 8);
}
my $command = 'echo -n ciao ; false';
my ($status, $output) = executeCommand_wrong ($command);
print "[$output] -> [$status]\n";
($output, $status) = executeCommand_correct($command);
print "[$output] -> [$status]\n";
__END__
[ciao] -> [0]
[ciao] -> [1]
If you cannot live without having
$status as the first returned value, just use reverse:
sub executeCommand {
my $command = join ' ', @_;
reverse ($_ = qx{$command 2>&1}, $? >> 8);
}
Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')
Don't fool yourself.