Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

how to invoke unix commands in perl

by Anonymous Monk
on Feb 08, 2001 at 20:00 UTC ( #57193=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how to invoke unix command( say "wc -l " to count lines ina file) in perl

Comment on how to invoke unix commands in perl
Re: how to invoke unix commands in perl
by arhuman (Vicar) on Feb 08, 2001 at 20:04 UTC

    use backquote :
    $result=`wc -l /etc/passwd`;
    or use system or exec

      OOPS ! I can't modify it...

      Please use your usual doc (Site Library, Camel book, perldoc) to understand the implication in term of process forking, shell spawning, security...
      But most of all don't forget that spawning a shell is cpu/memory consumming and can often be avoided by few perl internal function...
      (Especially in this case of a wc -l)

Re: how to invoke unix commands in perl
by mirod (Canon) on Feb 08, 2001 at 20:08 UTC

    Read the doc! Perl comes with lots of docs. man perl on a unix box or use the Perl documentation that comes with the Activestate on Windows.

    perldoc system would work fine (make sure you read how to avoid shell interpolation if you run in an insecure context like CGI)

    Lastly my $var= `wc -l file`; will put the result of the command in $var.

Re: how to invoke unix commands in perl
by Boldra (Deacon) on Feb 08, 2001 at 20:21 UTC
    Just in case your problem is even more obvious than anyone else has realised:

    You need to be running your perl on a UNIX box!

    Otherwise your command gets passed to whatever OS you _are_ running ;)
Re: how to invoke unix commands in perl
by stefan k (Curate) on Feb 08, 2001 at 20:22 UTC
    There are several ways to invoke a programm. One of the main differences is in the returning value.
    • system("wc -l");
      Will call the given command and return the return value of that command. This is usually not what you want, because most of the times wc -l will mean that you want to get the number of lines back from that call and not if that call was successful or not.
    • $nol = `wc -l`
      The backticks call the command and return it's output into the variable (here $nol. In this case this will be what you want.
    • Another way of doing this is to use
      $nol = qx/wc -l/;
      (mnemonic: qx quote execute). I think is just the same as the backquotes (at least I don't know any difference)
    • Of course there are other ways (exec,fork) that behave different with respect to processes, but I don't know much about this
    Hope this helps...
Re: how to invoke unix commands in perl
by coolmichael (Deacon) on Feb 10, 2001 at 11:52 UTC
    why bother with unix at all?

    perl -e "while(<>){} print qq($.\n);" filename
      But how to invoke setenv or source commands in perl These are built-in shell commands. -Bhanu
        Those particular commands would be useless anyway because they only take effect for the current shell which would be exited anyway once the OS call is done.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2014-12-29 13:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (188 votes), past polls