Calling a command from Perl, you need to see the output

I need to call some shell commands from perl. These commands take quite a long time to complete, so I would like to see their output, awaiting completion.

The system function does not give me any output until it is complete.

The exec function gives output; however, it exits the perl script from this point, which I did not want.

I am on Windows. Is there any way to do this?

+4
source share
1 answer

Backticks or qx command, run the command in a separate process and return the result:

 print `$command`; print qx($command); 

If you want to see intermediate output, use open to create a handle to the output of the command and read it.

 open my $cmd_fh, "$command |"; # <--- | at end means to make command # output available to the handle while (<$cmd_fh>) { print "A line of output from the command is: $_"; } close $cmd_fh; 
+12
source

Source: https://habr.com/ru/post/1332136/


All Articles