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

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!


The easiest way uses backticks or qx():

my $value = qx(ls);
print $value;

The output is similar to the ls.


My answer does not address your problem. However, if you REALLY want to do directory listing, don't call system ls like that. Use opendir(), readdir(), or a while loop.

For example,

while (<*>){
    print $_ ."\n";
}

In fact, if it's not a third-party proprietary program, always try to user Perl's own functions.

Tags:

Perl

System