Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Perl Timer Question
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
sarumont
Tux's lil' helper
Tux's lil' helper


Joined: 31 Oct 2002
Posts: 94
Location: /dev/urandom

PostPosted: Fri May 02, 2003 2:15 am    Post subject: Perl Timer Question Reply with quote

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
View user's profile Send private message
Jimbow
Guru
Guru


Joined: 18 Feb 2003
Posts: 597
Location: Silver City, NM

PostPosted: Fri May 02, 2003 6:50 am    Post subject: Reply with quote

Do I understand you correctly that your problem is that your code is blocking (waiting) on the following statement?
Code:
$line = <$sock>;

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
View user's profile Send private message
sarumont
Tux's lil' helper
Tux's lil' helper


Joined: 31 Oct 2002
Posts: 94
Location: /dev/urandom

PostPosted: Fri May 02, 2003 7:56 pm    Post subject: Reply with quote

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
View user's profile Send private message
sarumont
Tux's lil' helper
Tux's lil' helper


Joined: 31 Oct 2002
Posts: 94
Location: /dev/urandom

PostPosted: Wed May 07, 2003 2:34 am    Post subject: Reply with quote

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
View user's profile Send private message
Jimbow
Guru
Guru


Joined: 18 Feb 2003
Posts: 597
Location: Silver City, NM

PostPosted: Wed May 07, 2003 7:37 am    Post subject: Reply with quote

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
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum