Calebotomy

Adventures with SOAP using Perl: Part 2 ( SOAP::Data::Builder )

Start by reading the first 2 parts :

  1. Part 0 Prelude (setup server.pl)
  2. Part 1 SOAP::Lite

SOAP::Data::Builder is simply a wrapper around SOAP::Data and SOAP::Serializer for SOAP::Lite. I used it because it made my life easier building nested complicated SOAP objects. However for Part 2 I will simply be showing how to use it to do the same code as Part 1. Unfortunately since Part 1 is so Simple this actually makes SOAP::Data::Builder more complex than SOAP::Lite would be for this. In a future installment I will attempt to show more complex examples, but I will explain them less. Now let’s take a look at the code.

#!/usr/bin/perl
use 5.010;
use strict;
use warnings;
use SOAP::Lite +trace => [ ‘debug’ ];
use SOAP::Data::Builder;

my $req = SOAP::Lite->new(
readable => 1,
proxy => ‘http://localhost:8877',
ns => ‘http://namesservice.thomas_bayer.com/',
);

my $sb = SOAP::Data::Builder->new;

$sb->add_elem(
name => ‘name’,
value => ‘Mark’,
);

my $res = $req->getNameInfo( $sb->to_soap_data );

say ‘-’ x 3;

unless ( defined $res->fault ) {
say scalar $res->valueof(‘//country’);
} else {
say $res->fault->{faultstring};
say $res->fault->{detail}{error};
}

As you can see this is much the same as the final code in Part 1, and if you run it, it does exactly the same thing. The first difference you’ll notice is the self explanatory creation of the SOAP::Data::Builder object. After that comes the ->add_elem method call, which will simply create an element with a given element name and a value for that element.

Once you’ve finished adding elements to your $sb object, then you can call pass $sb->to_soap_data to the method that you’re calling on SOAP::Lite, this generates the structure that SOAP::Lite needs to be able to make your request.

The only thing I didn’t like about SOAP::Data::Builder is if you don’t pass the right parameters to ->add_elem it will not croak or error in any way that will really tell you what went wrong. Simple patches to this can fix it.


Share

comments powered by Disqus