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

I'm writing a perl script that controls a video player program through a telnet interface. The idea is to create a video slide-show by periodically sending commands to the player to play different files. I'd like to have the script respond to both stdin input from the user, and also be able to operate on a timer mode (so advance whenever "next" command received or when timer goes off).

I have a current kludgy implementation using forks (a main process forks off child timers, which kick the player, while the parent loops reading from stdin and kicks whenever the it reads a "next" line. See here.

I gather the best way to do this is probably using POE. I've read a lot of the documentation, and I can see how a POE script would look that could do either the timer, or the stdin read, but not both at once. I envision essentially two sessions: A would select on two handles reading lines and executing commands. One handle would be stdin, and the other would be a pipe whose write end was held by B. B would sleep for an interval, then send A a message (identical to B's next command message) to A on the pipe. All major state would reside in A.

I'm not sure though how to keep the select from blocking the timer (though I've read the POE docs on using yield instead of sleep for the timer) without then creating a busy-loop over can_read... I've also checked out Wheel:Run for the timer process, but I'm worried getting pipes set up between the processes will be a nightmare. The main session will also have to be able to cancel and restart the timer (since the user may wish to turn off the auto-advance).

Any suggestions on the best structure for this or the best parts of POE to use are welcome. I'd also welcome any pointers to example POE scripts that do this sort of thing (listening to stdin and also responding asynchronously to some other generated event(s)).

  • Comment on outline of POE setup for responding to stdin and timer

Replies are listed 'Best First'.
Re: outline of POE setup for responding to stdin and timer
by rcaputo (Chaplain) on Oct 26, 2011 at 00:50 UTC
Re: outline of POE setup for responding to stdin and timer
by zentara (Cardinal) on Oct 26, 2011 at 12:04 UTC
    Another option is Glib. Here is a simple example using the Glib eventloop, your telnet filehandle could be watched by an add_watch callback.

    You could also use threads and threads:shared to avoid the forks and pipes.

    #!/usr/bin/perl use warnings; use strict; use Glib; my $main_loop = Glib::MainLoop->new; my @input = ('a'..'z'); my $count = 1; my $timer = Glib::Timeout->add (1000, \&timer_callback, undef, 1 ); sub timer_callback{ $count++; print "$count\n"; return 1; } my $timer1 = Glib::Timeout->add (100, \&timer1_callback, undef, 1 ); sub timer1_callback{ push (@input,shift(@input)); # circular list print "\t", $input[0], "\n"; return 1; } Glib::IO->add_watch (fileno 'STDIN', [qw/in/], \&watch_callback, 'STDI +N'); $main_loop->run; #################################################################### sub watch_callback { my ($fd, $condition, $fh) = @_; if(sysread(STDIN, my $buf, 1024)){ print "$buf\n" } #always return TRUE to continue the callback return 1; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh