How can I hook into Perl's print?

前端 未结 5 1196
情话喂你
情话喂你 2020-11-29 03:59

Here\'s a scenario. You have a large amount of legacy scripts, all using a common library. Said scripts use the \'print\' statement for diagnostic output. No changes are

5条回答
  •  萌比男神i
    2020-11-29 04:14

    You can use Perl's select to redirect STDOUT.

    open my $fh, ">log.txt";
    print "test1\n";
    my $current_fh = select $fh;
    print "test2\n";
    select $current_fh;
    print "test3\n";
    

    The file handle could be anything, even a pipe to another process that post processes your log messages.

    PerlIO::tee in the PerlIO::Util module seems to allows you to 'tee' the output of a file handle to multiple destinations (e.g. log processor and STDOUT).

提交回复
热议问题