Just for fun (and because I haven't done a ReadKey in a while), here's my guess at a structure for what you are asking.

#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11141714 use warnings; use Term::ReadKey; use Time::HiRes qw( time ); use List::Util qw( max ); $| = 1; my $n = my @menuitems = qw( one two three four ); my ($w, $h) = GetTerminalSize; my $interval = 10; my $due = time + $interval; my $result = eval { ReadMode 'cbreak'; while( 1 ) { print "\e[H\e[J", "\n" x ($h - @menuitems - 5), "enter number for choice:\n\n"; print "$_ : $menuitems[ $_ - 1 ]\n" for 1 .. @menuitems; print "\nnumber : "; my $delta = max 0.001, $due - time; local $_ = ReadKey $delta; if( not defined $_ ) { print("running occasional program every $interval seconds"); sleep 1; $due = time + $interval; } elsif( /[1-$n]/ ) { print("running $menuitems[$_ - 1]"); sleep 1; } elsif( /[qQ\e]/ ) { last; } else { print("bad key, try again"); sleep 1; } } } || $@; ReadMode 'restore'; print "$result\n";

Of course, replace the sleeps with your subs, and more work would be required if you have more than 9 menu items.


In reply to Re: Run subroutine occasionally by tybalt89
in thread Run subroutine occasionally by rementis

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.