From: Guillermo Roditi Date: Mon, 29 Oct 2007 19:49:03 +0000 (+0000) Subject: some optimizations to builder and lazy build along with better errors on inlined... X-Git-Tag: 0_27~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a0748c37561e0112f395a943b0b0bae1058270d8;p=gitmo%2FMoose.git some optimizations to builder and lazy build along with better errors on inlined lazy build accessor --- diff --git a/lib/Moose/Meta/Attribute.pm b/lib/Moose/Meta/Attribute.pm index 77cd5b6..1c64d20 100644 --- a/lib/Moose/Meta/Attribute.pm +++ b/lib/Moose/Meta/Attribute.pm @@ -224,9 +224,11 @@ sub initialize_instance_slot { $val = $self->default($instance); } elsif (!defined $val && $self->has_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; + if($builder = $instance->can($builder)){ + $val = $instance->$builder; + } else { + confess(blessed($instance)." does not support builder method '$builder' for attribute '" . $self->name . "'"); + } } if (defined $val || $self->has_default) { @@ -313,9 +315,11 @@ sub get_value { } if ( $self->has_builder ){ my $builder = $self->builder; - confess(blessed($instance)." does not support builder method '$builder' for attribute '" . $self->name . "'") - unless $instance->can($builder); - $self->set_value($instance, $instance->$builder); + if($builder = $instance->can($builder)){ + $self->set_value($instance, $instance->$builder); + } else { + confess(blessed($instance)." does not support builder method '$builder' for attribute '" . $self->name . "'"); + } } else { $self->set_value($instance, undef); } diff --git a/lib/Moose/Meta/Method/Accessor.pm b/lib/Moose/Meta/Method/Accessor.pm index d5e67e3..504ae36 100644 --- a/lib/Moose/Meta/Method/Accessor.pm +++ b/lib/Moose/Meta/Method/Accessor.pm @@ -161,9 +161,11 @@ sub _inline_check_lazy { ' $default = $attr->default(' . $inv . ') if $attr->has_default;' . ' if ( $attr->has_builder ) { '. ' my $builder = $self->builder;'. - ' confess(blessed('.$inv.')." does not support builder method \'$builder\' for attribute \'" . $attr->name . "\'")'. - ' unless '.$inv.'->can($builder); '. - ' $default = '.$inv.'->$builder;'. + ' if($builder = '.$inv.'->can($builder)){ '. + ' $default = '.$inv.'->$builder; '. + ' } else { '. + ' confess(blessed('.$inv.')." does not support builder method \'$builder\' for attribute \'" . $attr->name . "\'");'. + ' }'. ' }'. ($attr->should_coerce ? '$default = $attr->type_constraint->coerce($default);' @@ -182,8 +184,14 @@ sub _inline_check_lazy { return 'unless (exists ' . $slot_access . ') {' . ' if ($attr->has_default) { ' . $slot_access . ' = $attr->default(' . $inv . '); }' . - ' elsif ($attr->has_builder) { my $builder = $attr->builder; ' . $slot_access . ' = ' . $inv . '->$builder; }' . - ' else { ' .$slot_access . ' = undef; } '. + ' elsif ($attr->has_builder) { '. + ' my $builder = $attr->builder; ' . + ' if($builder = '.$inv.'->can($builder)){ '. + ' ' . $slot_access . ' = ' . $inv . '->$builder; '. + ' } else { '. + ' confess(blessed('.$inv.')." does not support builder method \'$builder\' for attribute \'" . $attr->name . "\'");'. + ' }'. + ' } else { ' .$slot_access . ' = undef; } '. '}'; }