Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

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
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2016-07-23 18:00 GMT
Find Nodes?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?

    Results (221 votes). Check out past polls.