sub tee { my $file = shift; open FILE, "> $file" or die "Cannot open($file) for writing: $!\n"; return if my $pid = open STDOUT, "|-"; die "Cannot fork STDOUT: $!\n" unless defined $pid; print FILE while ; close FILE; exit; } tee('/some/file'); while ( <> ) { print; } close STDOUT; # be nice to tee's