Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

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

Replies are listed 'Best First'.
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 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 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 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 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.
        how about with system("what ever you want to do in unix"); ?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://57193]
[Corion]: Yeah, I also went a more roundabout way, just to find that the solution had been with Perl all along! ;)
[tye]: perhaps the "less secure" comment was motivated by old versions of getlogin() and trolled through the 'last' log trying to match your TTY. On modern Unix, I believe getlogin() just returns a fundamental bit of identity from your process.
[tye]: (Because every thing you do has that tag available for auditd.)
[tye]: Though it is certainly true that you should not use getlogin() for auth().
[LanX]: tye: just a tip for the next time, I found interesting things there...
[LanX]: like shmem commands : shmctl, shmget, shmread, shmwrite...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (6)
As of 2017-06-23 18:58 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.