为什么在Perl中会收到“匿名哈希中的元素数量为奇数”的警告?

6

帮帮我,我正在使用以下perl脚本通过metaweblogAPI over XMLRPC在我的wordpress博客中创建一个新的帖子,并使用自定义字段,但是似乎自定义字段存在问题。只有第二个自定义字段(width)似乎被发布了。无法正确发布“height”。当我添加另一个字段时,我会收到“匿名哈希中的元素数量奇数”错误。这一定是什么简单的问题 - 请有人检查一下我的语法是否正确?谢谢。

#!/usr/bin/perl -w
use strict;
use RPC::XML::Client;
use Data::Dumper;

my $cli=RPC::XML::Client->new('http://www.sitename.com/wp/xmlrpc.php');

my $appkey="perl"; # doesn't matter
my $blogid=1; # doesn't matter (except blogfarm)

my $username="Jim";
my $passwd='_____';

my $text=<<'END';

This is the post content...

You can also include html tags...

See you!
END

my $publish=0; # set to 1 to publish, 0 to put post in drafts

my $resp=$cli->send_request('metaWeblog.newPost',
$blogid,
$username,
$passwd,
{
  'title'       => "this is doodoo",
  'description' => $text,
  'custom_fields' => {
    { "key" => "height", "value" => 500 },
    { "key" => "width", "value" => 750 }
  },
},
$publish);

exit 0;

请参考这个复制哈希引用的答案:https://dev59.com/oGw05IYBdhLWcg3wsz3N#7083603 - ophidion
1个回答

13

虽然在技术上是有效的语法,但它并没有做你想象中的事情。

'custom_fields' => {
    { "key" => "height", "value" => 500 },
    { "key" => "width", "value" => 750 }
},

大致相当于:

'custom_fields' => {
    'HASH(0x881a168)' => { "key" => "width", "value" => 750 }
},

这肯定不是你想要的。(0x881a168部分会有所变化,实际上是哈希引用存储的地址。)

我不确定自定义字段的正确语法是什么。你可以尝试

'custom_fields' => [
    { "key" => "height", "value" => 500 },
    { "key" => "width", "value" => 750 }
],

这将设置custom_fields为一个哈希数组。但这可能不正确。这取决于send_request的期望值。


CJM,你太棒了!你的建议起作用了。两个自定义字段都成功发布了;)非常感谢! - Jim

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接