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)).


In reply to outline of POE setup for responding to stdin and timer by bchoward00

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.