From: Guillermo Roditi Date: Mon, 29 Oct 2007 19:44:29 +0000 (+0000) Subject: just some little optimizations to builder stuff X-Git-Tag: 0_44~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8fe581e5ccc954825e6089ca2adb751051e495e7;p=gitmo%2FClass-MOP.git just some little optimizations to builder stuff --- diff --git a/lib/Class/MOP.pm b/lib/Class/MOP.pm index 14afdef..1a6a603 100644 --- a/lib/Class/MOP.pm +++ b/lib/Class/MOP.pm @@ -405,12 +405,12 @@ Class::MOP::Attribute->meta->add_method('new' => sub { if ref $options{builder} || !(defined $options{builder}); confess("Setting both default and builder is not allowed.") if exists $options{default}; + } else { + (Class::MOP::Attribute::is_default_a_coderef(\%options)) + || confess("References are not allowed as default values, you must ". + "wrap then in a CODE reference (ex: sub { [] } and not [])") + if exists $options{default} && ref $options{default}; } - (Class::MOP::Attribute::is_default_a_coderef(\%options)) - || confess("References are not allowed as default values, you must ". - "wrap then in a CODE reference (ex: sub { [] } and not [])") - if exists $options{default} && ref $options{default}; - # return the new object $class->meta->new_object(name => $name, %options); }); diff --git a/lib/Class/MOP/Attribute.pm b/lib/Class/MOP/Attribute.pm index c2cfdf1..a91473f 100644 --- a/lib/Class/MOP/Attribute.pm +++ b/lib/Class/MOP/Attribute.pm @@ -43,12 +43,12 @@ sub new { if ref $options{builder} || !(defined $options{builder}); confess("Setting both default and builder is not allowed.") if exists $options{default}; + } else { + (is_default_a_coderef(\%options)) + || confess("References are not allowed as default values, you must ". + "wrap then in a CODE reference (ex: sub { [] } and not [])") + if exists $options{default} && ref $options{default}; } - (is_default_a_coderef(\%options)) - || confess("References are not allowed as default values, you must ". - "wrap then in a CODE reference (ex: sub { [] } and not [])") - if exists $options{default} && ref $options{default}; - bless { '$!name' => $name, '$!accessor' => $options{accessor}, @@ -91,11 +91,12 @@ sub initialize_instance_slot { # attribute's default value (if it has one) if (!defined $val && defined $self->{'$!default'}) { $val = $self->default($instance); - } elsif (!defined $val && defined $self->{'$!builder'}) { - my $builder = $self->{'$!builder'}; - confess(blessed($instance)." does not support builder method '$builder' for attribute '" . $self->name . "'") - unless $instance->can($builder); - $val = $instance->$builder; + } elsif (!defined $val && defined( my $builder = $self->{'$!builder'})) { + if($builder = $instance->can($builder) ){ + $val = $instance->$builder; + } else { + confess(blessed($instance)." does not support builder method '$builder' for attribute '" . $self->name . "'"); + } } $meta_instance->set_slot_value($instance, $self->name, $val); }