Thursday, 8 November 2007

SOAP::Lite for Perl Tutorial

SOAP or XML is used as a common communication standard between the applications. If you are building a Perl application, which needs to communicate with other application, then here is a small tutorial.

Package to be used : SOAP::Lite
Source : http://soaplite.com/download.html

Client Code:
#!perl -w

use strict;
use SOAP::Lite;
use Data::Dumper;
my $service = SOAP::Lite
-> proxy("$URL"); #Target URL

my $result = $serive->methodName(); #Target Methodname
print "Result :".Dumper($result);

As an alternative we can use WSDL file instead of URL, It goes like this.

#!perl -w

use strict;
use SOAP::Lite;
use Data::Dumper;
my $service = SOAP::Lite
my $service = SOAP::Lite->service('http://url?wsdl');
my $result = $serive->methodName(); #Target Methodname
print "Result :".Dumper($result);
The method we have called here does not have any input parameters. Normally SOAP body will not be empty (target method will always require some input params). Now lets see how we have to pass the parameters to the target method.

#!perl -w

use strict;
use SOAP::Lite;
use Data::Dumper;
my $service = SOAP::Lite
-> proxy("$URL"); #Target URL
my $result = $serive->methodName('param1', 'param2', ...); #Target Methodname
print "Result :".Dumper($result);

The parameter type depends on the value of the parameter. If the value is int, it is by default int. hence if you have to pass the integer value as string, you might have to pass the parameters in a different way.

#!perl -w

use strict;
use SOAP::Lite;
use Data::Dumper;
my $service = SOAP::Lite
-> proxy("$URL"); #Target URL
my @params = (
SOAP::Data->name("param_name1" => SOAP::Data->type("type" => 'value1'),
SOAP::Data->name("param_name2" => SOAP::Data->type("type" => 'value2'),
my $result = $serive->methodName(@params);
print "Result :".Dumper($result);
Here "type" can be string/long/int or anything, that is acceptable by the SOAP:Lite package.
Till Now we are able to create SOAP requests, which can have simple parameters in SOAP:Body. What if we have to send the nested parameters? If we have to take an example,


value1

subvalue1




The perl code above is not able to build this nested request. hence we are going to modify it to a little extent to meet our requirements.

#!perl -w

use strict;
use SOAP::Lite;
use Data::Dumper;
my $service = SOAP::Lite
-> proxy("$URL"); #Target URL
my @params = (
SOAP::Data->name("param_name1" => SOAP::Data->type("type" => 'value1'),
SOAP::Data->name("param_name2" => SOAP::Data->type("type" => 'value2'),
my $result = $serive->methodName(@params);
print "Result :".Dumper($result);

No comments: