Perl Web service 服务器端

前提:

 安装了apache /mod_perl/perl (我这里的版本分别为 2.0/2.0/5.8)

 

之后就要配置httpd.conf,分配一个单独的目录给web service。用于存perl 模块以及代码

 

PerlRequire /绝对路径(或者相对)/startup.pl  ## 参考http://www.fayland.org/journal/mod_perl_configuring.html
<Location /world>
SetHandler perl-script
PerlHandler CLASS::World
</Location>

 

其中startup.pl 代码如下:

 

# Always a good thing to put at the top of every mod_perl script. This will # save many headaches as you work. use strict; use ModPerl::Util; # Tell mod_perl where to find Hello.pm module use lib qw(/xxx/xxx/xx); ## 这里重要,因为后台的web service的pm要找到这里。 # The name of the module to load using mod_perl use CLASS::World; # This script must return TRUE, and this line accomplishes that, and conveniently # also checks to ensure that we are actually running mod_perl with our version # of Apache. $ENV{MOD_PERL} or die "not running under mod_perl!";

 

之后在/xxx/xxx/xx 目录下建立 CLASS/World.pm  perl模块文件,其内容如下:

package CLASS::World; use SOAP::Transport::HTTP; my $server = SOAP::Transport::HTTP::Apache -> dispatch_to('WorldFunctions'); sub handler { $server->handler(@_); } package WorldFunctions; sub new { bless {}, shift; } sub Hello { my ($s, $name) = @_; return 'Hello, ' . $name . "/n"; } sub GoodBye { my ($s, $name) = @_; return 'Goodbye, ' . $name . "/n"; } 1;

 

 

到此,服务器端就算简单的完成了。

写一个简单的client,调用server:

 

use SOAP::Lite; my $soap_client = SOAP::Lite -> uri('http://your_id_or_domain/WorldFunctions') -> proxy('http://your_id_or_domain/world'); print $soap_client->Hello('Joe')->result; print $soap_client->GoodBye('Joe')->result;

 

 

结果:

Hello, Joe
Goodbye, Joe

 

 

当然我这个还不完美。起码页面上还看不到wsdl说明。需要进一步研究:)

 

 

                                                   

你可能感兴趣的:(Web,服务器,service,perl,Class,SOAP)