Michael said:
What would be more efficient code, if I don't want to have to press
Enter after each keystroke? In fact, the tight loop bothers me somewhat.
Basically, the purpose of the while-loop is to wait until a key has
been read in. If no key was read in, the while-block gets executed.
In the above example, the block does absolutely nothing. (Sometimes
you may want something to happen if no was pressed, which is why I
think the example was written like this.)
However, if all you want to do is wait until the first key is
pressed, you basically want to perform a "blocking" read (as opposed to
a "non-blocking" read). The "ReadKey(-1)" performs a "non-blocking"
read; to do a "blocking" read, you want to pass in a zero for
ReadKey()'s argument.
Therefore, I would recommend changing the above while-loop to the
following one line of code:
$pressedkey = ReadKey(0); # Note: no loop!
This will prevent the program from advancing any further until a key is
pressed.
By the way, did you know that you can get comprehensive
documentation for a module by using the perldocs? That is, you can
read the documentation for Term::ReadKey by typing the following at any
Unix prompt:
perldoc Term::ReadKey
The reason I ask this is because, in a previous post, you mentioned
that you found the Term::ReadKey documentation online. (I once knew a
programmer who'd been programming Perl for several years who wasn't
aware that he could look up the documentation for any module just by
typing "perldoc ModuleName" at a Unix prompt. Instead of using
documentation he already had, he had to scour the web to find it!)
(And sometimes the programmers who already know about the perldocs
don't realize that there are a lot of other programmers who don't know
about them, so the ones who don't know about the usefulness of the
perldocs continue to be left in the dark about them.)
Anyway, I hope this helps you, Michael.
-- Jean-Luc