use Carp 'confess';
use Scalar::Util 'blessed', 'weaken';
+use Try::Tiny;
-our $VERSION = '1.01';
+our $VERSION = '1.11';
$VERSION = eval $VERSION;
our $AUTHORITY = 'cpan:STEVAN';
## Inline methods
sub _generate_accessor_method_inline {
- my $self = shift;
- my $attr = $self->associated_attribute;
- my $attr_name = $attr->name;
- my $meta_instance = $attr->associated_class->instance_metaclass;
-
- my ( $code, $e ) = $self->_eval_closure(
- {},
- 'sub {'
- . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
- . ' if scalar(@_) == 2; '
- . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
- . '}'
- );
- confess "Could not generate inline accessor because : $e" if $e;
+ my $self = shift;
+ my $attr = $self->associated_attribute;
+
+ my $code = try {
+ $self->_compile_code([
+ 'sub {',
+ $attr->inline_set('$_[0]', '$_[1]'),
+ 'if scalar(@_) == 2;',
+ $attr->inline_get('$_[0]') . ';',
+ '}',
+ ]);
+ }
+ catch {
+ confess "Could not generate inline accessor because : $_";
+ };
return $code;
}
sub _generate_reader_method_inline {
- my $self = shift;
- my $attr = $self->associated_attribute;
- my $attr_name = $attr->name;
- my $meta_instance = $attr->associated_class->instance_metaclass;
-
- my ( $code, $e ) = $self->_eval_closure(
- {},
- 'sub {'
- . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;'
- . $meta_instance->inline_get_slot_value('$_[0]', $attr_name)
- . '}'
- );
- confess "Could not generate inline reader because : $e" if $e;
+ my $self = shift;
+ my $attr = $self->associated_attribute;
+
+ my $code = try {
+ $self->_compile_code([
+ 'sub {',
+ 'confess "Cannot assign a value to a read-only accessor"',
+ 'if @_ > 1;',
+ $attr->inline_get('$_[0]') . ';',
+ '}',
+ ]);
+ }
+ catch {
+ confess "Could not generate inline reader because : $_";
+ };
return $code;
}
sub _generate_writer_method_inline {
- my $self = shift;
- my $attr = $self->associated_attribute;
- my $attr_name = $attr->name;
- my $meta_instance = $attr->associated_class->instance_metaclass;
-
- my ( $code, $e ) = $self->_eval_closure(
- {},
- 'sub {'
- . $meta_instance->inline_set_slot_value('$_[0]', $attr_name, '$_[1]')
- . '}'
- );
- confess "Could not generate inline writer because : $e" if $e;
+ my $self = shift;
+ my $attr = $self->associated_attribute;
+
+ my $code = try {
+ $self->_compile_code([
+ 'sub {',
+ $attr->inline_set('$_[0]', '$_[1]') . ';',
+ '}',
+ ]);
+ }
+ catch {
+ confess "Could not generate inline writer because : $_";
+ };
return $code;
}
sub _generate_predicate_method_inline {
- my $self = shift;
- my $attr = $self->associated_attribute;
- my $attr_name = $attr->name;
- my $meta_instance = $attr->associated_class->instance_metaclass;
-
- my ( $code, $e ) = $self->_eval_closure(
- {},
- 'sub {'
- . $meta_instance->inline_is_slot_initialized('$_[0]', $attr_name)
- . '}'
- );
- confess "Could not generate inline predicate because : $e" if $e;
+ my $self = shift;
+ my $attr = $self->associated_attribute;
+
+ my $code = try {
+ $self->_compile_code([
+ 'sub {',
+ $attr->inline_has('$_[0]') . ';',
+ '}',
+ ]);
+ }
+ catch {
+ confess "Could not generate inline predicate because : $_";
+ };
return $code;
}
sub _generate_clearer_method_inline {
- my $self = shift;
- my $attr = $self->associated_attribute;
- my $attr_name = $attr->name;
- my $meta_instance = $attr->associated_class->instance_metaclass;
-
- my ( $code, $e ) = $self->_eval_closure(
- {},
- 'sub {'
- . $meta_instance->inline_deinitialize_slot('$_[0]', $attr_name)
- . '}'
- );
- confess "Could not generate inline clearer because : $e" if $e;
+ my $self = shift;
+ my $attr = $self->associated_attribute;
+
+ my $code = try {
+ $self->_compile_code([
+ 'sub {',
+ $attr->inline_clear('$_[0]') . ';',
+ '}',
+ ]);
+ }
+ catch {
+ confess "Could not generate inline clearer because : $_";
+ };
return $code;
}
=head1 DESCRIPTION
-This is a subclass of <Class::MOP::Method> which is used by
+This is a subclass of C<Class::MOP::Method> which is used by
C<Class::MOP::Attribute> to generate accessor code. It handles
generation of readers, writers, predicates and clearers. For each type
of method, it can either create a subroutine reference, or actually