Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Using System command

by perlatbest (Initiate)
on Apr 19, 2012 at 23:27 UTC ( #966060=perlquestion: print w/replies, xml ) Need Help??
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 (Chaplain) 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.

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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://966060]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2018-02-25 03:54 GMT
Find Nodes?
    Voting Booth?
    When it is dark outside I am happiest to see ...

    Results (312 votes). Check out past polls.