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

use Net::AIM; my $nick = 'xx'; $aim = new Net::AIM; $aim->newconn( Screename => 'xx', Password => 'xx' ) or die "Cannot connect to AIM Server!"; $aim->getconn->add_handler('config', \&init); $aim->start; sub init { my ($self, $event) = @_; my ($str) = $event->args; $self->setconfig($str); $self->send_im($nick, "Are you there, good sir?"); }
When I try to run this, I get the following error:

Can't call method "add_handler" without a package or object reference at aimBot.pl on line 9.

I'm pretty sure I have an instance of the Net::AIM class in the $aim variable, right?

I'm of the rare perl 'newbie' species, so any advice/tips would be greatly appreciated.

Replies are listed 'Best First'.
Re: Net::AIM Sorrows
by The Mad Hatter (Priest) on May 24, 2003 at 05:29 UTC
    I don't think you can directly call $aim->getconn->add_handler('config', \&init); I'd suggest
    my $conn = $aim->getconn; $conn->add_handler('config', \&init);
    Also, I may be wrong, but I thought the method was set_handler not add_handler...
      The Mad Hatter is right--you need to break these up:

      my $AIM = new Net::AIM; $AIM->newconn(Screenname => $name, Password => $password, AutoReconnect => 1) or die "Unable to open AIM connection.\n"; my $conn = $AIM->getconn();

      Then, once you have $conn, you can set handlers, send messages and so forth:

      sub send_aim { local($_) = shift; my $ready = 0; $conn->set_handler(config => sub {$ready = 1}); $AIM->do_one_loop; $conn->send_im($to_screenname, "From $name: $_"); }

      See also, this node and this site for more info. (Wiredbots.com has good examples for setting/using handlers.)

Re: Net::AIM Sorrows
by theorbtwo (Prior) on May 24, 2003 at 06:35 UTC

    I just checked quickly, and perlmonkscb uses $conn->set_handler('im_in', \&im_in); (and suchlike, of course). (I'd show my config handler, but I don't handle it.)

    I think the specific problem is that getconn returns the active connection, and until you've start()ed, there is no active connection. OTOH, you /have/ done a newconn by then, so I could be wrong... I'm too lazy to take a better look right now.


    Warning: Unless otherwise stated, code is untested. Do not use without understanding. Code is posted in the hopes it is useful, but without warranty. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).