我从 https://metacpan.org/pod/Mastodon::Client 复制了以下示例代码,但它不起作用:
#!/usr/bin/env perl
use strict;
use warnings;
use Mastodon::Client;
my $client = Mastodon::Client->new(
instance=>'mastodon.cloud',
name=>'Perl watcher',
client_id=>'<myid>',
client_secret=>'<mysecret>',
access_token=>'<mytoken>',
coerce_entities=>1
) or die "Cannot connect";
my $listener = $client->stream( 'public' );
$listener->on( update => sub {
my ($listener, $status) = @_;
printf "%s said: %sn",
$status->account->display_name,
$status->content;
});
$listener->start;
它给了我以下错误:
Mastodon::Listener: 1 at/home/sergio/perl5/lib/perl5/Role/EventEmitter.pm 第 21 行。
这恰好发生在$listener->start;
行中,但我不知道为什么。
-- Emit error in Mastodon::Listener (0)
Mastodon::Listener: 1 at /home/sergio/perl5/lib/perl5/Role/EventEmitter.pm line 21.
^ ROLE_EVENTEMITTER_DEBUG=1 v perl -d:Confess mastodon.pl
Mastodon::Listener: 1 at /home/sergio/perl5/lib/perl5/Role/EventEmitter.pm line 21.
Role::EventEmitter::emit(Mastodon::Listener=HASH(0x55d97ff65e90), "error", 1, "Can't locate IO/Async/SSL.pm in @INC (you may need to install"..., ARRAY(0x55d980399a10)) called at /home/sergio/perl5/lib/perl5/Mastodon/Listener.pm line 172
Mastodon::Listener::__ANON__[/home/sergio/perl5/lib/perl5/Mastodon/Listener.pm:173](CODE(0x55d97fdca288), Mastodon::Listener=HASH(0x55d97ff65e90), "error", 1, "Can't locate IO/Async/SSL.pm in @INC (you may need to install"..., ARRAY(0x55d980399a10)) called at (eval 400)[/home/sergio/perl5/lib/perl5/Class/Method/Modifiers.pm:89] line 1
Mastodon::Listener::__ANON__[(eval 400)[/home/sergio/perl5/lib/perl5/Class/Method/Modifiers.pm:89]:1](Mastodon::Listener=HASH(0x55d97ff65e90), "error", 1, "Can't locate IO/Async/SSL.pm in @INC (you may need to install"..., ARRAY(0x55d980399a10)) called at (eval 402)[/home/sergio/perl5/lib/perl5/Class/Method/Modifiers.pm:148] line 2
Mastodon::Listener::emit(Mastodon::Listener=HASH(0x55d97ff65e90), "error", 1, "Can't locate IO/Async/SSL.pm in @INC (you may need to install"..., ARRAY(0x55d980399a10)) called at /home/sergio/perl5/lib/perl5/Mastodon/Listener.pm line 86
Mastodon::Listener::__ANON__[/home/sergio/perl5/lib/perl5/Mastodon/Listener.pm:86](undef, undef, ARRAY(0x55d980399ae8)) called at /home/sergio/perl5/lib/perl5/Mastodon/Listener.pm line 94
Mastodon::Listener::__ANON__[/home/sergio/perl5/lib/perl5/Mastodon/Listener.pm:94](undef) called at /home/sergio/perl5/lib/perl5/Net/Async/HTTP.pm line 905
Net::Async::HTTP::__ANON__[/home/sergio/perl5/lib/perl5/Net/Async/HTTP.pm:906]("Can't locate IO/Async/SSL.pm in @INC (you may need to install"...) called at /home/sergio/perl5/lib/perl5/Future.pm line 1008
Future::on_fail(Future=HASH(0x55d98037b678), CODE(0x55d98037af88)) called at /home/sergio/perl5/lib/perl5/Net/Async/HTTP.pm line 906
Net::Async::HTTP::do_request(Net::Async::HTTP=HASH(0x55d97ff65ef0), "uri", "https://mastodon.cloud/api/v1/streaming/public", "headers", HASH(0x55d980399a70), "on_error", CODE(0x55d980399ab8), "on_header", ...) called at /home/sergio/perl5/lib/perl5/Mastodon/Listener.pm line 146
Mastodon::Listener::start(Mastodon::Listener=HASH(0x55d97ff65e90)) called at mastodon.pl line 21
如果我在第 21 行/home/sergio/perl5/lib/perl5/Role/EventEmitter.pm
错误文件中将$_[0]
更改为 $_[1]
并安装了缺少的库 IO::Async::SSL,我会得到:
-- Emit error in Mastodon::Listener (0)
Mastodon::Listener: Error decoding JSON payload: Reference bless( do{(my $o = 0)}, 'JSON::PP::Boolean' ) did not pass type constraint "Bool" (in $args->{"locked"}) at /home/sergio/perl5/lib/perl5/Mastodon/Types.pm line 83
Reference bless( do{(my $o = 0)}, 'JSON::PP::Boolean' ) did not pass type constraint "Bool" (in $args->{"locked"})
"Bool" is defined as: (Type::Tiny::XS::Bool($_))
不幸的是,默认情况下,Role::EventEmitter 隐藏了该错误的详细信息。至少perl -d:Confess
在堆栈跟踪中显示有用的信息。
否则,您可以通过注册error
处理程序来恢复错误消息:
use Data::Dumper;
...
$listener->on( error => sub {
my ($is_fatal, $message, $details) = @_;
die "error ($is_fatal): $messagen" . Dumper($details);
});
实际的错误消息,
Can't locate IO/Async/SSL.pm in @INC (@INC contains: ...)
只是意味着您需要安装 IO::异步::SSL 模块。
... did not pass type constraint "Bool" (in $args->{"locked"}) ...
错误看起来像 Mastodon::Client 中的问题 #10。有一个补丁,但它还没有进入官方的Mastodon::客户端版本。
可能的解决方法可能是将 Type::Tiny 降级到版本 1.003002 或更低版本。