Link to home
Start Free TrialLog in
Avatar of stakor
stakorFlag for United States of America

asked on

Called Program Status

I have a perl script that calls another program in such a way that it records the results of the command in a string. i.e.

$program_results = `program $ARGV[0] `;

I would like to have perl check the status of the command, for instance did it complete with a non-error of zero - or did it error out somehow. Is there a way of checking the status of  a program that is run while capturing the output?
Avatar of sjklein42
sjklein42
Flag of United States of America image

To get the return status, use the system function instead of back-ticks:

$program_results = system("program $ARGV[0] >nul: 2>nul:");

Open in new window


I added code to prevent other output produced by program from also being returned, but this is up to you.
Avatar of stakor

ASKER

I don't fully understand.  I am hoping to capture everything that program would normally print to screen in $program_results. I would also like to have the ability to run a check on another variable to see the exit status. I didn't know if there was a variable built into perl that would show the exit status of the last command or something along those lines.

If I understand the program snippet above, $program_results is the exit variable?
ASKER CERTIFIED SOLUTION
Avatar of clockwatcher
clockwatcher

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of stakor

ASKER

So if everything ran correctly, $program_retval should be equal to zero?

if ($program_retval != 0) {
   <freak_out>
};

clockwatcher gets it right with $?

Learn something every day.