if(defined $instance){ # Application::ToInstance
# rebless instance
bless $instance, $consumer->name;
- $consumer->_initialize_object($instance, $instance);
+ $consumer->_initialize_object($instance, $instance, 1);
}
return;
push @triggers_queue, [ $attribute->trigger, $object->{$slot} ];
}
}
- elsif(!$is_cloning) { # no init arg, noop while cloning
+ else { # no init arg
if ($attribute->has_default || $attribute->has_builder) {
- if (!$attribute->is_lazy) {
+ if (!$attribute->is_lazy && !exists $object->{$slot}) {
my $default = $attribute->default;
my $builder = $attribute->builder;
my $value = $builder ? $object->$builder()
if ref($object->{$slot}) && $attribute->is_weak_ref;
}
}
- elsif($attribute->is_required) {
+ elsif(!$is_cloning && $attribute->is_required) {
$self->throw_error("Attribute (".$attribute->name.") is required");
}
}
--- /dev/null
+#!perl
+# This test is contributed by Sanko Robinson.
+# https://rt.cpan.org/Public/Bug/Display.html?id=56837
+# "Role application to instance with init_arg'd attributes"
+use strict;
+use Test::More tests => 2;
+
+{
+ package Admin;
+ use Mouse::Role;
+ sub shutdown {1}
+}
+{
+ package User;
+ use Mouse;
+ has 'name' =>
+ (isa => 'Str', is => 'ro', init_arg => 'Name', required => 1);
+}
+
+package main;
+my $tim = User->new(Name => 'Tim');
+
+Admin->meta->apply($tim);
+
+ok($tim->can('shutdown'),
+ 'The role was successfully composed at the object level');
+is($tim->name, 'Tim',
+ '... attribute with init_arg was re-initialized correctly');
}
used++;
}
- else if(!is_cloning){ /* no init arg, noop while cloning */
+ else { /* no init arg */
if(flags & (MOUSEf_ATTR_HAS_DEFAULT | MOUSEf_ATTR_HAS_BUILDER)){
- if(!(flags & MOUSEf_ATTR_IS_LAZY)){
+ /* skip if the object has the slot (it occurs on cloning/reblessing) */
+ if(!(flags & MOUSEf_ATTR_IS_LAZY) && !has_slot(object, slot)){
mouse_xa_set_default(aTHX_ xa, object);
}
}
- else if(flags & MOUSEf_ATTR_IS_REQUIRED) {
+ /* don't check while cloning (or reblesseing) */
+ else if(!is_cloning && flags & MOUSEf_ATTR_IS_REQUIRED) {
mouse_throw_error(attr, NULL, "Attribute (%"SVf") is required", slot);
}
}