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

fir*_*ist 3 reference parent-child perl6

我正在尝试创建一个到RPC服务器的Perl 6客户端接口,类层次结构与服务器URL匹配.例如

# for url path: /account/login
# client code:
$client.account.login;
Run Code Online (Sandbox Code Playgroud)

为此,"子"对象(帐户)需要存储对其父对象(客户端)的引用.
这就是我尝试过的:

#!/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' );
Run Code Online (Sandbox Code Playgroud)

运行此命令会显示以下错误消息:

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
Run Code Online (Sandbox Code Playgroud)

我还不知道任何perl6类/对象的习惯用语 - 我是否以正确的方式进行设计?
如果是这样,为什么$!clientlogin()方法中未定义?

作为参考,这里是我试图转换的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' );
Run Code Online (Sandbox Code Playgroud)

这给出了预期的输出:

1..2
ok 1 - login successful
ok 2 - client has session_id
Run Code Online (Sandbox Code Playgroud)

ab5*_*act 5

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

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

这将导致以下行为:

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

因此,当您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) );
    }
}
Run Code Online (Sandbox Code Playgroud)

它可能需要一些时间来适应newBUILD区别.一个关键的区别的一点是,self是不是在范围内可用的new,但它在范围内可用的BUILD(尽管在还未完全构建形式).