gmpassos has asked for the wisdom of the Perl Monks concerning the following question:

I made an STDOUT redirection that handles automatically the heanders output. Soo I was testing all the possible behaviors and found a bug with $|.

(Just to remember, $| is the autoflush value of the selected output.)

The problem is that you can print to the HANDLER without select() it, soo the $| that PRINT() inside the TIEHANDLE class reads, isn't the autoflush value ($|) of it self, is the value of another selected output.

Soo, I need to get explicity the autoflush value ($|) of the tied handler. For that I was thinking to set the selected output to the TIEDHANDLE when inside the PRINT, and when going out of it reset to the previouse output, with something like that:

sub PRINT { ... my $sel = select("FOOBAR::STDOUT") ; my $autoflush_val = $| ; select($sel) ; ... }
But this is a big problem, since I can't know where the GLOB of my tied HANDLER lives (in the code FOOBAR::STDOUT), and even if I know I can't change the selected output when inside of PRINT, since I can't ensure that I really can reselect the previouse output ($sel), since some output aren't global or can be another tied handler. Soo, this approach will bring much more bugs!

Soo, there is a way to get the autoflush value of a tied handler without use $| ?
(Let's try to avoid XS hack, but any XS code is still welcome).

Update: Just to be explicity, I'm not talking about buffers! I'm just talking in a way to get the $| value, and ensure that this value is from my tied handle! What I will do with it (buffer system or not) is my bussiness.

Graciliano M. P.
"Creativity is the expression of the liberty".

Replies are listed 'Best First'.
•Re: How to get explicity the autoflush var ($|) of a TIED HANDLER?
by merlyn (Sage) on Dec 27, 2003 at 21:43 UTC
      I know that! (As my code show).

      Actually a tied handle just doesn't buffer automatically, since it has an autoflush value ($|) like any other output!

      My problem is not to make the buffer system, is the autoflush value, that exists in the tied handle and should be used if was implemented there!!!

      In other words, I'm not talking, and haven't talked, about buffers!

      Graciliano M. P.
      "Creativity is the expression of the liberty".

        OK, taking another tack at this: you want the glob to which you're tied. I don't think Perl provides that, either in each call, or even in the TIEHANDLE method. So, the user can set and clear $| all day on that handle, and you have no clue how to get to it. That's life.

        -- Randal L. Schwartz, Perl hacker
        Be sure to read my standard disclaimer if this is a reply.

Re: How to get explicity the autoflush var ($|) of a TIED HANDLER?
by ant9000 (Monk) on Dec 29, 2003 at 09:42 UTC
    I don't have a real answer, but IO::Handle does something of the same sort in its autoflush() method, so it can be done: that's the definition of the sub on my 5.6.1 perl:
    sub autoflush { my $old = new SelectSaver qualify($_[0], caller); my $prev = $|; $| = @_ > 1 ? $_[1] : 1; $prev; }
    HTH,
    Ant9000
      That's a different situation. Because autoflush is a method of the filehandle object, it has access to it (as $_[0]). With a tied filehandle, the methods are called using the tied object returned by TIEHANDLE, which doesn't have any way to access the original tie'd handle unless it was passed as an extra parameter to tie.