Posted on 2003-10-29
Last Modified: 2010-03-04

What i am trying to do is to use "system" to invoke a program and catch the execution result. However, any output and error message produced by the program during execution are redirected to a file. The following script does not work because the last two fields are also passed to the program. SO how can fix this? Thanks.

$result = system "program args", ">temFile", "2>&1";

if ($result) {
 print "error\n";
 }else {
 print "ok\n";

Question by:rongz
  • 2

Expert Comment

ID: 9641529
You don't necessarily need to use system. If all you're concerned about is the output of the program, you can try:

#!perl -w

use strict;

open FH, "$program_name |";

while (<FH>) {

The pipe in the open command will cause Perl to read the standard output of the program being run.

Author Comment

ID: 9642263
Actually, what I want is that user can just see if the program is executed sucessfully or not, and output (including error message) from the program is redirected to a file.

Accepted Solution

ultimatemike earned 50 total points
ID: 9642299
Make the command string that is being executed contain the redirects, rather than as parameters. That seems to work.

Try this:

$result = system "program args >temFile 2>&1";

if ($result) {
 print "error\n";
 }else {
 print "ok\n";

Expert Comment

ID: 9648061
You might need the perl variable:-


Which contains the exit code of the program you ran.

$rc=`program args >&temFile`;
print "It returned $?\n";

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: (…

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now