From: Dave Rolsky Date: Fri, 16 Sep 2011 14:44:33 +0000 (-0500) Subject: Reuse an attribute's old insertion order if it has one X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e52716e07f057808ee85fd5b87b002e93e89e61c;p=gitmo%2FMoose.git Reuse an attribute's old insertion order if it has one --- diff --git a/lib/Class/MOP/Mixin/HasAttributes.pm b/lib/Class/MOP/Mixin/HasAttributes.pm index 508abf7..0c7be08 100644 --- a/lib/Class/MOP/Mixin/HasAttributes.pm +++ b/lib/Class/MOP/Mixin/HasAttributes.pm @@ -22,10 +22,17 @@ sub add_attribute { my $attr_name = $attribute->name; - $self->remove_attribute($attr_name) - if $self->has_attribute($attr_name); + my $old_order; - my $order = ( scalar keys %{ $self->_attribute_map } ); + if ($self->has_attribute($attr_name)) { + my $old_attr = $self->remove_attribute($attr_name); + $old_order = $old_attr->insertion_order; + } + + my $order + = defined $old_order + ? $old_order + : ( scalar keys %{ $self->_attribute_map } ); $attribute->_set_insertion_order($order); $self->_attribute_map->{$attr_name} = $attribute;