Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

getting values out of RPC::XML response array

by inblosam (Monk)
on May 21, 2004 at 04:52 UTC ( [id://355181]=perlquestion: print w/replies, xml ) Need Help??

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

I am now trying to use RPC::XML, and I now get results. When I use Data::Dumper it looks like the output below. I have tried several different things but cannot get the values out (like 333333333333333, heyheywearethemonkeys (from 'thiskey'), and item1 from the 'thattoo' array. I have searched and searched but all of the examples I have seen give me an error, like "can't coerce array into hash".
... my $req = RPC::XML::request->new('search', @paramfields); my $resp = $client->send_request($req); print "Response is $resp\n"; print Dumper(\$resp); ### NOW I WANT TO GET SOME VALUES OUT ### but I can't figure it out! ### am I missing some critical documentation? ### I tried to read on CPAN but that didn't help or make sense to me
Here is what Data::Dumper shows:
Response is RPC::XML::array=ARRAY(0x8465fb0) $VAR1 = \bless( [ bless( do{\(my $o = '333333333333333')}, 'RPC::XML::int' ), bless( do{\(my $o = '0.158333')}, 'RPC::XML::double' ), bless( [ bless( { '' => bless( do{\(my $o = '')}, 'RPC::XML::string' ), 'thiskey' => bless( do{\(my $o = 'heyheywearethemonkeys')} +, 'RPC::XML::string' ), 'that' => bless( do{\(my $o = '6')}, 'RPC::XML::int' ), 'thistoo' => bless( do{\(my $o = 'Joe')}, 'RPC::XML::strin +g' ), 'thattoo' => bless( [ bless( do{\(my $o = 'item1')}, 'RPC::XML::string' ), bless( do{\(my $o = 'item2')}, 'RPC::XML::string' ), bless( do{\(my $o = 'item3')}, 'RPC::XML::string' ) ], 'RPC::XML::array' ), 'something' => bless( do{\(my $o = '')}, 'RPC::XML::string +' ), 'somethingelse' => bless( do{\(my $o = 'EFG')}, 'RPC::XML: +:string' ), 'hello' => bless( do{\(my $o = '2003')}, 'RPC::XML::int' ) }, 'RPC::XML::struct' ), bless( { '' => bless( do{\(my $o = '')}, 'RPC::XML::string' ), 'hola' => bless( do{\(my $o = '2940')}, 'RPC::XML::string' + ), 'howdy' => bless( do{\(my $o = '6')}, 'RPC::XML::int' ), 'doody' => bless( do{\(my $o = 'Smithton')}, 'RPC::XML::st +ring' ), 'a_list' => bless( [ bless( do{\(my $o = 'getitem1')}, 'RPC::XML::string' ) +, bless( do{\(my $o = 'getitem2')}, 'RPC::XML::string' ) +, bless( do{\(my $o = 'getitem3')}, 'RPC::XML::string' ) ], 'RPC::XML::array' ), 'hi' => bless( do{\(my $o = '')}, 'RPC::XML::string' ), 'yo' => bless( do{\(my $o = 'HIJ')}, 'RPC::XML::string' ), 'ciao' => bless( do{\(my $o = '1951')}, 'RPC::XML::int' ) }, 'RPC::XML::struct' ), ], 'RPC::XML::array' ) ], 'RPC::XML::array' );

OKAY...UPDATE...
I tried something else and now I get this structure from Data::Dumper. It is a ton easier to read, but when I try to get the values out it tells me "Global symbol "@result" requires explicit package name". I was trying:
print $result[0] or $result[4]{'thiskey'}, "\n";
Update again
Okay I did this:
my @newresult = $result; print $newresult[0][0], "\n";
and that gave me the 'this' value. Now I just need to figure how to get values out of the hash.
and this is the data:
$VAR1 = \[ 'this', 'that', 'hmmm', 'ok', [ { '' => '', 'somelist' => [ 'item1', 'item2', 'item3' ], 'thiskey' => 'nomoremonkeysjumpingonthebed', 'hiii' => '0.200000', }, { '' => '', 'somelist' => [ 'item1', 'item2', 'item3' ], 'thiskey' => 'nomoremonkeysjumpingonthebed', 'hiii' => '0.200000', }, ] ];
Any help would be much appreciated! THANKS!

Replies are listed 'Best First'.
Re: getting values out of RPC::XML response array
by inblosam (Monk) on May 21, 2004 at 12:49 UTC
    Okay, I'm having organizational problems. I figured it out for the Frontier::Client code, but not the RPC::XML code, with the OO bless stuff. So if you have any ideas on how to get that out, please help!
    my @response = $resp; print "Result is $response[0][0]\n";
    This gives me:
    Result is RPC::XML::int=SCALAR(0x8123c90)
      Well, what you got to do is to really really read what author says about RPC::XML methods. I know it sounds kind of harsh, but I know it, because i didn't read carefully and therefore decided to use Frontier::RPC2 which was a HUGE mistake...but first things first:

      best explanation is always an example so here is very simple RPC::XML server
      #!/usr/bin/perl -w use RPC::XML; use RPC::XML::Server; use Data::Dumper; # xml rpc server $server = RPC::XML::Server->new(host => 'localhost', port => '8888', q +ueue=>'5', no_default=>1, no_http=>0); $server->add_method({ name => 'test', signature => [ 'struct struct' +], code => \&test }); $server->server_loop; sub test { print Dumper(\@_); my $serv = shift; my $hash = shift; print Dumper(\$hash); my %hash = %$hash; print Dumper(\%hash); if ($hash{called_number_int}) { return {'answer' => 'OK bejby'}; } else { return RPC::XML::fault->new('444', 'not good'); } }
      And here's a client to that server:
      #!/usr/bin/perl -w require RPC::XML; require RPC::XML::Client; use Data::Dumper; my @params = ({ 'called_number_net'=>{"nat"=>3,"number"=>223344600 +}, 'called_number_int'=>'+420223344600', 'event_type'=>1, }); $cli = RPC::XML::Client->new('http://localhost:8888/RPC2'); $request = RPC::XML::request->new('test', @params); $resp = $cli->send_request($request); print Dumper(\$resp); $hash = $resp->value; print Dumper (\$hash); print $hash->{answer}; print "\n";
      now if you will start the server and call the client, here's what you will get... server:
      ./rpcxmlserver.pl $VAR1 = [ bless( { '__response' => bless( { '_content' => '', '_rc' => 200, '_headers' => bless( { 'co +ntent-type' => 'text/xml', 'ac +cept' => 'text/xml', 'ac +cept-encoding' => 'deflate', 'rp +c-server' => 'RPC::XML::Server/1.44', 'rp +c-encoding' => 'XML-RPC' }, 'H +TTP::Headers' ), '_msg' => 'OK' }, 'HTTP::Response' ), 'method_name' => 'test', '__started' => 1204548264, '__auto_methods' => 0, '__method_table' => { 'test' => bless( { 'signature +' => [ + 'struct struct' + ], 'sig_table +' => { + 'struct' => 'struct' + }, 'name' => +'test', 'code' => +sub { "DUMMY" } }, 'RPC::XML +::Method' ) }, '__compress_re' => qr/(?-xism:deflate)/, '__message_temp_dir' => '', '__auto_updates' => 0, '__compress' => 'deflate', '__daemon' => bless( \*Symbol::GEN0, 'HTTP::Daemon' + ), '__message_file_thresh' => 1048576, '__xpl_path' => [], '__version' => 'RPC::XML::Server/1.44', '__path' => '', '__timeout' => 10, '__compress_thresh' => 4096, '__debug' => 0, 'signature' => [ 'struct', 'struct' ], '__requests' => 0, '__parser' => bless( { 'stack' => [], 'cdata' => '+420223344600' }, 'RPC::XML::Parser' ), '__port' => '8888', '__host' => 'localhost' }, 'RPC::XML::Server' ), { 'called_number_net' => { 'nat' => '3', 'number' => '223344600' }, 'event_type' => '1', 'called_number_int' => '+420223344600' } ]; $VAR1 = \{ 'called_number_net' => { 'nat' => '3', 'number' => '223344600' }, 'event_type' => '1', 'called_number_int' => '+420223344600' }; $VAR1 = { 'called_number_net' => { 'nat' => '3', 'number' => '223344600' }, 'event_type' => '1', 'called_number_int' => '+420223344600' };
      and client:
      ./rpcxmlklient.pl $VAR1 = \bless( { 'answer' => bless( do{\(my $o = 'OK bejby')}, 'RPC: +:XML::string' ) }, 'RPC::XML::struct' ); $VAR1 = \{ 'answer' => 'OK bejby' }; OK bejby
      So as you can see, there's a difference between getting data from RPC::XML in server and in client and you probably need both of them...
      in server it's these lines:
      my $serv = shift; my $hash = shift; my %hash = %$hash;
      and it works like this - a) 1st shift loads info from rpc xml, that is not your data...and if you think it's huge, try to change no_default=>1 parameter in server to no_default=>0 ;]
      b) 2nd shift loads reference to your data
      c) and finally 3rd line makes an object from reference

      in client it's
      $hash = $resp->value;

      And that's it ;)

      P.S. - in case you were wondering why NOT to use Frontier::RPC2... apart from many other small things, that can be finally somehow fixed, it CAN'T work with the right format for fault codes and strings, they just aren't there, so by any mean you add them, the output xml rpc string won't be in the right format. RPC::XML on the other hand works with faults like a charm...if you'll remove parameter called_number_int from client, you'll get it.
Re: getting values out of RPC::XML response array
by inblosam (Monk) on May 21, 2004 at 12:31 UTC
    Figured it out, I think.

    Looks like if I do this, I can get what I need:
    print "$newresult[0][4][0]{'thiskey'} is my value\n";

    Just needed a sounding board, I guess.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://355181]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2024-04-25 16:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found