I am trying to write the output generated by a simple command "dir" to a file. Can you please help me do that?

Here is what I am doing --

system ("dir");

this will run dir command on my D: drive now, I want to write this ouput (in short whatever is displayed on cmd) to file. How can I do that?

you mean you want to save the information displayed on your
command prompt to an msword or notepad file?

Next time I would prefer to see some effort to solve your programming reqirements before posting code. Here is one way it can be done:

my $dir = qx{dir};
open (my $OUT, ">", 'dir.txt') or die "$!";
print $OUT $dir;
close $OUT;

Sure.... thanks so much for the help. Actually I am a beginner of per and I was using only system command and was not aware of qx.


As a beginner you should read the documentation for functions and modules and such things as you try and use them. You would notice that system() does not return any application output back to your perl script.

If you don't know how to read the perl documentation locally, you can read it online:


the version you have installed locally may not be the latest version of perl, which the online documentation is. But most of the core functions are still the same.

This question has already been answered. Start a new discussion instead.