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";

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
rongzAuthor Commented:
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.
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";

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You might need the perl variable:-


Which contains the exit code of the program you ran.

$rc=`program args >&temFile`;
print "It returned $?\n";
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.