Link to home
Start Free TrialLog in
Avatar of mdlittle
mdlittle

asked on

Exexcuting an external program from perl

I want to execute and external progam, get the exit code AND the output from the external program (but I DO NOT want the output from the external program to go to the console.) I have been struggling with system, exec and backticks and just can't get my desired result.

Avatar of mdlittle
mdlittle

ASKER

The output from the external program should be copied to a scalar (i know it will only be one line).

ASKER CERTIFIED SOLUTION
Avatar of Mindo
Mindo

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
Where is the return code from the executed program? I checked $? but it only give me the return code of open and not the executed program.

My mistake. It does work as requested. Thanks for your quick response.
To check the return code you must do:

$status = system("vi $myfile");

or

$status = system("vi",  $myfile);

or

$status = system($program, $arg1, $arg);
die "$program exited funny: $?" unless $status == 0;

If you want to know both the return status and program output, do the following:

$status = system("program > filename");
die "$program exited funny: $?" unless $status == 0;

# here the output of the program is in the file "filename". You can open and view it or print it.

I don't think there's another solution.
After running you "open" example I looked at $? and the exit code looked correct to me. I tested the running program both with correct params and one that I knew would return a result other than 0.

I think it works. When I ran it correctly I got a 0 in $? and when I ran it with incorrect params ( one i know would cause a 2 or 3 ) $? showed a 2.
Ok then, the $? error code makes the work.