如何在perl6中存储父对象的引用(从perl5转换)

时间:2016-01-22 10:13:27

标签: reference parent-child perl6

我正在尝试为RPC服务器创建一个Perl 6客户端接口,其类层次结构与服务器URL匹配。 e.g。

# for url path: /account/login
# client code:
$client.account.login;

为此,'child'对象(account)需要存储对其父对象(client)的引用。
这就是我尝试过的:

#!/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

1 个答案:

答案 0 :(得分:5)

因此Perl 6 OO与我使用的其他实现有几点不同。一个是它将为您自动填充成员变量的绝佳方式。但是,这仅在使用公共访问者定义时才有效。

class G {
   has $!g;
   has $.e;
   method emit { say (defined $!g) ?? "$!g G thing" !! "nada G thing" }
}

这将导致以下行为:

> my $g = G.new( :g('unit-of-some-kind'), :e('electric') )
G.new(e => "electric")
> $g.emit
nada G thing
> $g.e
electric

因此,当您将self作为MyApp::Client::Account的引用传递时,它不会绑定到$!client变量,因为默认构造函数只会绑定到可公开访问的成员变量

您可以选择使其可访问,也可以将对象构建逻辑放在自己的手中。这就是我想象的代码,我需要在Perl 6中使用我自己的版本,但必须保持客户端私有:

class MyApp::Client::Account {
    has $!client;

    method new(:$client) {
        self.bless( :$client );
    }

    # binds $!client to $client automatically based on the signature
    submethod BUILD(:$!client) { }

    method login() {
        # fake login
        $!client.session_id = 'abc';

        return 'ok';
    }
}

class MyApp::Client {
    has $.session_id is rw;
    has $.account;

    # the default .new will call .bless for us, which will run this BUILD
    submethod BUILD {
        $!account = MyApp::Client::Account.new( :client(self) );
    }
}

可能需要一些时间来适应newBUILD的区别。一个关键的区别点是selfnew范围内不可用,但 BUILD的范围内可用(尽管不是 - 尚未完全构造的形式)。