我試圖創建一個Perl客戶機接口到一個RPC服務器,並且類層次結構與服務器URL匹配。例如如何在perl6中存儲對父對象的引用(從perl5轉換)
# for url path: /account/login
# client code:
$client.account.login;
對於這項工作,將「孩子」對象(賬戶)需要存儲到它的父對象(客戶端)的參考。
這是我已經試過:
#!/usr/bin/env perl6
use v6;
class MyApp::Client::Account {
has $!client;
method login() {
# fake login
$!client.session_id = 'abc';
return 'ok';
}
}
class MyApp::Client {
has $.session_id is rw;
method account() {
state $empire = MyApp::Client::Account.new(:client(self));
return $empire;
}
}
use Test;
plan(2);
my $client = MyApp::Client.new;
my $response = $client.account.login;
is($response, 'ok', 'login successful');
ok($client.session_id, 'client has session_id');
運行本提供了以下錯誤信息:
1..2
Method 'session_id' not found for invocant of class 'Any'
in method login at test.pl6 line 9
in block <unit> at test.pl6 line 29
# Looks like you planned 2 tests, but ran 0
我真的不知道任何perl6類/對象成語,但 - 我是甚至將關於正確方式的設計?
如果是這樣,爲什麼$!client
內的login()
方法未定義?
僅供參考,下面是我試圖從轉換的perl5(裸機)版本:
#!/usr/bin/env perl
package MyApp::Client::Account;
sub new {
my $class = shift;
return bless {@_}, $class;
}
sub login {
my $self = shift;
# fake login
$self->{client}->session_id('abc');
return 'ok';
}
package MyApp::Client;
sub new {
my $class = shift;
return bless {@_}, $class;
}
sub session_id {
my $self = shift;
if (@_) {
$self->{session_id} = shift;
}
return $self->{session_id};
}
sub account {
my $self = shift;
$self->{account} ||= MyApp::Client::Account->new(client => $self);
return $self->{account};
}
package main;
use Test::More tests => 2;
my $client = MyApp::Client->new;
my $response = $client->account->login;
is($response, 'ok', 'login successful');
ok($client->session_id, 'client has session_id');
這給預期輸出:
1..2
ok 1 - login successful
ok 2 - client has session_id