Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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

Replies are listed 'Best First'.
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 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 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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://16572]
Approved by root
[ambrus]: "you are far more likely to be squished between two other dudes" => wait, really? aren't most planes, weighted by people traveling on it, so narrow that there are only two or three seats on each side of the one corridor, so only one third of the people ge
[ambrus]: t squished between two other people?
holli is about to take the first hit since a month and this weed is strong shit. Should my head explode, someone call 911.
[ambrus]: And even when I traveled on a big plane for a long flight, it had 3 seats, then corridor, then 4 seats, then corridor, then 3 seats, so less than half of the people sit between two other dudes.
[1nickt]: you are missing an entire element, which is that your base ticket price only gets you a middle seat. If you want to be window or aisle you must pay, or gamble that you'll be assigned there at ...
[1nickt]: ... flihght time (hoping that not enough others have paid and there are some "good" seats to be allotted.

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (10)
As of 2017-12-18 13:39 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (486 votes). Check out past polls.