次のモジュールVERY SIMPLE perl module TEST.pmがあります
package TEST;
sub new {
my ($class) = @_;
my $self = {};
bless $self, $class;
return $self;
}
sub test {
my ($self, $args) = @_;
return "Test";
}
sub test2 {
my ($self, $args) = @_;
push @{$self->{CODES}}, 1 ;
return;
}
1;
次に、このモジュールを testmoduletest.pl で使用します
#!/usr/bin/perl -w
use lib "blablabla";
use TEST;
my $test = TEST->new();
print "ref of test = ", ref($test), "\n";
my $t = $test->test();
print "t is now $t and ref of test is ",ref($test),"\n";
$t = $test->test2();
if ($t) {
print "t is now $t and ref of test is ",ref($test),"\n";
} else {
print "t is uninitialized and ref of test is ",ref($test),"\n";
print "code = $_\n" for @{$test->{CODES}};
}
結果は予想どおりです。
ref of test = TEST
t is now Test and ref of test is TEST
t is uninitialized and ref of test is TEST
code = 1
ただし、このサービス TESTService.cgi で SOAP 経由でこのモジュールを使用する場合
#!/usr/bin/perl -w
use SOAP::Transport::HTTP;
use lib "blablabla";
use TEST;
SOAP::Transport::HTTP::CGI
-> dispatch_to('TEST')
-> handle;
そしてこのクライアントで testtestclient.pl
#!/usr/bin/perl -w
use SOAP::Lite #+trace => ['all', '-objects'],
+autodispatch =>
uri => 'http://blablabla.com/TESTService',
proxy => 'http://blablalba.com/cgi-bin/TESTService.cgi';
my $test = TEST->new();
print "ref of test = ", ref($test), "\n";
my $t = $test->test();
print "t is now $t and ref of test is ",ref($test),"\n";
$t = $test->test2();
if ($t) {
print "t is now $t and ref of test is ",ref($test),"\n";
} else {
print "t is uninitialized and ref of test is ",ref($test),"\n";
print "code = $_\n" for @{$test->{CODES}};
}
結果は、 $test が参照を失っているようなものです:
ref of test = TEST
t is now Test and ref of test is TEST
t is uninitialized and ref of test is REF
Not a HASH reference at testtestclient.pl line 16.
手伝ってくれてありがとう。