I would like to create an IRC client in Gtk2. I based the code on a simple-bot example found at IRC hacks. So far the program connects to the server just fine, the problem I encounter is when the user closes the window. The program tends to hang and becomes non-responsive. I made an attempt to fix it but but I still cant find where it is hanging.
#!/usr/local/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; use threads; use threads::shared; use IO::Socket; #-------------------Shared Variables------------------- my $server:shared = "irc.freenode.net"; my $nick:shared = "simple_bot"; my $login:shared = "simple_bot"; my $channel:shared = "#GRRUVI"; my $die:shared = 0; ################################################# my $thread = threads->new( \&magic); ################################################# #-------------------Main Loop------------------- my $window = Gtk2::Window->new('toplevel'); $window->signal_connect( delete_event => sub { $die = 1; $thread->join; Gtk2->main_quit; }); $window->set_default_size( 300, 200 ); my $table = Gtk2::Table->new(2, 1, FALSE); my $scroller = Gtk2::ScrolledWindow->new; my $textview = Gtk2::TextView->new; my $entry = Gtk2::Entry->new; $scroller->add($textview); $table->attach_defaults($scroller, 0, 1, 0, 1); $table->attach_defaults($entry, 0, 1, 1, 2); $window->add($table); $window->show_all; Gtk2->main; ################################################# #-------------------IRC Thread------------------- sub magic { # Connect to the IRC server. my $sock = new IO::Socket::INET( PeerAddr => $server, PeerPort => 6667, Proto => 'tcp') or die "Can't connect\n"; # Log on to the server. print $sock "NICK $nick\r\n"; print $sock "USER $login 8 * :Perl IRC Hacks Robot\r\n"; # Read lines from the server until it tells us we have connected. while (my $input = <$sock>) { # Check the numerical responses from the server. if ($input =~ /004/) { # We are now logged in. last; } elsif ($input =~ /433/) { die "Nickname is already in use."; } } goto END if $die == 1; # Join the channel. print $sock "JOIN $channel\r\n"; # Keep reading lines from the server. while (my $input = <$sock>) { chop $input; if ($input =~ /^PING(.*)$/i) { # We must respond to PINGs to avoid being disconnected. print $sock "PONG $1\r\n"; } else { # Print the raw line received by the bot. print "$input\n"; } goto END if $die == 1; } END: undef $sock; } #################################################

In reply to IRC Client (non-bot) by deadpickle

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.