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

前端 未结 8 1049
耶瑟儿~
耶瑟儿~ 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:45

    You can do this through a file handle as well. Not as elegant as some solutions, but it would likely work. Something along the lines of:

    my $foo;
    open(READ, "env ps |");
    while () {
        print;
        $foo .= $_;
    }
    print $foo;
    close(READ);
    

提交回复
热议问题