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

I'm working on an alternative client for iRATE (http://irate.sourceforge.net/) in Perl/Tk. For the purpose of this question, the important thing is I'm trying to play mp3s one at a time using Xmms::Remote.

To play a single mp3 with Xmms::Remote, I create a one-file playlist and call the play method. Then -- here's the tricky part -- I have to find out when the song is over so I can play the next one.

The only way to detect this is the is_playing() method, which will return true as long as xmms is still playing. I have to put the next song on as soon as it returns false.

In the meantime, while the mp3 is playing, I have to handle all the usual Perl/Tk events, button-clicks and whatnot. Naively polling for the end of the song would lock up the app.

I'm new to Perl/Tk and event-based programming, but it seems this event I'm waiting for (is_playing returning false) is a lot like the events Perl/Tk's MainLoop is waiting on already.

Forgive me if I'm stupid and I missed some obvious documentation somewhere, but can anyone tell me how I can set up my own non-gui-related Perl/Tk event to handle this?

Replies are listed 'Best First'.
Re: setting up non-gui callbacks in Perl/Tk
by pg (Canon) on Nov 09, 2003 at 07:26 UTC

    This is a very simple GUI timer done in Tk, and demos how you can have a background process running, and at the same time, still have GUI components response to user action normally. In this case the background process is the tick, and user can click reset button to reset the timer back to 0. In your case, just put the is_playing() checking in the repeat sub.

    use Tk; use strict; my $mw = MainWindow->new(title => "Timer"); my $elapsed_sec = 0; my $elapsed_sec_label = $mw->Label(-textvariable => \$elapsed_sec)->pa +ck(); $mw->Button(text => "reset", command => sub {$elapsed_sec = 0})->pack( +); $mw->repeat(1000 => sub {$elapsed_sec ++}); #this is repeated every se +cond, and you can put your is_playing here MainLoop;
      ... thanks a million.
Re: setting up non-gui callbacks in Perl/Tk
by PodMaster (Abbot) on Nov 09, 2003 at 05:56 UTC
    Forgive me if I'm stupid and I missed some obvious documentation somewhere, but can anyone tell me how I can set up my own non-gui-related Perl/Tk event to handle this?
    You've got the magic word in your title, and it's "callback", looking at the Tk documentation you should find Tk::callback, which in it's "SEE ALSO" section has "Tk::bind Tk::after Tk::options Tk::fileevent". Tk::after is what you're after ;)

    Also useful is super search and http://perltk.org

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.