ilovechristy has asked for the wisdom of the Perl Monks concerning the following question:

Help, I'm trying to create a new post in my wordpress blog with custom fields using the following perl script using metaweblogAPI over XMLRPC, but there seems to be an issue with the custom fields. Only the second custom field (width) ever seems to get posted. Can't get the "height" to publish properly. When I add another field, I get the "Odd number of elements in anonymous hash" error. This has got to be something simple - would someone kindly sanity 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;
Buddha bless you.

Replies are listed 'Best First'.
Re: hash (within an array?) issue
by jwkrahn (Abbot) on Aug 30, 2009 at 01:04 UTC
    'custom_fields' => { { "key" => "height", "value" => 500 }, { "key" => "width", "value" => 750 } },

    The key custom_fields has a hash as its value.   The hash has the key { "key" => "height", "value" => 500 } and the value { "key" => "width", "value" => 750 }.   Since hash keys are converted to strings what you actually have is something like:

    'custom_fields' => { 'HASH(0x8153c28)' => { "key" => "width", "value" => 750 } },
      jwkrahn, your answer proved to be correct. I changed the {} to [] as follows:
      'custom_fields' => [ { "key" => "height", "value" => 500 }, { "key" => "width", "value" => 750 } ],
      and it did the trick! Thanks!
      Buddha bless you.
Re: hash (within an array?) issue
by stevieb (Canon) on Aug 30, 2009 at 01:11 UTC

    How does the parser interpret this?

    I'm looking at the code (without testing), and almost thinking that height => 500 and width => 750 instead of having the "key" and "value" words there.

    fwiw, "use diagnostics" may help in this case, as it may display more information on the issue at hand. I've found in the past that this type of warning is notifying you that you are trying to shove an unmatched number of elements into a hash.

    Steve