use strict;
use warnings;
-use Carp 'confess';
-
-our $VERSION = '0.57';
+our $VERSION = '0.62_01';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
use base 'Moose::Meta::Method',
'Class::MOP::Method::Accessor';
-## Inline method generators
+sub _error_thrower {
+ my $self = shift;
+ ( ref $self && $self->associated_attribute ) || $self->SUPER::_error_thrower();
+}
sub _eval_code {
my ( $self, $code ) = @_;
# NOTE:
# set up the environment
+ my $environment = q{
my $attr = $self->associated_attribute;
my $attr_name = $attr->name;
+ my $meta = $self;
my $type_constraint_obj = $attr->type_constraint;
my $type_constraint_name = $type_constraint_obj && $type_constraint_obj->name;
my $type_constraint = $type_constraint_obj
? $type_constraint_obj->_compiled_type_constraint
: undef;
+};
#warn "code for $attr_name =>\n" . $code . "\n";
- my $sub = eval $code;
- confess "Could not create writer for '$attr_name' because $@ \n code: $code" if $@;
+ my $sub = $self->_eval_closure($environment, $code);
+ $self->throw_error("Could not create writer for '${\$self->associated_attribute->name}' because $@ \n code: $code", error => $@, data => $code ) if $@;
return $sub;
}
$self->_eval_code('sub {'
. $self->_inline_pre_body(@_)
- . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
+ . $self->_inline_throw_error('"Cannot assign a value to a read-only accessor"', 'data => \@_') . ' if @_ > 1;'
. $self->_inline_check_lazy($inv)
. $self->_inline_post_body(@_)
. 'return ' . $self->_inline_auto_deref( $slot_access ) . ';'
my $type_constraint_name = $attr->type_constraint->name;
- # FIXME
- # This sprintf is insanely annoying, we should
- # fix it someday - SL
- return sprintf <<'EOF', $value, $attr_name, $value, $value,
-$type_constraint->(%s)
- || confess "Attribute (%s) does not pass the type constraint because: "
- . $type_constraint_obj->get_message(%s);
-EOF
+ qq{\$type_constraint->($value) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) does not pass the type constraint because: " . \$type_constraint_obj->get_message($value)}, "data => $value") . ";";
}
sub _inline_check_coercion {
}
sub _inline_check_required {
- my $attr = (shift)->associated_attribute;
+ my $self = shift;
+ my $attr = $self->associated_attribute;
my $attr_name = $attr->name;
return '' unless $attr->is_required;
- return qq{(\@_ >= 2) || confess "Attribute ($attr_name) is required, so cannot be set to undef";} # defined $_[1] is not good enough
+ return qq{(\@_ >= 2) || } . $self->_inline_throw_error(qq{"Attribute ($attr_name) is required, so cannot be set to undef"}) . ';' # defined $_[1] is not good enough
}
sub _inline_check_lazy {
$code .= ' my $default;'."\n".
' if(my $builder = '.$instance.'->can($attr->builder)){ '."\n".
' $default = '.$instance.'->$builder; '. "\n } else {\n" .
- ' confess((Scalar::Util::blessed('.$instance.') || '.$instance.')." does not support builder method '.
- '\'".$attr->builder."\' for attribute \'" . $attr->name . "\'");'. "\n }";
+ ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name') .
+ ';'. "\n }";
}
$code .= ' $default = $type_constraint_obj->coerce($default);'."\n" if $attr->should_coerce;
$code .= ' ($type_constraint->($default))' .
- ' || confess "Attribute (" . $attr_name . ") does not pass the type constraint ("' .
- ' . $type_constraint_name . ") with " . (defined($default) ? overload::StrVal($default) : "undef");'
+ ' || ' . $self->_inline_throw_error('"Attribute (" . $attr_name . ") does not pass the type constraint ("' .
+ ' . $type_constraint_name . ") with " . (defined($default) ? overload::StrVal($default) : "undef")' ) . ';'
. "\n";
$code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, '$default') . "\n";
}
elsif ($attr->has_builder) {
$code .= ' if (my $builder = '.$instance.'->can($attr->builder)) { ' . "\n"
. ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, ($instance . '->$builder'))
- . "\n } else {\n" .
- ' confess((Scalar::Util::blessed('.$instance.') || '.$instance.')." does not support builder method '.
- '\'".$attr->builder."\' for attribute \'" . $attr->name . "\'");'. "\n }";
+ . "\n } else {\n"
+ . ' ' . $self->_inline_throw_error(q{sprintf "%s does not support builder method '%s' for attribute '%s'", ref(} . $instance . ') || '.$instance.', $attr->builder, $attr->name')
+ . ';'. "\n }";
}
else {
$code .= ' ' . $self->_inline_init_slot($attr, $instance, $slot_access, 'undef') . "\n";
my ($self, $instance, $value) = @_;
my $attr = $self->associated_attribute;
return '' unless $attr->has_trigger;
- return sprintf('$attr->trigger->(%s, %s, $attr);', $instance, $value);
+ return sprintf('$attr->trigger->(%s, %s);', $instance, $value);
}
sub _inline_get {
$sigil = '%';
}
else {
- confess "Can not auto de-reference the type constraint '" . $type_constraint->name . "'";
+ $self->throw_error("Can not auto de-reference the type constraint '" . $type_constraint->name . "'", type_constraint => $type_constraint );
}
"(wantarray() ? $sigil\{ ( $ref_value ) || return } : ( $ref_value ) )";