I am trying to spoof non-blocking reads on a Win32 client. I have spent a lot of time looking around at various code examples. The vec()..select() method on <STDIN> does not work. ReadLine in TERM::Readkey doesn't work either. but the ReadKey function does.
Here is my code;
#!/usr/bin/perl
use Term::ReadKey;
until ($Name){
print "Your callsign?>";
chomp ($Name = <>);
if ($Name) {
print "say: !! to exit\n\n";
open (TX, ">>d:/telnet/say.txt");
printf TX time." $Name entered.\n";
close (TX);
}
}
$Lastime = time;
while($key.$Input ne "!!"){
open (RX, "<d:/telnet/say.txt");
@Said = <RX>;
close (RX);
foreach $Said(@Said){
$Said =~ m/(^\d+)(\s[\D \d]*)/;
if ($1 >= $Lastime){
print $2;
Tmptime = $1;
}
}
$Lastime = $Tmptime + 1;
if ($key = ReadKey 2){
print $key;
if (ord($key) != 13){
chomp($Input = <>);
}
else {
$key="";
$Input = "";
}
open (TX, ">>d:/telnet/say.txt");
printf TX time." $Name said: $key$Input\n";
close (TX);
}
}
open (TX, ">>d:/telnet/say.txt");
printf TX time." $Name left.\n";
close (TX);
print "\n\nBye.";
It's a basic realtime chat client. It's not entirely bulletproof and it's not very efficient because it reads the whole say.txt file every 2 seconds. I can work on these limitations later.
The part I really need to fix is; Because the first keypress interrupts the ReadKey block it has to be printed to the console seperately. Thus if the user wants to delete their input, they can't delete the first character. Is there any way to inject it into the console instead?
I know this thing is built with dark magic and it has inherrent corruption but I have been on this quest for weeks and this is the best solution I have come up with so far.
All suggestions for improvement would be greatly appreciated.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.