0

在以下代码中,我尝试为参数化角色中的属性定义触发器。

#!/usr/bin/env perl

package WordSizeRoleParameterized;
use MooseX::Role::Parameterized;

# a parameterized role with a trigger for an attribute
# does not work
role {   
    has 'word' => ( is => 'rw', trigger => \&_trigger_word_size,);

    has 'word_size' => ( is => 'ro', writer => '_set_word_size', );

    method '_trigger_word_size' => sub {
        my $self = shift;
        my $size;

        if ( $self->word ) { $size = split //, $self->word; }
        else               { $size = 0; }

        print "WordsizeRoleParameterized::_trigger_word_size() called : size = $size \n";
        $self->_set_word_size($size);
    };
};

1;

package WordSizeRole;
use Moose::Role;

# a plain role with a trigger for an attribute
# works as expected 
    has 'word' => ( is => 'rw', trigger => \&_trigger_word_size,);

    has 'word_size' => ( is => 'ro', writer => '_set_word_size', );

    sub _trigger_word_size {
        my $self = shift;
        my $size;

        if ( $self->word ) { $size = split //, $self->word;}
        else               { $size = 0;}
        $self->_set_word_size($size);
    }

1;

package ClassWordSizeParameterized;
use Moose;
with 'WordSizeRoleParameterized';
1;

package ClassWordSize;
use Moose;
with 'WordSizeRole';
1;

#------------
package main;
#------------

use strict;
use warnings;

# no error
my $wordy = ClassWordSize->new({'word' => 'goodie'});
print "word : '", $wordy->word, "', size: ", $wordy->word_size, "\n";

# but using the paramaeterized role ...
my $wordy_parameterized = ClassWordSizeParameterized->new();

# will cause an error if calling $wordy_parameterized->word('Superdubadubaduuuh')); 
#
# Undefined subroutine &WordSizeRoleParameterized::_trigger_word_size called at accessor
# ClassWordSizeParameterized::word (defined at roleTrigger.pl line 8) line 7.
$wordy_parameterized->word('Superdubadubaduuuh');
print "word : '", $wordy_parameterized->word, "', size: ", $wordy_parameterized->word_size, "\n";

调用 $wordy_parameterized->word('Superdubadubaduuuh'),它应该触发触发例程导致错误:Undefined subroutine &WordSizeRoleParameterized::_trigger_word_size called

有趣的是,WordSizeRoleParameterized::_trigger_word_size 方法存在(您可以调用 $wordy_parameterized->_trigger_word_size();),但它不会作为属性的触发例程触发。MooseX::Role::Parameterized 的文档没有说明任何内容。

MooseX::Role::Parameterized 的某些实现细节是否不允许或完全不可能?

4

1 回答 1

1

它或多或少是MooseX::Role::Parameterized. 使用method关键字时,它不会在WordSizeRoleParameterized包中创建方法。同样,has您在块中使用的其他关键字role {}不会影响使用MooseX::Role::Parameterized. 此类声明仅影响role {}块在幕后生成的匿名角色,然后影响使用该匿名角色的类(或其他角色)。

这里的问题\&_trigger_word_size是绑定到错误的包(WordSizeRoleParameterized当它应该绑定到匿名角色的包时)。更改\&_trigger_word_sizesub { shift->_trigger_word_size(@_) }可以解决问题,因为它使用正常的方法解析,因此该方法将在正确的包 ( ClassWordSizeParameterized) 中找到。

word : 'goodie', size: 6
WordsizeRoleParameterized::_trigger_word_size() called : size = 18 
word : 'Superdubadubaduuuh', size: 18

您看到$wordy_parameterized->_trigger_word_size的是因为该方法是通过匿名角色复制到类中的,而不是通过WordSizeRoleParameterized.

于 2012-06-08T19:22:37.613 回答