# Usage: $teed_fh = tee @output_handles; # Returns a new filehandle that, when written, copies to all @output_handles sub tee { use vars qw(*__TEE_FH); my $tfh = do { \local *__TEE_FH }; # XXX Should use IO::Handle instead defined( my $pid = open($tfh, "|-") ) or die "fork: $!"; return select((select($tfh), $| = 1)[0]) if $pid; for (@_) { select($_); $| = 1 } while( sysread(STDIN, my $block, 8192) ) { print $_ $block for @_ } kill 9,$$ or exit; # XXX Should use POSIX::_exit instead }