Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Single character input 2

Status
Not open for further replies.

bluegroper

Technical User
Dec 12, 2002
407
AU
How do I get perl to accept merely a single character of input from the console, and then move on without waiting for the "Enter" key ?
Such as
Code:
#!/perl -w
use strict;
use Net::Wake;
my $Input;
print "Wake On Lan\n";
print "\nWake up SlimServer <Y/N/Q> ?";
$Input = <STDIN>; chomp $Input ; $Input = uc($Input);
if ($Input eq "Q") { exit 0; }
if ($Input eq "Y") {
   Net::Wake::by_udp(undef,'00:10:DC:A8:5F:84');
}
exit 0;

This is just a snippet from a larger project, but I hope you can get the idea.
Just need to press y, n or q, to proceed.
TIA's
- BG
 
you don't that I know of. But maybe someone knows a trick I am unaware of.
 
mikevh knows the trick. In Term::Readkey, you will be able to get a single character at a time.

Michael Libeson
 
You might also want to take a look at this thread: thread219-979228

 
Thx for the helps above.
This almost works, but why cannot perl print the prompt line "Enter something :" BEFORE a key is pressed ?
Code:
#!/perl -w
use Term::ReadKey;
print "\nEnter something : ";
ReadMode 4;     # Change to raw mode
while (not defined ($key = ReadKey(-1))) {}
ReadMode 0;     # Reset original mode 
print "\nThe key is $key\n";
using perl for win32.
TIA's
- BG
 
Add this to your script before your print statement.
Code:
$| = 1;
That will flush the output buffer immediately after the print instead of waiting for a return.

See perldoc perlop

 
Sorry, that should be perlvar, not perlop.
(I always get those 2 confused. [3eyes])



 
Mike, thats brill.
Much thx for that help.
My small project now works like a bought one.
Actually, I was a little surprised how difficult this in perl.
Certainly a trivial matter in C or even Basic.
No matter, it works, and I'll log it for re-use next time.
- BG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top