View previous topic :: View next topic |
Author |
Message |
sarumont Tux's lil' helper
Joined: 31 Oct 2002 Posts: 94 Location: /dev/urandom
|
Posted: Fri May 02, 2003 2:15 am Post subject: Perl Timer Question |
|
|
I'm writing a perl IRC bot and I want to have a module to run a quiz of sorts. I have everything working except for the timer...I need to be able to refresh my $time variable without waiting for input from the other end, which is what it does now...here's that section of the code:
Code: |
QUESTION: while( $time <= $limit ) {
$time = time;
$line = <$sock>;
( $junk, $text ) = split( / :/, $line );
( $nick, $comType, $comDest ) = split( / /, $junk );
( $nick, $hostname ) = split( /!/, $nick );
$nick =~ s/://;
chomp( $text );
#print for logging purposes
print "<$nick:$comType:$comDest> $text";
if( $comDest eq $channel ) {
if( $text =~ /^$ans/i ) {
$score{ $nick }++;
print $sock "PRIVMSG $channel :\cC15,1That's correct,\cC9 $nick!";
if( $score{ $nick } == 5 ) {
$winner = 1;
print $sock "PRIVMSG $channel :\cC9,1 $nick \cC15wins! Thanks for playing Movie Line Trivia v0.10!";
last QUESTION;
}
sleep 3;
last QUESTION;
}
}
}
|
I know that the output to the channel is pretty horrible, but it's still in "beta". Any input would be very much appreciated. _________________ ~Sarumont
"Time is an illusion. Lunchtime doubly so." |
|
Back to top |
|
|
Jimbow Guru
Joined: 18 Feb 2003 Posts: 597 Location: Silver City, NM
|
Posted: Fri May 02, 2003 6:50 am Post subject: |
|
|
Do I understand you correctly that your problem is that your code is blocking (waiting) on the following statement?
If so, the answer is to use non-blocking I/O. It is a little tricksie but not too bad. You can start finding out about it with "perldoc -f select" and "perldoc IO::Select". _________________ After Perl everything else is just assembly language. |
|
Back to top |
|
|
sarumont Tux's lil' helper
Joined: 31 Oct 2002 Posts: 94 Location: /dev/urandom
|
Posted: Fri May 02, 2003 7:56 pm Post subject: |
|
|
Thanks...that's the line it's waiting for...I'll let you know if it works when I have a chance to implement it (going to a Music Midtown this weekend). _________________ ~Sarumont
"Time is an illusion. Lunchtime doubly so." |
|
Back to top |
|
|
sarumont Tux's lil' helper
Joined: 31 Oct 2002 Posts: 94 Location: /dev/urandom
|
Posted: Wed May 07, 2003 2:34 am Post subject: |
|
|
OK...now I have it timing out all the time...can_read isn't doing the trick...it seems taht it's always returning an undefined array, even if text is typed...do you have any suggestions for this? _________________ ~Sarumont
"Time is an illusion. Lunchtime doubly so." |
|
Back to top |
|
|
Jimbow Guru
Joined: 18 Feb 2003 Posts: 597 Location: Silver City, NM
|
Posted: Wed May 07, 2003 7:37 am Post subject: |
|
|
Here are a couple of ideas. You could write the select(,,,) code yourself. You might need to use some sort of a read() command to read a single byte from the socket each time it is ready because "$line =<$sock>" will still block waiting for \n. Code: |
Set up select read vector
while (...) {
if data is waiting {
while data is waiting {
read one byte
}
}
...
} |
Or you could try "use Fcntl" and use fcntl to make your $sock non-blocking. _________________ After Perl everything else is just assembly language. |
|
Back to top |
|
|
|