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

The reason I didnt want to detach the thread is because this is for the IRC client that I had to recode using Parse::IRC. I cant print to the $sock in the main code line making the user unable to send a message to the server. If I could get the while loop to be non-blocking then I could send a signal that tells the thread to send a message to the server (which now that I think about seems primitive). I may try the  select option but I have 1 question: how do you check for a complete line?

Replies are listed 'Best First'.
Re^3: Joining a Thread in w While loop
by pc88mxer (Vicar) on Mar 05, 2008 at 16:20 UTC
    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/

      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