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

Hello all. I'm having trouble with a script I'm running on my linux box. It's an IRC bot that was working when I first wrote it, but now that I've rebooted my computer and tried to run it again, it doesn't seem to work, and it issues my these two errors:

Can't connect to irc.whatever.net:6667! at /usr/lib/perl5/site_perl/Net/IRC.pm line 192

Can't call method "add_handler" on unblessed reference at ./bot line 75.

Here is what my code looks like:
#!/usr/bin/perl use Net::IRC; use strict; # create the IRC object my $irc = new Net::IRC; # Create a connection object. You can have more than one "connection" +per # IRC object, but we'll just be working with one. my $conn = $irc->newconn( Server => shift || 'irc.whatever.net', Port => shift || '6667', Nick => 'Bot', Ircname => 'Bot', Username => 'Bot'); # We're going to add this to the conn hash so we know what channel we # want to operate in. $conn->{channel} = shift || '#mychannel'; sub on_connect { # shift in our connection object that is passed automatically my $conn = shift; # when we connect, join our channel and greet it $conn->join($conn->{channel}); $conn->privmsg($conn->{channel}, 'Hello everyone!'); $conn->{connected} = 1; } sub on_join { # get our connection object and the event object, which is passed # with this event automatically my ($conn, $event) = @_; # this is the nick that just joined my $nick = $event->{nick}; # say hello to the nick in public $conn->privmsg($conn->{channel}, "Hello, $nick!"); } sub on_part { # pretty much the same as above my ($conn, $event) = @_; my $nick = $event->{nick}; $conn->privmsg($conn->{channel}, "Goodbye, $nick!"); } sub on_public { my($conn, $event) = @_; my $text = $event->{args}[0]; if ($text eq "\U$text") { if (length $text > 6) { my $name_text = $event->{nick}; $conn->privmsg($event->{to}[0], "$name_text: No need to yell. Switch t +o lowercase and be happy. =)"); } } sub on_msg { my($conn, $event) = @_; $conn->privmsg($event->{nick}, "I, don't appreciate being /msg'd."); } # add event handlers for join and part events $conn->add_handler('join', \&on_join); $conn->add_handler('part', \&on_part); $conn->add_handler('public', \&on_public); $conn->add_handler('msg', \&on_msg); # The end of MOTD (message of the day), numbered 376 signifies we've c +onnect $conn->add_handler('376', \&on_connect); # start IRC $irc->start();
_______________________

I entered irc.whatever.net for the server, because I've tried to connect it to multiple servers and it won't connect to any of them. I would think that this is simply a problem with my code, because it usually is =), but this same script was working before i rebooted my computer. Any help on this subject would be greatly appreciated, as I'm pulling my hair out over it. =)

Thanks a lot

Edit by myocom: (fixed code tags)

Replies are listed 'Best First'.
Re: Net::IRC problem
by erikharrison (Deacon) on Mar 16, 2002 at 02:36 UTC

    First off, your code won't even compile - this may be because there were some mistakes transfering to the browser, I dunno. You're missing the right curlie to end the on_public sub. Second, your code has no indentation and declares some subs in the middle. Now, we can argue about sub declaration all we want, (and frankly, you can do it however you want, and since they are handlers it is probably reasonable) but some indenting, however you choose to do it, would make life easier. :-)

    Now, your first problem is with the module - it is what fails. Now if a connection isn't made, then $conn isn't an object - that's your second problem. This is why add_handler won't work - the connection fails, so the return isn't a connection object. I don't know why it fails - Net::IRC is called a work in progress. Try loading a fresh copy of the module and it's friends and try it all again. If that doesn't work, send a bug report to the author.

    Cheers,
    Erik
      Ok, I'll try that. Thanks a lot for your help, up until now, no one has had the slightest clue as to what I was talking about. ;) Also, about the indentation issue and the curly brace...I have the curly brace in my code, and my code is also completely indented...it looks really nice...lol. I guess something got messed up when I posted it here, not that I expect you to believe me or anything, but my code really does look nice. =) Thanks again for your help!