我想用一個更具體的子類來繼承和擴展一個基類,該子類從訪問器中移除必需的屬性並指定一個懶惰構建的默認值。但是,這樣做時,派生類不再圍繞調用訪問器的周圍子例程。修改繼承的訪問器並保留修飾符
我在做什麼錯在我的定義?
編輯:我應該說,我可以簡單地繼承訪問器而不修改它,並且around修飾符仍然有效,我知道我可以做一些事情,比如設置訪問器來獲取getter,然後定義一個getter方法訪問者的名稱(即sub attr { my $self = shift; my $value = $self->_get_attr; return "The value of attr is '$value'"; }
)。我只是很驚訝周圍的修改器被輕易地拋棄。
use strict;
use warnings;
use 5.010;
package My::Base;
use Moose;
has 'attr' => (is => 'ro', isa => 'Str', required => 1);
around 'attr' => sub {
my $orig = shift;
my $self = shift;
my $response = $self->$orig(@_);
return "The value of attr is '$response'"
};
package My::Derived;
use Moose;
extends 'My::Base';
has '+attr' => (required => 0, lazy_build => 1);
sub _build_attr {
return "default value";
}
package main;
my $base = My::Base->new(attr => 'constructor value');
say $base->attr; # "The value of attr is 'constructor value'"
my $derived = My::Derived->new();
say $derived->attr; # "default value"