in reply to writing a PerlIO::via module to log stderr?

I wonder if what you really want to do is to use Tie::FileHandle::MultiPlex. What I would do here is open a new filehandle that duplicates STDOUT or STDERR, then close STDOUT or STDERR, open the logfile(s), and then tie STDOUT or STDERR to the multiplex of the duped filehandle and the logfile.

But more info about PerlIO::via is always a good thing :-)

  • Comment on Re: writing a PerlIO::via module to log stderr?

Replies are listed 'Best First'.
Re^2: writing a PerlIO::via module to log stderr?
by DrWhy (Chaplain) on Mar 15, 2005 at 21:56 UTC
    Thanks Tanktalus; that will do the trick. Here's a toy script that demonstrates it's use:

    Update: The first version of this only looked like it worked. This version really does.

    #!/usr/bin/perl use Tie::FileHandle::MultiPlex; open (REALERR, ">&STDERR"); tie *STDERR, Tie::FileHandle::MultiPlex, *A, *REALERR, *B; open(A,'>>','testA') or die "testA: $!\n"; open(B,'>>','testB') or die "testB: $!\n"; warn "test message $$";

    This will print out 'test message 2314' or some such to the screen (stderr) as well as appending the same line to files named testA and testB.

    I'd still like to learn more about perlio layers, though the need is not as urgent right now; I'd still be interested in a whitepaper-ish thing on perlio layers if anyone knows of one.

    --DrWhy

    "If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."