If you have Term::ReadKey installed, it is a simple matter of saying

  ReadKey( -1 ); # or ReadLine( -1 )

and then you can read from STDIN and if there is nothing waiting your script will continue. Note that this doesn't work on Win32. I have created the same functionality in the past using a watchdog file:

my $watchdog = "./watchdog.@{[time]}.$$"; open WATCHDOG, ">$watchdog" or die "Cannot open $watchdog for output +: $!\n"; print WATCHDOG "This file may safely be deleted\n"; close WATCHDOG; while( $universe->evolve ) { if( !(++$iters % 100_000) ) { last unless -f $watchdog; } # do some more stuff } do_logging(); unlink $watchdog;

Once your script is churning away, all you have to do is to find some way of deleting the file you created at the top of the script. Kill it, and your script can close down doing whatever housekeeping needs to be done.

In the example, I show that the test be performed once every x iterations. For performance reasons, you don't really want to be statting the disk on every iteration. Tune this value to get the reactivity you require.

I quite like this solution, as you don't need to install another module, it's eminently portable and other processes can kill the execution if need be.

--
g r i n d e r

In reply to Re: keyboard input during runtime... by grinder
in thread keyboard input during runtime... by Shadowfax

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.