我正在尝试向 Mojo::UserAgent 添加一个角色来处理对 wikidata SPARQL 服务的查询。
特别是,我希望能够修改响应,以便服务提供的 JSON 更可用。
其要点是我希望能够写作
my $ua = Mojo::UserAgent->new->with_roles('+Wikidata');
my $tx = $ua->query($some_sparql); # ->query is defined by Mojo::UserAgent::Role::Wikidata
my $items = $tx->res->items; # this is the crux of the question
但这意味着 - 据我所知:
问题是:
我只是创建一个包装器:
sub query {
... do whatever you need to make the request from the query ...
my $tx = $ua->get( ... );
... do whatever you want to extract results ...
}
我宁愿将用户代理级别和应用程序级别分开。由于您添加的功能与成为用户代理无关,因此我认为您不应该将其添加为用户代理对象。