system() rc hosed?

G

Greg G

I've got some code that looks like this:

@args = ("/usr/local/bin/myprogram ", " -a ", $param1, " -b ",
$param2);
print STDERR @args, "\n";
$rc = 0xffff & system (@args);

It *should* work, but it is patently ignoring the return code from
myprogram. In fact, the return code is 255, which seems to translate to
"command failed". If I capture the output from stderr and run it from
the command line, it works fine.

In fact, this works fine:

$capture = `@args`;
print STDOUT $capture,"\n";

I get the output from the myprogram.

Any ideas what's happening to me here?

-Greg G
 
G

Gary E. Ansok

I've got some code that looks like this:

@args = ("/usr/local/bin/myprogram ", " -a ", $param1, " -b ",
$param2);
print STDERR @args, "\n";
$rc = 0xffff & system (@args);

It *should* work, but it is patently ignoring the return code from
myprogram. In fact, the return code is 255, which seems to translate to
"command failed". If I capture the output from stderr and run it from
the command line, it works fine.

When you pass system() a list of arguments, as you are doing here,
they are taken exactly as the program to run and the arguments to
pass to it.

So your system() command is looking for a program called
"/usr/local/bin/myprogram " -- without the quotes, but _with_
the trailing space. Also, the first and third arguments to that
program are going to have leading and trailing spaces.

If you want to print out the arguments nicely, I would use
print STDERR join(' ', @args), "\n";
(You could play with the $" variable, which works out to the same thing.)

However, I often prefer something like
print STDERR map(">$_<", @args), "\n";
especially if trailing spaces or newlines might be involved.

Gary
 
G

Greg G

Gary said:
When you pass system() a list of arguments, as you are doing here,
they are taken exactly as the program to run and the arguments to
pass to it.

So your system() command is looking for a program called
"/usr/local/bin/myprogram " -- without the quotes, but _with_
the trailing space. Also, the first and third arguments to that
program are going to have leading and trailing spaces.

If you want to print out the arguments nicely, I would use
print STDERR join(' ', @args), "\n";
(You could play with the $" variable, which works out to the same thing.)

However, I often prefer something like
print STDERR map(">$_<", @args), "\n";
especially if trailing spaces or newlines might be involved.

Thanks! That seems to have cleared things up nicely.

-Greg G
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top