Hannes said:
Is there a way in Perl to get a callback triggered by incoming data?
Keeping the process busy by polling the non-blocking socket in an
idle-callback wouldn't be very good...
Why not? That's very similar to what Tk::MainLoop() is doing. You have
to poll to find something (unless you're using signals, but in your case
you can't).
For your purposes, I would replace the call to Tk::MainLoop with
something like the following [untested - adapted from example in perldoc
IO::Select]:
use Tk qw/:eventtypes/;
use IO::Select;
use IO::Socket;
my $lsn = new IO::Socket::INET(Listen => 1, LocalPort => 12345);
my $sel = new IO::Select($lsn);
while (1) { # this is your mainloop
# process any Tk events like mouse moves/button presses/etc
1 while $MW->DoOneEvent(ALL_EVENTS|DONT_WAIT);
# poll for any data in the sockets in a non-blocking way
if (my @ready = $sel->can_read(0.1)) {
for my $fh (@ready) {
if ($fh == $lsn) { # new connection
my $new = $lsn->accept;
$sel->add($new);
} else {
# somebody sent something
my $data = <$fh>;
if (defined $data) {
#
# trigger your callback based on what $data contains.
#
} else { # hung up
$sel->remove($fh);
$fh->close;
}
}
}
}
}
Now your clients can connect to port 12345 on the local machine and send
whatever data is needed to trigger the callbacks.
--Ala