Solved

Exexcuting an external program from perl

Posted on 2000-02-20
7
286 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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

617 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