dev > perl > modules > related_modules
document updated 11 years ago, on Jun 8, 2011

How do you 'tee' the STDOUT?


It works, but delays things by 1.0 seconds or more.

a snippet of Perl code

# replicate everything that goes to STDOUT to a file too  (uses a forked subprocess)
use autodie;
sub tee {
    my ($filename) = @_;
    open my $origstdout, '>&STDOUT';
    open my $fh, ">$filename";
    if (!open STDOUT, '|-') {
        ## child process
        while (sysread(STDIN, my $buffer, 1024)) {
            syswrite($origstdout, $buffer);
            syswrite($fh, $buffer);
    ## parent process continues on and returns
    close $origstdout;
    close $fh;

CPAN modules