【发布时间】:2023-04-10 22:38:01
【问题描述】:
我的应用程序配置为发送 JSON 响应。我需要向它添加一个 RSS(或其他任何东西)端点。没有无操作序列化器,尽管写一个很简单:
{ # do not use this lol
package Dancer2::Serializer::ThisIsNotAnOkayThingToDo_Raw;
use Moo;
with 'Dancer2::Core::Role::Serializer';
sub serialize { $_[1] }
sub deserialize { $_[1] }
1;
}
get '/mything/rss' => sub {
my $rss = new XML::RSS (version => '2.0');
$rss->channel(title => "Wharrgarbl");
$rss->add_item(title => "Potato");
send_as(ThisIsNotAnOkayThingToDo_Raw => $rss->as_string, {content_type => 'application/rss+xml; charset=UTF-8'});
}
但是,这失败了,声称 __PACKAGE__::send_as 未定义(但在服务器上运行 perldoc Dancer2::Manual 确实说 send_as 应该在那里)。
# __PACKAGE__ is me redacting the sensitive out of the error message
Undefined subroutine &__PACKAGE__::send_as called at...
文档还声称send_as 使用send_file,所以我试图去掉中间人:
get '/mything/rss' => sub {
my $rss = new XML::RSS (version => '2.0');
$rss->channel(title => "Wharrgarbl");
$rss->add_item(title => "Potato");
my $xml = $rss->as_string;
send_file(\$xml, content_type => 'application/rss+xml; charset=UTF-8');
}
然而,这会通过序列化程序:
hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this) at .../site/lib/JSON.pm line 154.
做什么?
【问题讨论】: