From: Peter J. Holzer on
On 2010-01-14 02:19, Ben Morrow <ben(a)morrow.me.uk> wrote:
> Quoth "Peter J. Holzer" <hjp-usenet2(a)hjp.at>:
>> Maybe a function "flush that stream before reading from this stream". Or
>> - even more generic - just a hook which is called for certain ops. So
>> you could do something like
>>
>> $in->add_hook(pre_read => sub { $out->flush });
>
> This is called a 'PerlIO layer'.

Duh! You are right of course. I haven't thought of that. (I did think of
subclassing IO::Handle, but I couldn't figure out how to make STDOUT use
the new class).

hp

From: Ben Morrow on

Quoth "Peter J. Holzer" <hjp-usenet2(a)hjp.at>:
> On 2010-01-14 02:19, Ben Morrow <ben(a)morrow.me.uk> wrote:
> > Quoth "Peter J. Holzer" <hjp-usenet2(a)hjp.at>:
> >> Maybe a function "flush that stream before reading from this stream". Or
> >> - even more generic - just a hook which is called for certain ops. So
> >> you could do something like
> >>
> >> $in->add_hook(pre_read => sub { $out->flush });
> >
> > This is called a 'PerlIO layer'.
>
> Duh! You are right of course. I haven't thought of that. (I did think of
> subclassing IO::Handle, but I couldn't figure out how to make STDOUT use
> the new class).

IO::Handle is a disgusting hack. I expect it could be done, but it
wouldn't be easy: an IO::Handle is a blessed glob containing a tied
filehandle, so you would have to tie *main::STDOUT{IO} and then either
(re)bless *main::STDOUT or assign it into a new glob which could be
blessed.

You could also, of course, tie STDOUT directly. I have never really
understood why it appears to have been necessary for Perl to end up with
three different systems for attaching magic to filehandles, none of
which quite works right.

Ben