Question

I'm new in using Perl and am currently trying to work with XML-RPC. Can anyone help me to retrieve from the sample below the SEQUENCE (which should be 0) and the LANGUAGE (which shoud be FRA)?

Thanks in advance for your help.

$VAR1 = bless( {
             'args' => [
                         bless( {
                                  'SEQUENCE' => bless( do{\(my $o = '0')}, 'RPC::XML::string' ),
                                  'LANGUAGE' => bless( do{\(my $o = 'FRA')}, 'RPC::XML::string' ),
                                  'END_OF_SESSION' => bless( do{\(my $o = 'FALSE')}, 'RPC::XML::string' ),

                                }, 'RPC::XML::struct' )
                       ],
             'name' => 'get.getMethod'
           }, 'RPC::XML::request' );
Was it helpful?

Solution

It appears that your string is an output from Data::Dumper perhaps? No matter the source, it can be turned into a data structure using an eval.

Then it's just a matter of a bit of decyphering of the RPC::XML documentation to come up with the following:

use strict;
use warnings;
use RPC::XML;

my $str = do { local $/; <DATA> };

my $req = do {
    no strict 'vars';
    eval $str;
} or die "Error in data, $@";

### You should already have a $req equivalent object.

my $hashref = ${$req->args}[0]->value;

while (my ($k, $v) = each %$hashref) {
    print "$k -> $v\n";
}

__DATA__
$VAR1 = bless( {
    'args' => [
        bless( {
            'SEQUENCE' => bless( do{\(my $o = '0')}, 'RPC::XML::string' ),
            'LANGUAGE' => bless( do{\(my $o = 'FRA')}, 'RPC::XML::string' ),
            'END_OF_SESSION' => bless( do{\(my $o = 'FALSE')}, 'RPC::XML::string' ),
        }, 'RPC::XML::struct' )
    ],
    'name' => 'get.getMethod'
}, 'RPC::XML::request' );

Outputs:

SEQUENCE -> 0
LANGUAGE -> FRA
END_OF_SESSION -> FALSE

OTHER TIPS

author of RPC::XML here.

Assuming that your request object is named $req, you can get the arguments list with the "args" method:

my $args = $req->args;

Since you are looking for data from the first (and only argument), which is a "struct" (hash table), you would use index [0] on the arguments, i.e., $args->[0], as a hash reference.

But! This is the request object, so the data has already been encoded as an object so that it can be serialized when the request is sent to a server. So $args->[0] is actually an object of the RPC::XML::struct class. Likewise, the values within the struct have been encoded as well. This is why the data-dumper output shows all the "bless" calls to various RPC::XML::* classes.

Fortunately, there is a method that all the data classes have in common, called value. And better yet, it operates recursively on the compound data-types of RPC::XML::struct and RPC::XML::array. This means that calling value on this particular argument-object will not only give you an ordinary hash reference, but all the values within that hash reference will be Perl native types rather than other RPC::XML::* objects.

So this will get you to where you want to be:

my $args    = $req->args;
my $hashref = $args->[0]->value;
# At this point, you can now access $hashref->{SEQUENCE} and $hashref->{LANGUAGE}

Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top