Eventloop(3pm)

πŸ’– Eventloop β€” Tk::Event πŸ’–

okay so bestie, this is the manual page for Tk::Event (it lives in the perl manual pages, section 3pm, user contributed). The whole vibe is ToolKit for Events. yes that's the actual name. it's giving "we named it in a hurry but it slaps." πŸŽ€


✨ SYNOPSIS ✨

here's the whole thing, verbatim, no notes:

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])
QueueEvent takes a callback and an optional position in that bracket situation, and yeah the brackets are literal, that's how it's written, i don't make the rules. πŸ’…

🌸 DESCRIPTION 🌸

so the tea is: that whole thing up there? better than nothing. but honestly bestie it's still hard to use. 😭 like, lowkey painful. most scripts don't want raw events, they want the higher level result β€” a line, a "block" of data, etc. that's the dream. that's the goal.

so it has occured to me (yes, "occured", i'm quoting the original spelling, no cap, leave it alone 🫢) that we could use the new-ish TIEHANDLE thus:

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

while (<SOMEHANDLE>)
 {
 }

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

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

and this is the iconic part, the main character move ✨: it's using the scalar part of the glob representing the _inner_ IO as a buffer in which to accumulate chars. that's it. that's the whole trick. obsessed. πŸ‘‘


πŸ’œ a lil sign-off πŸ’œ

this page is from perl v5.40.1, dated 2025-04-13, and it's filed under User Contributed Perl Documentation β€” which is exactly where all the fun stuff lives tbh. πŸ¦‹ go forth and event-loop, bestie. periodt. πŸ’–

πŸ“¦ where this page comes from