哈希表是Perl对象的典型初始值设定项.现在你的输入是不可靠的,因为你不知道对于任何给定的键是否有定义的值,也不知道键是否存在.现在你想要将这些不可靠的输入提供给Moose对象,虽然缺少键是完全没问题的,但你确实希望摆脱未定义的值,这样你就不会得到一个充满未定义属性的对象.
在实例化对象并过滤掉未定义的值时,您当然可以非常小心.但是,假设你想在构造函数中安装该过滤器,因为它在一个地方.您希望构造函数忽略未定义的值,但不要在遇到它们时死掉.
对于访问器方法,您可以在周围使用以防止将该属性设置为undef.但是那些method modifiers不是为构造函数调用的,仅用于访问器.在Moose中是否有类似的设施来实现对c’tor的相同效果,即阻止任何undef属性被接受?
请注意,如果属性为undef,则Moose Any类型将在对象中创建哈希键.我不希望这样,因为我希望%$self不包含任何undef值.
这是我做的一些测试:
package Gurke; use Moose; use Data::Dumper; has color => is => 'rw',isa => 'Str',default => 'green'; has length => is => 'rw',isa => 'Num'; has appeal => is => 'rw',isa => 'Any'; around color => sub { # print STDERR Dumper \@_; my $orig = shift; my $self = shift; return $self->$orig unless @_; return unless defined $_[0]; return $self->$orig( @_ ); }; package main; use Test::More; use Test::Exception; my $gu = Gurke->new; isa_ok $gu,'Gurke'; diag explain $gu; ok ! exists $gu->{length},'attribute not passed,so not set'; diag q(attempt to set color to undef - we don't want it to succeed); ok ! defined $gu->color( undef ),'returns undef'; is $gu->color,'green','value unchanged'; diag q(passing undef in the constructor will make it die); dies_ok { Gurke->new( color => undef ) } 'around does not work for the constructor!'; lives_ok { $gu = Gurke->new( appeal => undef ) } 'anything goes'; diag explain $gu; diag q(... but creates the undef hash key,which is not what I want); done_testing;