?
Solved

Called Program Status

Posted on 2011-02-24
5
Medium Priority
?
475 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:stakor
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:sjklein42
ID: 34976755
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.
0
 

Author Comment

by:stakor
ID: 34976797
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?
0
 
LVL 25

Accepted Solution

by:
clockwatcher earned 2000 total points
ID: 34976825
Try $?.

$program_results = `program $ARGV[0] `;
$program_retval = $? >> 8;

perldoc perlvar:

       $?      The status returned by the last pipe close, backtick ("``")
               command, successful call to wait() or waitpid(), or from the
               system() operator.  This is just the 16-bit status word
               returned by the traditional Unix wait() system call (or else is
               made up to look like it).  Thus, the exit value of the
               subprocess is really ("$? >> 8"), and "$? & 127" gives which
               signal, if any, the process died from, and "$? & 128" reports
               whether there was a core dump.
0
 

Author Comment

by:stakor
ID: 34976860
So if everything ran correctly, $program_retval should be equal to zero?

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

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34976902
clockwatcher gets it right with $?

Learn something every day.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question