问题描述
我正在探索 AnyEvent::IRC::Client 库以将消息列表发送到 IRC 频道。我遵循其 Metacpan 站点(参考:https://metacpan.org/pod/AnyEvent::IRC::Client)中的示例代码,但我需要发送消息列表而不是单个消息。因此,我能够成功地将数组中的所有消息发送到 IRC 通道。问题是事件循环在最后没有关闭(即我必须按 Ctrl+C 来终止程序)。因此,任何有关如何关闭事件循环的见解将不胜感激。谢谢!
# Send some messages to IRC channel
use strict ;
use warnings 'all' ;
no warnings 'uninitialized' ;
use AnyEvent ;
use AnyEvent::IRC::Client ;
use Data::Dumper ;
sub say { print @_,"\n" }
my @messages = ( 'msg1','msg2','msg3','msg4','msg5','msg6','msg7','msg8','msg9','msg10' ) ;
sendIrc( 'ircServer',6667,'#ircChannel1','user123','psswd',\@messages ) ;
sub sendIrc {#Params: ircServer(Str),port(Int),channel(Str),nick(Str),psswd(Str),messages(ArrRef<Str>) ; #Return: void ;
my ( $server,$port,$chan,$nick,$psswd,$messages ) = @_ ;
my $timer ;
my $condVar = AnyEvent->condvar ;
my $con = AnyEvent::IRC::Client->new ;
$con->reg_cb( connect => sub {
my ( $cond,$err ) = @_ ;
if ( defined $err ) {
warn "connect error: $err\n" ;
return ;
}#end if
}) ;
$con->reg_cb( registered => sub {
say "User is in!" ;
}) ;
$con->reg_cb( disconnect => sub {
say "User is out!" ;
}) ;
$con->reg_cb( sent => sub {
my ( $con ) = @_ ;
if ( $_[2] eq 'PRIVMSG' ) {
say "Msg sent!" ;
$timer = AnyEvent->timer (
after => 1,cb => sub {
undef $timer ;
$condVar->end ;
}#end callback
);#end timer
}#end if
}) ;
$con->connect( $server,{ nick => $nick,password => $psswd } ) ;
for my $msg ( @$messages ) {
$condVar->begin ;
$con->send_srv( PRIVMSG => ( $chan,$msg ) ) ;
}#end for
$condVar->wait ;
$con->disconnect ;
return ;
}#end sub
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)