How can I store Perl's system function output to a variable?

karthi_ms picture karthi_ms · Mar 10, 2010 · Viewed 61.9k times · Source

I have a problem with the system function. I want to store the system functions output to a variable.

For example,

system("ls");

Here I want all the file names in the current directory to store in a variable. I know that I can do this by redirecting the output into a file and read from that and store that to a variable. But I want a efficient way than that. Is there any way .

Answer

abubacker picture abubacker · Mar 10, 2010

No, you cannot store the values of the ls output , since system always execute the command as a child process , so try with backtick `command` which executes the command in the current process itself!