Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Running External Commands

by Anonymous Monk
on Jun 06, 2000 at 13:46 UTC ( #16572=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What is the most economical way to run an external program and gather its output into a varible (Scalar or Array) in Perl Backticks, system or someother way that i haven't tried yet

Comment on Running External Commands
RE: Running External Commands
by le (Friar) on Jun 06, 2000 at 14:00 UTC
    system() doesn't gather the output of the executed program, it just returns the exit status. With backticks, you can collect the output. You could also open a pipe from a command:
    open(PIPE, "command |") or die $!; while(<PIPE>) { # ... do something }
Re: Running External Commands
by Michalis (Pilgrim) on Jun 06, 2000 at 16:33 UTC
    The best way (to my knowledge) to do that is to use backticks, assigning the result to a scalar (the output doesn't have to be a single line, or a single word or anything. For example:
    $result=`/bin/tail -15 /var/adm/messages`;
    You should be very careful though, if you decide to pass to the command options from the user's input (for example through a CGI script). But that's a fact, either you use backticks or system.
      Since you've almost mentioned taint checking, I figured I'd jump in with a link to a taint checking FAQ.

      CGI/Perl Taint Mode FAQ

      Taint checking, for those that don't know, is how you solve the "be very careful" issue that Michalis mentioned.

RE: Running External Commands
by merlyn (Sage) on Jun 06, 2000 at 21:41 UTC
Re: Running External Commands
by sh1tn (Priest) on Feb 24, 2005 at 11:50 UTC
    You may want to see the following items:
    perldoc -f system
    perldoc -f exec
    perldoc -q command


Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://16572]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (18)
As of 2014-07-23 14:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (144 votes), past polls