in reply to Porting Perl 5.6 to Perl 5.8 issue with self-tie

The second tee doesn't work because you didn't save your original STDOUT, so tee doesn't get it.
#!/usr/bin/perl -wl $| = 1; # unbuffered output print $$; for(1,2){ open SAVEOUT,">&STDOUT"; my $pid = open STDOUT,"| tee log_$_"; open STDERR, ">&STDOUT"; print "this goes to STDOUT (log_$_)"; warn "This goes to STDERR (log_$_)"; sleep 1; print "killing $pid"; kill 15, $pid; close STDOUT; close STDERR; open STDOUT,">&SAVEOUT"; } __END__ Name "main::SAVEOUT" used only once: possible typo at tee.pl line 4. 14781 this goes to STDOUT (log_1) This goes to STDERR (log_1) at tee.pl line 8. killing 14782 this goes to STDOUT (log_2) This goes to STDERR (log_2) at tee.pl line 8.
$ cat log_1 log_2 this goes to STDOUT (log_1) This goes to STDERR (log_1) at tee.pl line 8. this goes to STDOUT (log_2) This goes to STDERR (log_2) at tee.pl line 8.

Of course, if you need to restore STDERR later, save that too. See open. Note that if you remove the sleep 1 line in the above code you will likely not see any output, because then the tee process gets killed too soon.

--shmem

update: fixed inconsistencies stemming from putting a oneliner into a file..

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: Porting Perl 5.6 to Perl 5.8 issue with self-tie
by fwashbur (Sexton) on Dec 11, 2006 at 22:25 UTC
    Hey shmem,

    This looks like what I'm looking for! I had tried combos of saving STDOUT and killing the tee, but not in this configuration. I'll give this a try. This could explain why I was having trouble with STDERR as well.

    Thanks - Rick

      Hi shmem,

      I tried this scheme on my system. It ran into the same issue I had when trying the example out of the O'Reilly book. The first log works great, but the second log attempt results in the following error at the first print statement: tee: write error: Invalid argument

      Are you running this on 5.8? Could there be an issue with my 'tee'?

      Thanks - Rick

        Hmm. Yes, I'm running 5.8, and no, I guess it's not an issue with tee. Strange.

        Next I would do is wrapping tee with a shell script which traces tee, to see what it does, specifically open/closes. Log the fileno() of your STDERR and STDOUT to the apache error log (real STDERR ;-) as well as the fileno() of your redirected handles. Ah, and you could omit the close STDOUT - just re-open it. I didn't check the return value of close (shame on me) which you should do.

        Could you provide a condensed version of the code you are running?

        --shmem

        _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                      /\_¯/(q    /
        ----------------------------  \__(m.====·.(_("always off the crowd"))."·
        ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}