Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Called Program Status

Posted on 2011-02-24
5
Medium Priority
?
480 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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

636 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