Wrapping up a SOAP::Lite service in a class

Wrapping up a SOAP::Lite service in a class

am 06.02.2008 12:09:34 von Mark Knoop

Hi

It's Mark's class workshop today :)

I want to wrap up a SOAP service handle (returned from a call to a wsdl
file) in a class (ultimately so I can make it a singleton and only call it
once but we'll leave that for the purposes of this example).

So if I have something like this (calling a random public SOAP service)

use strict;
use warnings;
use SOAP::Lite;
use Data::Dumper;
my $service =
SOAP::Lite->service('http://ws.cisa.ca/WehireWS/JobsWs.asmx? WSDL');
my $result = $service->GetAll;
print Dumper($result);

It works fine and I get

$VAR1 = {
'JobDetail' => {
'PostingDate' => '1/30/2006',
'Location' => 'British Columbia',
'Url' =>
'http://www.wehire.ca/view.php?job_id=1408',
'Category' => 'Construction',
'Company' => 'XS West Construction Group',
'Title' => 'Equipment Operator'
}
};

However if I try to abstract it into my own class eg

#############

package Soap;
use strict;
use warnings;
use SOAP::Lite;

sub new {

my $class = shift;

my $self =
SOAP::Lite->service('http://ws.cisa.ca/WehireWS/JobsWs.asmx? WSDL');

bless ($self, $class);

return $self;

}

1;

################

use strict;
use warnings;
use Soap;
use Data::Dumper;
my $service = Soap->new;
my $result = $service->GetAll;
print Dumper($result);

#################

I get

Can't locate object method "GetAll" via package "Soap" at soap2.pl line 6.

I don't expect anyone to read the SOAP::Lite documentation for me but I am
wondering whether there is anything fundamentally flawed in my approach?

Thanks
Mark

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

RE: Wrapping up a SOAP::Lite service in a class

am 06.02.2008 13:22:38 von Brian Raven

Mark Knoop <> wrote:
> Hi
> =

> It's Mark's class workshop today :)
> =

> I want to wrap up a SOAP service handle (returned from a call to a
> wsdl =

> file) in a class (ultimately so I can make it a singleton and only
> call it once but we'll leave that for the purposes of this example). =

> =

> So if I have something like this (calling a random public SOAP
> service) =

> =

> use strict;
> use warnings;
> use SOAP::Lite;
> use Data::Dumper;
> my $service =3D
> SOAP::Lite->service('http://ws.cisa.ca/WehireWS/JobsWs.asmx? WSDL');
> my $result =3D $service->GetAll;
> print Dumper($result);
> =

> It works fine and I get
> =

> $VAR1 =3D {
> 'JobDetail' =3D> {
> 'PostingDate' =3D> '1/30/2006',
> 'Location' =3D> 'British Columbia',
> 'Url' =3D>
> 'http://www.wehire.ca/view.php?job_id=3D1408',
> 'Category' =3D> 'Construction',
> 'Company' =3D> 'XS West Construction Group',
> 'Title' =3D> 'Equipment Operator'
> }
> };
> =

> However if I try to abstract it into my own class eg
> =

> #############
> =

> package Soap;
> use strict;
> use warnings;
> use SOAP::Lite;
> =

> sub new {
> =

> my $class =3D shift;
> =

> my $self =3D
> SOAP::Lite->service('http://ws.cisa.ca/WehireWS/JobsWs.asmx? WSDL');
> =

> bless ($self, $class);
> =

> return $self;
> =

> }
> =

> 1;
> =

> ################
> =

> use strict;
> use warnings;
> use Soap;
> use Data::Dumper;
> my $service =3D Soap->new;
> my $result =3D $service->GetAll;
> print Dumper($result);
> =

> #################
> =

> I get
> =

> Can't locate object method "GetAll" via package "Soap" at soap2.pl
> line 6. =

> =

> I don't expect anyone to read the SOAP::Lite documentation for me but
> I am wondering whether there is anything fundamentally flawed in my
> approach? =


Yes there is. Basically, you are re-blessing the object returned by
SOAP::Lite::service into your package namespace, and I don't see a
subroutine named GetAll defined there. You should store the returned
object in a container (hash or array), which you bless into your package
namespace. But that only fixes the first part of your problem, you still
don't have a subroutine named GetAll. You can delegate subroutine calls
to your class using the AUTOLOAD feature, see at least 'perldoc perlobj'
and 'perldoc perltoot' for help with that.

However, do you really want to go to all that trouble when all you seem
to be doing is hiding the service URL and only calling
SOAP::Lite::service once? The following would seem to be a simpler way
for Soap.pm to accomplish that:

----------------------------------
use strict;
use warnings;

package Soap;

use SOAP::Lite;

our $service;
our $url =3D 'http://ws.cisa.ca/WehireWS/JobsWs.asmx?WSDL';

sub service {
$service =3D SOAP::Lite->service($url)
unless defined $service;
return $service;
}

1;
----------------------------------

Then all you need in Soap.pl is:

----------------------------------
use strict;
use warnings;

use Soap;
use Data::Dumper;

my $result =3D Soap::service->GetAll;
print Dumper($result);
----------------------------------

HTH

-- =

Brian Raven =


==================== =====3D=
================
Atos Euronext Market Solutions Disclaimer
==================== =====3D=
================

The information contained in this e-mail is confidential and solely for the=
intended addressee(s). Unauthorised reproduction, disclosure, modification=
, and/or distribution of this email may be unlawful.
If you have received this email in error, please notify the sender immediat=
ely and delete it from your system. The views expressed in this message do =
not necessarily reflect those of Atos Euronext Market Solutions.

Atos Euronext Market Solutions Limited - Registered in England & Wales with=
registration no. 3962327. Registered office address at 25 Bank Street Lon=
don E14 5NQ United Kingdom. =

Atos Euronext Market Solutions SAS - Registered in France with registration=
no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 750=
09 Paris France.

L'information contenue dans cet e-mail est confidentielle et uniquement des=
tinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Tou=
te copie, publication ou diffusion de cet email est interdite. Si cet e-mai=
l vous parvient par erreur, nous vous prions de bien vouloir prevenir l'exp=
editeur immediatement et d'effacer le e-mail et annexes jointes de votre sy=
steme. Le contenu de ce message electronique ne represente pas necessaireme=
nt la position ou le point de vue d'Atos Euronext Market Solutions.
Atos Euronext Market Solutions Limited Soci=E9t=E9 de droit anglais, enregi=
str=E9e au Royaume Uni sous le num=E9ro 3962327, dont le si=E8ge social se =
situe 25 Bank Street E14 5NQ Londres Royaume Uni.

Atos Euronext Market Solutions SAS, soci=E9t=E9 par actions simplifi=E9e, e=
nregistr=E9 au registre dui commerce et des soci=E9t=E9s sous le num=E9ro 4=
25 100 294 RCS Paris et dont le si=E8ge social se situe 6/8 Boulevard Hauss=
mann 75009 Paris France.
==================== =====3D=
================

_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs