Did you get an answer that you like?

A non-blocking read would work, but the need to spread checks throughout the code to test for user input is a pain.

Using signals is a possibility, but from what I've read, the problems associated with signals in perl, even on fully complient POSIX systems, mean that getting this right can be a hit and miss affair even using 5.8's safe signals.

If your target is Win32, and you don't need portability, then there is a way that might work. The idea is that you would start a thread to monitor the keyboard waiting for the user to indicate they want to interact. Your main script would be running on another thread. When you recieve user input, you can use a couple of Win32 native apis; SuspendThread & ResumeThread to pause and restart the thread running the main code whilst you make your changes. This would neatly avoid the need to litter tests throughout the main code, but is obviously very OS specific.

The only real problem I see in implementing this is that that the API's require a native 'thread handle' rather than the perl 'thread id'. This is stored and used internally by the threads module, but not exposed. It would require a small modification to the module to gain access to this, perhaps if the $thread->tid method is called in a list context, then it would return both.

I'd be willing to take a crack at making this work if anyone would find it useful. It would probably need to be based around 5.8.1 as this is the first version in which threads have begun to act in a resonably sane manner. (Just ripe for me to come along and screw them up:)


Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller


In reply to Re: GUI-like behavior by BrowserUk
in thread GUI-like behavior by eweaverp

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.