Why am I getting the warning “An odd number of elements in an anonymous hash” in Perl?

Reference. I am trying to create a new Wordpress blog post with custom fields using the following perl script using metaweblogAPI on top of XMLRPC, but there seems to be a problem with custom fields. It seems that only the second user field (width) appears. It is not possible to post "height" correctly. When I add another field, I get the error "An odd number of elements in anonymous hashing." It should be something simple - can someone honestly check my syntax? Thanks.

#!/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; 
+6
perl warnings
source share
1 answer

While technically sound syntax, it does not do what you think.

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

roughly equivalent to something like:

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

which, of course, is not what you want. (Part 0x881a168 will change, this is actually the address where hashref is stored.)

I am not sure what the correct syntax is for custom fields. You can try

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

which sets custom_fields to a hash array. But this may be wrong. It depends on what send_request expects.

+13
source share

All Articles