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

Hi,
i'm new to all this POE stuff so explain it to me as simple as you can.

This script should connect to the server and join the channel '#Alex', well it doesnt , can someone try and explain to me why? The output i get is:
connected
registered
and thats all.

use Data::Dumper; use POE::Kernel; use POE::Session; use POE::Component::IRC; sub start { my ($kernel, $session) = @_[KERNEL, SESSION]; $kernel->post( 'test', 'connect', { Debug => 1, Nick => 'Bot', Server => 'administrator.co.il', Port => '6667', Username => 'BotV6-Poe', Ircname => 'ModHelp', } )and print("connected\n"); $kernel->post( 'test', 'register', 'all')and print("registered\n"); } sub stop { my ($kernel) = $_[KERNEL]; $kernel->call( 'test', 'quit', 'Neenios on ice!' ); } sub on_connect { print("in on_Connect\n"); my ($kernel) = $_[KERNEL]; $kernel->post( 'test', 'join', '#Alex' )and print("Joined\n"); } sub on_msg { my @args = @_[ ARG0 .. $#_ ]; my $list=join(" . ",@args); $kernel->post('test','privmsg','Isa',@_[ARG0]); } POE::Component::IRC->new('test'); POE::Session->create(inline_states =>{ _start=>\&start, _stop=>\&stop, irc_001=>\&on_connect, irc_msg=>\&on_msg,},); $poe_kernel->run();

Replies are listed 'Best First'.
Re: POE::IRC Not joining Channel
by Thelonius (Priest) on Oct 11, 2003 at 11:11 UTC
    If you add use strict; you will find an error. You may also want to $| = 1; so that you get your messages in a timely fashion.
      I changed the code.
      its connecting as you can see.
      also added "use strict" and didnt find anything.
      use Data::Dumper; use POE::Kernel; use POE::Session; use POE::Component::IRC; use strict; sub start { my ($kernel, $session) = @_[KERNEL, SESSION]; $kernel->post( 'test', 'connect', { Debug => 1, Nick => 'Bot', Server => 'irc.psam.se', Port => '6667', Username => 'BotV6-Poe', Ircname => 'ModHelp', } )and print("connecting\n"); $kernel->post( 'test', 'register','all')and print("registered\n"); } sub stop { my ($kernel) = $_[KERNEL]; $kernel->call( 'test', 'quit', 'Neenios on ice!' ); } sub on_connect { my ($kernel) = $_[KERNEL]; print("in on_Connect\n"); $kernel->post( 'test', 'join', '#Alex' )and print("Joined\n"); } sub on_msg { my @args = @_[ ARG0 .. $#_ ]; my $list=join(" . ",@args); $kernel->post('test','privmsg','Isa',@_[ARG0]); } sub conn { my ($server) = $_[ARG0]; print("connected to $server\n"); } $| = 1; POE::Component::IRC->new('test'); POE::Session->create(inline_states =>{ irc_connected=>\&conn, _start=>\&start, _stop=>\&stop, irc_001=>\&on_connect, irc_msg=>\&on_msg,},); $poe_kernel->run();
      the output is:
      connecting
      registered
      connected to irc.psam.se

        You're not checking whether or not your nickname is in use. Try this:

        use Data::Dumper; use POE::Kernel; use POE::Session; use POE::Component::IRC; use strict; sub start { my ($kernel, $session) = @_[KERNEL, SESSION]; $kernel->post( 'test', 'connect', { Debug => 1, Nick => 'Bot', Server => 'irc.psam.se', Port => '6667', Username => 'BotV6-Poe', Ircname => 'ModHelp', } )and print("connecting\n"); $kernel->post( 'test', 'register','all')and print("registered\n"); } sub stop { my ($kernel) = $_[KERNEL]; $kernel->call( 'test', 'quit', 'Neenios on ice!' ); } sub on_connect { my ($kernel) = $_[KERNEL]; print("in on_Connect\n"); $kernel->post( 'test', 'join', '#Alex' )and print("Joined\n"); } sub on_msg { my $kernel = @_[KERNEL]; my @args = @_[ ARG0 .. $#_ ]; my $list=join(" . ",@args); $kernel->post('test','privmsg','Isa',@_[ARG0]); } sub conn { my ($server) = $_[ARG0]; print("connected to $server\n"); } sub in_use { my $kernel = @_[KERNEL]; print "Nickname is currently in use\n"; $kernel->post( 'test','nick','Mr' . int rand 200); } POE::Component::IRC->new('test'); POE::Session->create(inline_states =>{ irc_connected=>\&conn, _start=>\&start, _stop=>\&stop, irc_001=>\&on_connect, irc_msg=>\&on_msg, # ERR_NICKNAMEINUSE => 433 irc_433=>\&in_use },); $poe_kernel->run();

        Hope this helps.

        antirice    
        The first rule of Perl club is - use Perl
        The
        ith rule of Perl club is - follow rule i - 1 for i > 1

        When I run your code I get
        Global symbol "$kernel" requires explicit package name at 298494b.pl l +ine 38
        That is, $kernel is undeclared (and undefined) in the subroutine on_msg.
Re: POE::IRC Not joining Channel
by Corion (Patriarch) on Oct 11, 2003 at 12:20 UTC

    I don't know much about POE, as I don't like event driven programming, but I think that you don't understand what $kernel->post does:

    I think that $kernel->post only puts a message into the event queue, and directly returns - which makes your diagnostic messages rather useless, as you don't see whether the connection attempt succeeded. I guess that the POE::Component::IRC component supplies a connected event or the like, on which you then should post your register message.

    All of this might well be wrong, as I don't know much about POE and even less about POE::Component::IRC - I draw my conclusions from my general knowledge of event driven programming ...

    perl -MHTTP::Daemon -MHTTP::Response -MLWP::Simple -e ' ; # The $d = new HTTP::Daemon and fork and getprint $d->url and exit;#spider ($c = $d->accept())->get_request(); $c->send_response( new #in the HTTP::Response(200,$_,$_,qq(Just another Perl hacker\n))); ' # web
Re: POE::IRC Not joining Channel
by rcaputo (Chaplain) on Oct 24, 2003 at 05:43 UTC

    You probably already have fixed the script by now, but people reading the thread in archives may find this useful.

    You can create _default handler that can dump events that were not handled any other way. This short function will let you know which events you are getting. For example, some servers don't send an 001 message, so your irc_001 handler may never be triggered.

    sub _default { my ( $state, $event, $args, $heap ) = @_[ STATE, ARG0, ARG1, HEAP +]; $args ||= []; # Prevents uninitialized-value warnings. print "default: $state = $event (@$args)\n"; return 0; }

    POE's cookbook contains a few recipes about IRC bots, including the _default debugging trick.

    -- Rocco Caputo - rcaputo@pobox.com - poe.perl.org