man Tk::Eventloop () - ToolKit for Events

NAME

Tk::Event - ToolKit for Events

SYNOPSIS

 use Tk::Event;

 Tk::Event->fileevent(\*FH, 'readable' => callback);

 Tk::Event->lineavail(\*FH, callback);

 use Tk::Event::Signal qw(INT);

 $SIG{'INT'} = callback;

 use Tk::Event::process;

 Tk::Event->proc($pid, callback);

 QueueEvent(callback [, position])

DESCRIPTION

That is better than nothing but still hard to use. Most scripts want higher level result (a line, a block of data etc.)

So it has occured to me that we could use new-ish TIEHANDLE thus:

my CW$obj = tie SOMEHANDLE,Tk::Event::IO;

while (<SOMEHANDLE>) { }

Then the READLINE routine registers a callback and looks something like:

sub READLINE { my CW$obj = shift; Event->io(*$obj,'readable',sub { sysread(*$obj,${*$obj},1,length(${*$obj}) }); my CW$pos; while (($pos = index(${*$obj},$/) < 0) { DoOneEvent(); } Event->io(*$obj,'readable',''); # unregister CW$pos += length($/); my CW$result = substr(${*$obj},0,$pos); substr(${*$obj},0,$pos) = ''; return CW$result; }

This is using the scalar part of the glob representing the _inner_ IO as a buffer in which to accumulate chars.