Sending Arrays with Remote Method Calls

This section provides a tutorial example on how to send and received an array as an input of the remote method call. All arrays are passed as hard references.

A reader of my book mailed me a sample RPC::XML server program that failed to receive an array from the client. The cause of the problem was related to how an input argument of an array is delivered to the receiving method on the server side. The rule of passing arrays is simple: "All arrays are passed as hard references". Here is the modified sample server program, RpcXmlServer2.pl:

#- RpcXmlServer2.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.

   use RPC::XML;
   use RPC::XML::Server;
   my $host = "localhost";
   my $port = "8001";
   my $daemon = RPC::XML::Server->new(host => $host, port => $port);
#
   print "Adding com.herong.saveArray...\n";
   my $method = RPC::XML::Method->new({
      name      => 'com.herong.saveArray',
      signature => ['string array'],
      code      => \&saveArray});
   $daemon->add_method($method);
#
   print "Listening at $host:$port...\n";
   $daemon->server_loop();

sub saveArray {
   my $s = shift; # The first parameter is the server object
#   my @a = shift; # Here is the mistake of the original program
   my $a = shift; # The array is received as a reference scalar
   my @a = @$a; # Taking the array out of reference
   open(MYFILE, "> myfile.txt") or die "cannot open the file";
   foreach my $line (@a) {
      print MYFILE "$line\n";
   }
   close MYFILE;
   return "Array saved ok.";
}

As you can see in the saveArray() method, the mistake of the original program is that the input argument is assigned to an array directly. There was no syntax error, but the array would contain only element representing the hard reference of the argument, which is an array. The output file would be a string of "ARRAY(0x...)"

The sample client program is also modified as RpcXmlClient2.pl:

#- RpcXmlClient2.pl
#- Copyright (c) HerongYang.com. All Rights Reserved.

   require RPC::XML;
   require RPC::XML::Client;
   my $client = RPC::XML::Client->new('http://localhost:8001');

   print "\nCalling com.herong.saveArray...\n";
   my $var = RPC::XML::array->new("one", "two");
   my $res = $client->send_request('com.herong.saveArray', $var);
   print "   Response string = ".$res->as_string."\n";
   print "   Response value = ".$res->value."\n";

   exit;

If you the server program first, then the client program, you will get the correct data in the output file:

one
two

This sample server program also shows you:

Table of Contents

 About This Book

 Perl on Linux Systems

 ActivePerl on Windows Systems

 Data Types: Values and Variables

 Expressions, Operations and Simple Statements

 User Defined Subroutines

 Perl Built-in Debugger

 Name Spaces and Perl Module Files

 Symbolic (or Soft) References

 Hard References - Addresses of Memory Objects

 Objects (or References) and Classes (or Packages)

 Typeglob and Importing Identifiers from Other Packages

 String Built-in Functions and Performance

 File Handles and Data Input/Output

 Open Files in Binary Mode

 Open Directories and Read File Names

 File System Functions and Operations

 Image and Picture Processing

 Using DBM Database Files

 Using MySQL Database Server

 Socket Communication Over the Internet

 XML::Simple Module - XML Parser and Generator

 XML Communication Model

 SOAP::Lite - SOAP Server-Client Communication Module

 Perl Programs as IIS Server CGI Scripts

 CGI (Common Gateway Interface)

 XML-RPC - Remote Procedure Call with XML and HTTP

RPC::XML - Perl Implementation of XML-RPC

 What Is RPC::XML?

 RPC::XML::Server - XML-RPC Server Interface Class

 RPC::XML Client and Data Classes

 Installing NMake 1.5

 Installing RPC::XML Module

 RpcXmlServer.pl - RPC::XML Server Sample Program

 RpcXmlClient.pl - RPC::XML Client Sample Program

Sending Arrays with Remote Method Calls

 Integrating Perl with Apache Web Server

 CGI.pm Module for Building Web Pages

 LWP::UserAgent and Web Site Testing

 Converting Perl Script to Executable Binary

 Managing Perl Engine and Modules on macOS

 Archived Tutorials

 References

 Full Version in PDF/EPUB