http://www.perlmonks.org?node_id=966060

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

I am trying to use below command, but not sure why I am not able to store the output value of system command to variable "cmd", as system command output is displaying on screen very fine.

my $cmd = system("find . type f -name \"fname*\") my $cmd = `ls -l`;

Replies are listed 'Best First'.
Re: Using System command
by thomas895 (Deacon) on Apr 19, 2012 at 23:38 UTC

    That's because system only returns 0 if the command was successful. See system. To write the response to a filehandle, use pipe or write a pipe using open.
    Be very careful when just putting user input directly into the command -- it's a big security risk and -T will produce errors.

    ~Thomas~
Re: Using System command
by mbethke (Hermit) on Apr 19, 2012 at 23:41 UTC

    That's because system does not return the STDOUT output of the command but the exit status, see "perldoc -f system". Having two separate constructs for the same job would be redundant.

    If the backticks do what you need, why not just use them?

    Edit: LOL, guess the number of possible wordings was quite limited ...

      Thanks for clarifying on this.