XML-RPC w/ Frontier::Client ARRAY() Problem

S

ssims

Let me start by saying, please forgive me if this has been discussed
before...I searched the group and didn't see it anywhere. Now...

I'm trying to interface with a XML-RPC server provided by my professor
for an assignment. The service has the following methods:

getMethods - Takes no arguments and returns a list of the methods
contained in the service.
getPrice - Takes a string denoting the beer brand and returns a double
representing the beer price.
getBeers - Takes no arguments and returns a list of the known beers.
getCheapest - Takes no arguments and returns the name of the least
expensive beer.
getCostliest - Takes no arguments and returns the name of the most
expensive beer.

I'm fine getting single items returned, it's for the getMethods and
getBeers where I'm stuck. My code is as follows:

- - - - -[begin code]- - - - -
use Frontier::Client;

my $server_url = 'http://frisbee.rit.edu:8200';
my $server = Frontier::Client->new('url' => $server_url,debug);
my $result;

$result = $server->call('beer.getMethods');
print "Methods: " . $result . "\n";

$result = $server->call('beer.getPrice','Genesee');
print "Price for Genesee: " . $result . "\n";

$result = $server->call('beer.getBeers');
print "Beers: " . $result . "\n";

$result = $server->call('beer.getCheapest');
print "Cheapest: " . $result . "\n";

$result = $server->call('beer.getCostliest');
print "Costliest: " . $result . "\n";
- - - - -[end code]- - - - -

For the Methods and the Beers I get an ARRAY(0x...) value. I have
tried the standard array methods, like foreach(@array) { print $_ } and
foreach $item (@array) { print $item } and nothing seems to work. How
can I get the data from the "ARRAY()" object? The Frontier::Client
documentation doesn't shed any light, and the perl array documentation
hasn't been helpful as of yet. Thanks for any and all guidance.

-Sean
 
E

Eric Bohlman

For the Methods and the Beers I get an ARRAY(0x...) value. I have

That means you're getting a *reference* to an array.
tried the standard array methods, like foreach(@array) { print $_ } and
foreach $item (@array) { print $item } and nothing seems to work. How

Those constructs work on actual arrays, not array references. However, you
can get an array out of an array reference by dereferencing it. Read the
perlreftut and perlref documents.
 
S

ssims

Okay, so after reading the perlref doc, I thought I was all set.
Unfortunately, it didn't work...

I changed:
$result = $server->call('beer.getMethods');
print "Methods: " . $result . "\n";

to read:
@result = $server->call('beer.getMethods');
print "@result\n"; # Returns the ARRAY ref.
$resultref = \@result;
print "$resultref\n"; # Returns the reference to the array
print "$resultref->[0]\n"; # Should return the first value of the array
references in @result, but it doesn't.

And the output is:
ARRAY(0x1d0ec2c)
ARRAY(0x1831c98)
ARRAY(0x1d0ec2c)

So am I correct in assuming that @result contains a pointer to an
array, and resultref contains a pointer to another array which also
contains a pointer to an array? Seems to be a little screwed up... I
even tried another service, thinking this service may be messed up, but
that too yields multiple array references. Help! please...I'm not sure
where to go with this.
 
G

Gunnar Hjalmarsson

ssims said:
Okay, so after reading the perlref doc, I thought I was all set.
Unfortunately, it didn't work...

I changed:
$result = $server->call('beer.getMethods');
print "Methods: " . $result . "\n";

to read:
@result = $server->call('beer.getMethods');

That gives you an array with an array ref as the only element.
print "@result\n"; # Returns the ARRAY ref.
$resultref = \@result;

That gives you a reference to the array of arrays...
print "$resultref\n"; # Returns the reference to the array
print "$resultref->[0]\n"; # Should return the first value of the array
references in @result, but it doesn't.

No, that prints the original array ref.

Try:

my $result = $server->call('beer.getMethods');
print "Methods:\n";
print "$_\n" for @$result;
 
S

ssims

Thank you for your help. Now that I see how you did it, it makes
perfect sense. I think I making it way more complicated then it
actually is. Thanks again.

-Sean
 
T

Tad McClellan

ssims said:
Thank you for your help. Now that I see how you did it, it makes
perfect sense. I think I making it way more complicated then it
actually is. Thanks again.


It often helps to get your head around a data structure by
dumping it out to see what you've got.

use Data::Dumper;

...

print Dumper \@result;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top