Subject: | Please avoid calling ref() on attribute values! |
I've been noodling on MooseX::Thunk which adds Data::Thunk support to
Moose. One small thing that tripped me up is that the
initialize_instance_slot method of Moose::Meta::Attribute includes the
following code:
if (ref $val && $self->is_weak_ref) {
$self->_weaken_value($instance);
}
Calling "ref" on the value causes Data::Thunk to think about the value,
and thus avoids the desired property of laziness. Simply swapping the
order of the two conditions in the conjunction fixes it:
if ($self->is_weak_ref && ref $val) {
$self->_weaken_value($instance);
}
Right now I'm overriding the whole initialize_instance_slot method in
my attribute trait, but that seems like overkill. So it would be nice
if this could be changed in a future Moose.