Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

So I decided to write an IRC client because all those others either don't support SOCKS or are too big. I was under the impression it would be a 10-minute project because the lovely POE::Component::IRC did all the work for me, but I've run across a problem. It fails to actually connect to anything, it just sits there. What's the problem? Code follows.
#!/usr/bin/perl use strict; use warnings; use POE::Component::IRC; use Tk; use YAML qw( LoadFile ); my $mw = MainWindow->new(); my $chan = $mw->Entry( -width => 10 )->pack(); my $join = $mw->Button( -text => 'Join', -command => sub { join($chan) + } )->pack(); my $in = $mw->Entry( -width => 80 )->pack(); $mw->Button( -text => 'Send', -command => sub { enter($in,$chan) } )-> +pack(); my $out = $mw->Text()->pack(); my @arg = %{ LoadFile("config_ircclient.yml") }; my $irc = POE::Component::IRC->spawn( @arg ) || die( "Huh?!\n" ); POE::Session->create( package_states => [ 'main' => [ qw( _default _start irc_001 irc_public irc_msg ) ] ], heap => { irc => $irc } ); if ( fork() ) { MainLoop; } else { POE::Kernel->run(); } sub _start { my $session = $irc->session_id(); POE::Kernel->post( $session => register => 'all' ); POE::Kernel->post( $session => connect => {} ); sendmsg( "POE is starting.\n" ); undef; } sub irc_001 { sendmsg( "Connected.\n" ); undef; } sub irc_public { my ($kernel,$sender,$who,$where,$what) = @_; my $nick = ( split /!/, $who )[0]; my $channel = $where->[0]; sendmsg( "$channel: $nick: $what\n" ); undef; } sub _default { my ( $event, $args ) = @_; my @output = ( "$event: " ); foreach my $arg ( @$args ) { if ( ref($arg) eq 'ARRAY' ) { push( @output, "[" . join(" ,", @$arg ) . "]" ); } else { push ( @output, "'$arg'" ); } } sendmsg( join( " ", @output ) . "\n" ); undef; } sub irc_msg { my ( $sender, $msg ) = ( (split(/!/,$_[0]))[0], $_[2] ); sendmsg( "PRIVMSG: $sender: $msg\n" ); undef; } sub enter { $irc->yield( privmsg => $_[1]->get() => $_[0]->get() ); $_[0]->selectionClear(); } sub sendmsg { $out->insert( 'end', $_[0] ); print "$_[0]"; } sub join { $irc->yield( join => $_[0] ); }

Replies are listed 'Best First'.
Re: Perl/Tk IRC client
by zentara (Cardinal) on Jan 24, 2008 at 14:03 UTC
    It fails to actually connect to anything, it just sits there. What's the problem?

    I'm not familiar with IRC nor POE, but since no one else answered, I would hazard a guess that the POE and Tk Mainloops are not working together properly. POE has an eventloop, as does Tk; one needs to be in control (probably POE), and the slave loop (Tk) needs to be updated frequently, for it to be responsive.

    See POE Tk interfaces

    Your code

    if ( fork() ) { MainLoop; } else { POE::Kernel->run(); }
    seems awfully suspicious, in that it separates the Tk and POE eventloops. How do the parent and child communicate after forking?

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum