Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

When is a while not a while?

by Miker (Scribe)
on Aug 24, 2000 at 17:37 UTC ( #29412=perlquestion: print w/replies, xml ) Need Help??

Miker has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, I have one that might not be possible... What I'm doing is running a program with Open2, but need to send instructions to it when there is a single key-press. For that I'm using from the Perl Cookbook. The problem I run into is I need to wait on While which causes my other process to hang untill it gets that key. I want it to just run it and take the key-presses as they come so to speak. Here's a simplified version of what I mean:
Use; foreach $i (@things) { &program_running($i); while ($keyboard = readkey) { # This is the gotcha if ($keyboard =~ /b/) { print "you pressed b \n"; } } }
Is there any way to check for a key-press that does not wait? Sort of check it on the fly, or periodicly? TIA, Miker

Replies are listed 'Best First'.
RE: When is a while not a while?
by mikfire (Deacon) on Aug 24, 2000 at 19:15 UTC
    I think you need to explore the wonders of Term::ReadKey, which has lots of neat tricks but one of them is the function ReadKey.

    And I quote from perldoc Term::ReadKey ( slightly reformatted to make it read easier )

    ReadKey MODE [, Filehandle] Takes an integer argument, which can currently be one of the following values: 0 Perform a normal read using getc -1 Perform a non-blocked read >0 Perform a timed read (If the filehandle is not supplied, it will default to STDIN.) If there is nothing waiting in the buffer during a non-blocked read, then undef will be returned. Note that if the OS does not provide any known mechanism for non-blocking reads, then a `ReadKey -1' can die with a fatal error. This will hopefully not be common.
    You can play meaner games with select() if you cannot get this module installed.


      Thanks, I'm glad to have as many options as possible. I'm going to know my way around CPAN pretty well after today :) I like the sounds of ReadKey so thats going to be my next try...
      Thanks, that did the trick perfectly. No while loop was needed as the key seek was called in one of the main loops of the program. Thank you very very much, mikfire:)
RE: When is a while not a while?
by jreades (Friar) on Aug 24, 2000 at 19:06 UTC

    I'll take a flyer and suggest a couple of possibilities depending on the behaviour you want::

    1. do {} while () -- this would allow the code to execute once before 'hanging' on the while. I can't really envision how this would apply to your situation, but hey, you never know.

    2. Threads -- I've got admit that I've only used threading in Java, but there are at least three threading modules on CPAN.

    What you'd want to do is have one thread waiting for keyboard input -- I'm not sure what the exact syntax would be, but you'd create the thread with a low priority/nice-ness so that other processes keep running (maybe even a sleep()), and spin off your other process as a seperate thread so that it can run without waiting for keyboard input (assuming that this is the behaviour you want).

    The tricky part is establishing a safe way for the two threads to talk to each other -- in Java you have Thread-safe objects/methods that you can essentially use as a lock on your data ('don't read this until I've finished writing to memory/disk...').

    I suspect that in Perl this will be a little tougher, but I'm sure that someone can either a) offer actual code, or b) point you to a module, to solve your problem.

    Sorry I can't offer actual code, but this is outside the scope of my normal Perl coding...

      Threaded Perl is labelled experimental for a reason!

      If you want to do this with threads, you will want to write a threaded program in another language and have it call Perl separate interpreters from within the threads.

      Along the same lines, you can always use fork() instead of threads. A little heavier, a lot safer. (Outside of *nix systems which have been optimized for forks, a *lot* heavier or else emulated with threading as described above.)

        What are the tradeoffs between fork() and threads?

        As I said, I've only used threads in Java (which AFAIK has no forking) and they are one of the fundamental classes... Anyway, tradeoffs: discuss.


      Thanks, that gives me more options than I had:) The threading sounds interesting, I'm off to CPAN to see what lurks. Thanks again:)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://29412]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2021-08-04 22:14 GMT
Find Nodes?
    Voting Booth?
    My primary motivation for participating at PerlMonks is: (Choices in context)

    Results (43 votes). Check out past polls.