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

I have been messing around with an aplication that will interface with irc as a gui. Now I coded the gui in Tk which is an event based module which enters into an endless loop waiting for and handling event calls by its very nature. Now I am also using Net::IRC to interface with the irc servers. This module however is also event based and also enters into an endless loop ($irc->start) which waits for and handles events.

Now the authors of Net::IRC forsaw this being an issue so they came up with the do_one_loop() function. However I am having difficulty coming up with a way where I can implement this function and return control back to Tk every time but still have the IRC module poll endlessly for events and as close as real time as possible. Right now I simply have a button which initiates the do_one_loop() function but this is a manual poll for info and not automatic. If I use the $irc->start function then the Net::IRC module never returns control back over to Tk and the gui appears to hang and becomes unusable.

Does anyone have any nifty ideas on how to share control back and forth real time between these two modules?

  • Comment on Event based modules playing nice together?

Replies are listed 'Best First'.
•Re: Event based modules playing nice together?
by merlyn (Sage) on Dec 31, 2004 at 03:01 UTC
Re: Event based modules playing nice together?
by William G. Davis (Friar) on Dec 31, 2004 at 04:44 UTC

    Try using Tk::After's repeat() method to periodically execute do_one_loop():

    my $id = $main_window->repeat(5, sub { $irc->do_one_loop });

    Tk will then execute the sub every 5 milliseconds (well, almost). Play around with the first argument to see what granularity works best. You can use afterCancel() to cancel the Tk::After event when you don't need it to execute anymore:

    $main_window->afterCancel($id);

    You should still check out POE, though. It has a much nicer IRC API than Net::IRC does.

Re: Event based modules playing nice together?
by Zaxo (Archbishop) on Dec 31, 2004 at 02:54 UTC

    How about forking off the IRC component, and employing some form of IPC to hand data back and forth? The IRC gizmo can write to the parent through a pipe, and the parent can know about it through a Tk::fileevent. If the parent writes to the child, the kid can listen with the same select loop that reads the IRC stream.

    Even without fork, the Tk::fileevent can be connected to the IRC socket to produce event-oriented actions.

    After Compline,
    Zaxo