How can I send Perl output to a both STDOUT and a variable?

前端 未结 8 1045
耶瑟儿~
耶瑟儿~ 2020-12-03 22:20

I would like to send the output from a command to both STDOUT and to a variable. I want to combine:

my $var = `some command` ;   
system( \'some command\' )          


        
8条回答
  •  鱼传尺愫
    2020-12-03 22:38

    You could use the IO::String module to select() STDOUT to a string and then call system() to run the command. You can collect the output from the IO::String handle. This effectively does what the backtick syntax does.

    So to gather command output realtime, run the system() command asynchronously through fork() or some other means and poll the handle for updates.

    EDIT: Per OP, it turns out this approach does not work. select() doesn't affect system() calls.

    Also, IO::String has been replaced with new open() syntax since Perl 5.8 that does the same function.

提交回复
热议问题