Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exexcuting an external program from perl

Posted on 2000-02-20
7
Medium Priority
?
295 Views
Last Modified: 2010-03-05
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.

0
Comment
Question by:mdlittle
  • 4
  • 3
7 Comments
 

Author Comment

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

0
 
LVL 2

Accepted Solution

by:
Mindo earned 200 total points
ID: 2540319
$output = `program args`; # collect output into one multiline string

@output = `program args`; # collect output into array, one line per element

or you can do:

open(README, "program args |") or die "Can't run program: $!\n";
while(<README>)
{
  $output .= $_;
}
close(README);
0
 

Author Comment

by:mdlittle
ID: 2540329
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.

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mdlittle
ID: 2540347
My mistake. It does work as requested. Thanks for your quick response.
0
 
LVL 2

Expert Comment

by:Mindo
ID: 2540355
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.
0
 

Author Comment

by:mdlittle
ID: 2540369
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.
0
 
LVL 2

Expert Comment

by:Mindo
ID: 2540383
Ok then, the $? error code makes the work.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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 …
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

885 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