Solved

Exexcuting an external program from perl

Posted on 2000-02-20
7
279 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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 …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

759 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

19 Experts available now in Live!

Get 1:1 Help Now