Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

how do I run a unix command inside a perl script, save it in a variable and use that variable later. thanks, jj

Replies are listed 'Best First'.
Re: running unix commands under perl
by Beatnik (Parson) on Jun 28, 2002 at 21:14 UTC
    backticks come to mind (qx//)...
    my $foo = qx/ls -l/;
    That's ofcourse for ANY type of OS :) You might want to call a command with the full path

    Greetz
    Beatnik
    ...Perl is like sex: if you're doing it wrong, there's no fun to it.
Re: running unix commands under perl
by krisahoch (Deacon) on Jun 28, 2002 at 21:17 UTC
    Try using the Shell package.
    use Shell;
    I was required to do NIS password authentication in my webpage, and here is the script that I used to figure it out.
    #!/bin/perl -w use strict; use diagnostics; use Shell; print "Enter your username: "; my $USER = <STDIN>; chop ($USER); print "Password: "; my $PASS = <STDIN>; chop($PASS); # ypmatch is a linux shell command. my $str = ypmatch("$USER", "passwd"); chop($str); my @Tmp = split(":", $str); my $Tr = $Tmp[1]; my $salt = substr($Tr, 0, 2); my $ENCPASS = crypt($PASS,$salt); print "-------------------------------------------------\n"; print "String from yp: $str\n"; print "Transformed to: $Tr\n"; print "Salt for pass: $salt\n"; print "Your encrypted Password: $ENCPASS\n"; print "Password from ypcat: $Tr\n";

    Now mind you, this was only a test. I certainly don't do any code that willy-nilly displays a person's password to the word.

    Kristofer Hoch.
Re: running unix commands under perl
by Marza (Vicar) on Jun 28, 2002 at 21:08 UTC

    Hmmm a homework assignment?

    For Unix commands take a look at system() and using ` ` to execute a command such as `hostname`