【问题标题】:Some Simple Errors With a IRC Bot Made With Perl使用 Perl 制作的 IRC 机器人的一些简单错误
【发布时间】:2011-06-15 13:17:12
【问题描述】:

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

Nathan-Camposs-MacBook-Pro:Desktop Nathan$ ./bot.pl
./bot.pl:第 1 行:使用:找不到命令
./bot.pl:第 4 行:我的:找不到命令
./bot.pl:第 8 行:意外标记附近的语法错误 ('
./bot.pl: line 8:
my $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();

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

【问题讨论】:

    标签: perl cpan irc bots


    【解决方案1】:

    另外,我敢肯定你以前在某个地方见过和听过这个,但请帮自己一个忙,不要使用Net::IRC...它已经死在水里了 7 年了。

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

    希望对您有所帮助。

    【讨论】:

    • 但是如何更改我的代码以使其与POE::Component::IRC 一起使用?
    • 有几个POE::Component::IRC IRC 机器人教程,我很惊讶你居然找到了一个不适合这个模块的。幸运的是,现有示例中没有太多代码,移植也不会是太大的挑战。同样的原则也适用于注册某些 IRC 相关事件的钩子,并编写函数来服务这些钩子/事件。
    • 同样,可以在此处找到示例 POE::Component::IRC bot:supportforums.net/showthread.php?tid=10989,文档位于 search.cpan.org/~hinrik/POE-Component-IRC-6.52/lib/POE/…
    • 非常感谢。另外,我改进了你的答案的格式:)
    【解决方案2】:

    参考http://freetexthost.com/wdmcihuvxx,您缺少 Net 库。根据您使用的操作系统,您有多种获取方式 - 或者只使用 CPAN。

    【讨论】:

    • 我从 CPAN 收到这条消息:Refusing to install due to lack of confirmation at Makefile.PL line 28, <STDIN> line 1. Warning: No success on command[/usr/bin/perl Makefile.PL]
    • 当它询问时,您是否回答是(或任何它要求您回答的问题)?
    • 你是用 'perl Makefile.PL' 开始的吗?
    • 不需要(不过我通常直接使用 IO::Socket)。使用“perl Makefile.PL”运行 libnet 安装。从 CPAN 下载 lib 存档后,您可以找到该文件。
    【解决方案3】:
    #!/usr/bin/perl
    

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

    另外,我会推荐:

    use strict;
    use warnings;
    

    【讨论】:

    • 那将是另一个问题。将其作为问题而不是作为评论提出,以便人们更容易找到答案。 (但作为 10 的初学者,获取cpanmin.us,然后获取 cpanm Net::IRC,然后阅读说明。)
    猜你喜欢
    • 2011-09-01
    • 2011-06-16
    • 1970-01-01
    • 2012-10-07
    • 2015-01-21
    • 2011-08-02
    • 2014-10-05
    • 1970-01-01
    • 2011-02-27
    相关资源
    最近更新 更多