paperlined.org
dev > perl > modules > related_modules
document updated 13 years ago, on Jun 8, 2011

How do you 'tee' the STDOUT?

/usr/bin/tee

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);
        }
        exit;
    }
    ## parent process continues on and returns
    close $origstdout;
    close $fh;
}

CPAN modules