Solved

Exexcuting an external program from perl

Posted on 2000-02-20
7
281 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 50 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

776 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