in reply to Re^2: Joining a Thread in w While loop
in thread Joining a Thread in w While loop

To check for a complete line just use a regex:
if ($buf =~ s/(.*\n)//) { # line is in $1 my $line = $1; ...process $line here... }

Note that the s operator conveniently removes the first complete line from $buf as well as returns whether there was a complete line found or not.

btw, have you considered using the Net::IRC module? Have a look at this example: http://jk0.org/projects/perl-irc-bot/

Replies are listed 'Best First'.
Re^4: Joining a Thread in w While loop
by deadpickle (Pilgrim) on Mar 05, 2008 at 19:57 UTC
    I have considered that but will it work with the GTK2 environment? most of these methods work but the problem is using them with GTK2 when you try to send something to the server. Parse::IRC was a real simple way to do this but I cant get it to work in GTK2 (since it seems that timeouts and IO's never work for me) so I'm going to give Net::IRC a run and see if that works better (though I'm not hopeful).
      I think you should rethink your design a bit, to avoid relying on the IO::Watch to watch the filehandle. You could setup a timer instead to read the filehandle at a fast interval, and check for $die at every timer callback run.

      Another super simple "hack", would be to have the server send out newlines to every client, like a strobe, every second. Then in your code, when the socket reads just a newline, close up and go to the end of the thread code block.


      I'm not really a human, but I play one on earth. Cogito ergo sum a bum
        A good idea. In shorthand:
        #-------------------Connect to IRC Server------------------- sub connecting { # Connect to the IRC server. $sock = new IO::Socket::INET( PeerAddr => $irc, PeerPort => 6667, Proto => 'tcp', ) or die "Can't connect\n"; print $sock "NICK $nick\r\n"; print $sock "USER $login 8 * :CoCoNUE Member $nick\r\n"; $sel->add($sock); ################################################# #Timeout my $timer_chat = Glib::Timeout->add(1, \&incoming_data); #################################################\ return 1; } #-------------------Watch for IRC Inputs------------------- sub incoming_data { if ($sel->can_read(1)){ my $input = <$sock>; $input =~ s/\r\n//g; my $hashref = $parser->parse( $input ); SWITCH: { my $type = lc $hashref->{command}; my @args; push @args, $hashref->{prefix} if $hashref->{prefix}; push @args, @{ $hashref->{params} }; if ( defined $dispatch{$type} ) { $dispatch{$type}->(@args); last SWITCH; } print STDOUT join( ' ', "irc_$type:", @args ), "\n"; } } return 1; }
        Now the only problem is that the GTK2 window stops responding. I think its caused by the my $input = <$sock> having nothing to read and it just makes the GTK2 program angry. Is there a way to avoid this other than adding a watch or any other crazy GTK2 dispatcher? Thanks for the help everyone.
        Update: should IO::Select be used? I have been barking up that tree with not much success either. I added the IO::Select to the loop and the program is now responding but it is very sluggish which is bad. any ideas?