I am feeling silly, so try this:
# Takes a filehandle and filename and has output to that handle go
# to STDERR and to that filename. It will work even if the handle
# being tied is STDERR itself. No escaping is done for the filename,
# caveat programmer
sub tee_to_stderr {
my $file = shift || die "No filename passed\n";
my $ip = open (STDERR, "| perl") || die "Cannot pipe to Perl: $!";
my $fh = select(STDERR);
$| = 1;
select($fh);
print STDERR qq(
unless(open (OUT, ">$file")) {
print STDERR "Cannot write to $file: \$!\n";
kill 9, getppid();
}
select(STDERR);
\$| = 1;
select(OUT);
\$| = 1;
while (<>) {
print OUT;
print STDERR;
}
);
print STDERR "\n__END__\n";
}
(The attempt to kill the parent won't work in Windows, otherwise this gives you one very convoluted way to do something that should be simple.)
The idea is to not just redirect STDERR, but also to have it still going to STDERR so that you get your log and they still see their own errors.
The right way to do this should use IO::Tee, but the problem is that one of the filehandles you want to tee is STDERR. This gets us into the same recursion that you saw in Filehandle Filter. I don't have the energy at the moment to offer a good solution, though I am sure there is one and that I will be very irritated with myself when I see it...
UPDATE
I am indeed irritated. Fastolfe pointed out that you can always dup a filehandle:
open(NEW, ">&OLD") or die "Cannot dup OLD: $!";
So dup STDERR, then proceed to do what you want. |