Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
The problem:

I want to run code until the user hits a key (I donít care which key). In old fashioned BASIC itís simple:
count! = 0 DO datain$ = INKEY$ count! = count! + 1 LOOP UNTIL LEN(datain$) > 0 PRINT count!
Running this for about a second results in half a million iterations. But I canít work out how to do the same thing in Perl.

I have tried:

One way:
use strict; use warnings; use diagnostics; my $count = 0; my $char; until (sysread STDIN, $char, 1) { $count++; } print $count;
Another way, based on Perl cookbook recipe 15.6:
use strict; use warnings; use diagnostics; use Term::ReadKey; my $count = 0; my $char; ReadMode('cbreak'); while ($char eq "") { $char = ReadKey(0); $count++; } print $count;
What happens:

Both respond in the same way. They sit until a key is pressed, and then return 1. I've only waited for up to five seconds, but even if I get multiple iterations by waiting longer, it's not nearly fast enough. I think what is happening is that the code is being optimised too far, but I would welcome clarification on this. I donít actually need the counter in the finished code Ė Iím trying to initialise a series of pseudo-random number generators, but that code isnít relevant to this particular part of the problem. The counter is just there to satisfy me that something is happening.

TIA and regards,

John Davies

In reply to Problem running code until a user hits a key by davies

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (7)
    As of 2018-06-20 19:59 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (117 votes). Check out past polls.