gpt4 book ai didi

perl - 将 MX::Declare 方法定义为属性触发器

转载 作者:行者123 更新时间:2023-12-04 06:08:09 24 4
gpt4 key购买 nike

以下代码按我的预期工作。当它所依赖的 foo 属性发生变化时,缓存的惰性属性会被清除并重建。

use MooseX::Declare;
use 5.010;

class Test {
has foo => ( isa => 'Str', is => 'rw', trigger => sub {my $self = shift; $self->clearer}, default => '' );

has lazy => ( isa => 'Str', is => 'ro', lazy => 1, clearer => 'clearer',
default => method { say 'building lazy'; return "foo is '".$self->foo."'"; },
);

method say ( ) {
say $self->lazy;
}
}

my $inst = Test->new( foo => 'baz' );
$inst->say;
$inst->say;
say $inst->foo();
$inst->foo('bar');
$inst->say;

输出:
building lazy
foo is 'baz'
foo is 'baz'
baz
building lazy
foo is 'bar'

但是,我如何将 MX::Declare 糖用于触发器子例程?定义 foo 为:
has foo => ( isa => 'Str', is => 'rw', trigger => method {$self->clearer}, default => '' );

导致类在编译时死亡(如下)。我的匿名方法声明有什么问题吗?

Trigger must be a CODE ref on attribute (foo) at C:/Strawberry/perl/site/lib/Moose/Meta/Attribute.pm line 423 Moose::Meta::Attribute::_process_trigger_option('Moose::Meta::Attribute', 'foo', 'HASH(0x2a5d14c)') called at C: /Strawberry/perl/site/lib/Moose/Meta/Attribute.pm line 299 Moose::Meta::Attribute::_process_options('Moose::Meta::Attribute', 'foo', 'HASH(0x2a5d14c)') called at C:/Strawb erry/perl/site/lib/Moose/Meta/Attribute.pm line 88 Moose::Meta::Attribute::new('Moose::Meta::Attribute', 'foo', 'trigger', 'MooseX::Method::Signatures::Meta::Metho d=HASH(0x39a421c)', 'isa', 'Str', 'definition_context', 'HASH(0x3452184)', 'default', '', 'is', 'rw') called at C:/Straw berry/perl/site/lib/Moose/Meta/Attribute.pm line 114 Moose::Meta::Attribute::interpolate_class_and_new('Moose::Meta::Attribute', 'foo', 'trigger', 'MooseX::Method::S ignatures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str', 'default', '', 'definition_context', 'HASH(0x3452184)', 'is', 'r w') called at C:/Strawberry/perl/site/lib/Moose/Meta/Class.pm line 704 Moose::Meta::Class::_process_new_attribute('Moose::Meta::Class=HASH(0x38c79d4)', 'foo', 'trigger', 'MooseX::Meth od::Signatures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str', 'default', '', 'definition_context', 'HASH(0x3452184)', 'is ', 'rw') called at C:/Strawberry/perl/site/lib/Moose/Meta/Class.pm line 697 Moose::Meta::Class::_process_attribute('Moose::Meta::Class=HASH(0x38c79d4)', 'foo', 'trigger', 'MooseX::Method:: Signatures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str', 'default', '', 'definition_context', 'HASH(0x3452184)', 'is', ' rw') called at C:/Strawberry/perl/site/lib/Moose/Meta/Class.pm line 566 Moose::Meta::Class::add_attribute('Moose::Meta::Class=HASH(0x38c79d4)', 'foo', 'trigger', 'MooseX::Method::Signa tures::Meta::Method=HASH(0x39a421c)', 'isa', 'Str', 'default', '', 'definition_context', 'HASH(0x3452184)', 'is', 'rw') called at C:/Strawberry/perl/site/lib/Moose.pm line 77 Moose::has('Moose::Meta::Class=HASH(0x38c79d4)', 'foo', 'isa', 'Str', 'is', 'rw', 'trigger', 'MooseX::Method::Si gnatures::Meta::Method=HASH(0x39a421c)', 'default', '') called at C:/Strawberry/perl/site/lib/Moose/Exporter.pm line 356

    Moose::has('foo', 'isa', 'Str', 'is', 'rw', 'trigger', 'MooseX::Method::Signatures::Meta::Method=HASH(0x39a421c) ',

'default', '') called at mx_declare.pl line 5 main::ANON() called at C:/Strawberry/perl/site/lib/MooseX/Declare/Syntax/MooseSetup.pm line 81 MooseX::Declare::Syntax::MooseSetup::ANON('CODE(0x38c3a94)') called at mx_declare.pl line 13

最佳答案

method关键字返回 MooseX::Method::Signatures::Meta::Method 的实例类,它是 Moose::Meta::Method 的子类,它是 Class::MOP::Method 的子类.

Moose 允许 default 的方法对象,但不适用于 trigger ,它必须是一个常规的 coderef。

如果你真的想使用 method关键字那里,你可能会这样做:

trigger => method { $self->clearer }->body,

但是按照@cjm 的建议去做可能更容易(也更理智),只需使用常规的 coderef:
trigger => sub { shift->clearer },

关于perl - 将 MX::Declare 方法定义为属性触发器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8101073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com