使用Perl制作的IRC Bot的一些简单错误

时间:2011-01-25 23:09:27

标签: perl cpan irc bots

我正在关注一个名为Programming IRC Bots In Perl的教程,为Abjects服务器上的频道制作一个简单的IRC机器人,问题是我遇到了一些奇怪的错误。看看:

  Nathan-Camposs-MacBook-Pro:桌面Nathan $ ./bot.pl
  ./bot.pl:line 1:use:command not found
  ./bot.pl:line 4:my:命令未找到
  ./bot.pl:8行:意外令牌('
./bot.pl: line 8:
附近的语法错误我的$ conn = $ irc-> newconn('
  Nathan-Camposs-MacBook-Pro:桌面Nathan $

使用此代码:

use Net::IRC;

# 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 || 'summer.abjects.net',
 # Note: IRC port is normally 6667, but my firewall won't allow it
 Port  => shift || '6667',
 Nick  => 'iBot',
 Ircname  => 'I\'ve bee built by iNathan!',
 Username => 'iBot'
);

# We're going to add this to the conn hash so we know what channel we
# want to operate in.
$conn->{channel} = shift || '#MobilePassion';

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;
}

# The end of MOTD (message of the day), numbered 376 signifies we've connect
$conn->add_handler('376', \&on_connect);

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!");
}

$conn->add_handler('join', \&on_join);

$irc->start();

我该怎么做才能纠正这个问题?

3 个答案:

答案 0 :(得分:3)

#!/usr/bin/perl

在顶部。 / bin / sh通常不了解Perl,这就是你所看到的。

另外,我建议:

use strict;
use warnings;

答案 1 :(得分:2)

此外,我确信你已经在某个地方看到和听过这个,但是请帮自己一个忙,不要使用Net::IRC ...它已经死在水中自我宣传了7年。

新建议是使用POE::Component::IRC或某些变体。虽然POE::Component::IRC为您提供了对机器人功能的最大控制力,灵活性和可见性,但更简单的方法是Bot::BasicBot

希望有所帮助。

答案 2 :(得分:1)

在参考http://freetexthost.com/wdmcihuvxx时,您错过了网络库。根据您所使用的操作系统,您可以通过多种方式获取它 - 或者只使用CPAN。