-
-package # hide the package from PAUSE
- ArrayBasedStorage::Attribute;
-
-use strict;
-use warnings;
-
-use Carp 'confess';
-
-our $VERSION = '0.01';
-
-use base 'Class::MOP::Attribute';
-
-sub initialize_instance_slot {
- my ($self, $meta_instance, $instance, $params) = @_;
- my $init_arg = $self->{init_arg};
- # try to fetch the init arg from the %params ...
- my $val;
- $val = $params->{$init_arg} if exists $params->{$init_arg};
- # if nothing was in the %params, we can use the
- # attribute's default value (if it has one)
- if (!defined $val && defined $self->{default}) {
- $val = $self->default($instance);
- }
- $meta_instance->set_slot_value($instance, $self->name, $val);
-}
-
-sub generate_accessor_method {
- my $self = shift;
- my $meta_class = $self->associated_class;
- my $attr_name = $self->name;
- return sub {
- my $meta_instance = $meta_class->initialize(Scalar::Util::blessed($_[0]))->get_meta_instance;
- $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
- $meta_instance->get_slot_value($_[0], $attr_name);
- };
-}
-
-sub generate_reader_method {
- my $self = shift;
- my $meta_class = $self->associated_class;
- my $attr_name = $self->name;
- return sub {
- confess "Cannot assign a value to a read-only accessor" if @_ > 1;
- $meta_class->initialize(Scalar::Util::blessed($_[0]))
- ->get_meta_instance
- ->get_slot_value($_[0], $attr_name);
- };
-}
-
-sub generate_writer_method {
- my $self = shift;
- my $meta_class = $self->associated_class;
- my $attr_name = $self->name;
- return sub {
- $meta_class->initialize(Scalar::Util::blessed($_[0]))
- ->get_meta_instance
- ->set_slot_value($_[0], $attr_name, $_[1]);
- };
-}
-
-sub generate_predicate_method {
- my $self = shift;
- my $meta_class = $self->associated_class;
- my $attr_name = $self->name;
- return sub {
- defined $meta_class->initialize(Scalar::Util::blessed($_[0]))
- ->get_meta_instance
- ->get_slot_value($_[0], $attr_name) ? 1 : 0;
- };
-}
-
+
package # hide the package from PAUSE
ArrayBasedStorage::Instance;
package # hide the package from PAUSE
+ InsideOutClass::Attribute;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+
+use Carp 'confess';
+use Scalar::Util 'refaddr';
+
+use base 'Class::MOP::Attribute';
+
+sub initialize_instance_slot {
+ my ($self, $meta_instance, $instance, $params) = @_;
+ my $init_arg = $self->{init_arg};
+ # try to fetch the init arg from the %params ...
+ my $val;
+ $val = $params->{$init_arg} if exists $params->{$init_arg};
+ # if nothing was in the %params, we can use the
+ # attribute's default value (if it has one)
+ if (!defined $val && defined $self->{default}) {
+ $val = $self->default($instance);
+ }
+ $self->associated_class
+ ->get_meta_instance
+ ->set_slot_value($instance, $self->name, $val);
+}
+
+## Method generation helpers
+
+sub generate_accessor_method {
+ my $self = shift;
+ my $meta_class = $self->associated_class;
+ my $attr_name = $self->name;
+ return sub {
+ my $meta_instance = $meta_class->get_meta_instance;
+ $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
+ $meta_instance->get_slot_value($_[0], $attr_name);
+ };
+}
+
+sub generate_reader_method {
+ my $self = shift;
+ my $meta_class = $self->associated_class;
+ my $attr_name = $self->name;
+ return sub {
+ confess "Cannot assign a value to a read-only accessor" if @_ > 1;
+ $meta_class->get_meta_instance
+ ->get_slot_value($_[0], $attr_name);
+ };
+}
+
+sub generate_writer_method {
+ my $self = shift;
+ my $meta_class = $self->associated_class;
+ my $attr_name = $self->name;
+ return sub {
+ $meta_class->get_meta_instance
+ ->set_slot_value($_[0], $attr_name, $_[1]);
+ };
+}
+
+sub generate_predicate_method {
+ my $self = shift;
+ my $meta_class = $self->associated_class;
+ my $attr_name = $self->name;
+ return sub {
+ defined $meta_class->get_meta_instance
+ ->get_slot_value($_[0], $attr_name) ? 1 : 0;
+ };
+}
+
+package # hide the package from PAUSE
InsideOutClass::Instance;
use strict;
return exists $self->{meta}->get_package_variable('%' . $slot_name)->{refaddr $instance} ? 1 : 0;
}
-sub inline_slot_access {
- my ($self, $instance, $slot_name) = @_;
- $slot_name =~ s/\'//g;
- ('$' . $self->{meta}->name . '::' . $slot_name . '{Scalar::Util::refaddr(' . $instance . ')}');
-}
-
1;
__END__
package Foo;
use metaclass (
- ':instance_metaclass' => 'InsideOutClass::Instance'
+ ':attribute_metaclass' => 'InsideOutClass::Attribute',
+ ':instance_metaclass' => 'InsideOutClass::Instance'
);
__PACKAGE__->meta->add_attribute('foo' => (
if (!defined $val && defined $self->{default}) {
$val = $self->default($instance);
}
- $self->associated_class
- ->get_meta_instance
- ->set_slot_value($instance, $self->name, $val);
+ $meta_instance->set_slot_value($instance, $self->name, $val);
}
# NOTE:
## Method generation helpers
sub generate_accessor_method {
- my $self = shift;
- #my $meta_class = $self->associated_class;
- my $meta_instance = $self->associated_class->get_meta_instance;
+ my $self = shift;
my $attr_name = $self->name;
- #return sub {
- # my $meta_instance = $meta_class->get_meta_instance;
- # $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
- # $meta_instance->get_slot_value($_[0], $attr_name);
- #};
-
- my $code = "sub {\n"
- . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
- . " if scalar(\@_) == 2;\n"
- . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'", '$_[1]')
- . "\n}";
- my $sub = eval $code;
- confess "Could not eval code:\n$code\nbecause: $@" if $@;
- return $sub;
+ return sub {
+ my $meta_instance = Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))->get_meta_instance;
+ $meta_instance->set_slot_value($_[0], $attr_name, $_[1]) if scalar(@_) == 2;
+ $meta_instance->get_slot_value($_[0], $attr_name);
+ };
}
sub generate_reader_method {
my $self = shift;
- #my $meta_class = $self->associated_class;
- my $meta_instance = $self->associated_class->get_meta_instance;
my $attr_name = $self->name;
- #return sub {
- # confess "Cannot assign a value to a read-only accessor" if @_ > 1;
- # $meta_class->get_meta_instance
- # ->get_slot_value($_[0], $attr_name);
- #};
-
- my $code = "sub {\n"
- . 'confess "Cannot assign a value to a read-only accessor" if @_ > 1;' . "\n"
- . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'", '$_[1]')
- . "\n}";
- my $sub = eval $code;
- confess "Could not eval code:\n$code\nbecause: $@" if $@;
- return $sub;
+ return sub {
+ confess "Cannot assign a value to a read-only accessor" if @_ > 1;
+ Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
+ ->get_meta_instance
+ ->get_slot_value($_[0], $attr_name);
+ };
}
sub generate_writer_method {
my $self = shift;
- #my $meta_class = $self->associated_class;
- my $meta_instance = $self->associated_class->get_meta_instance;
my $attr_name = $self->name;
- #return sub {
- # $meta_class->get_meta_instance
- # ->set_slot_value($_[0], $attr_name, $_[1]);
- #};
-
- my $code = "sub {\n"
- . $meta_instance->inline_set_slot_value('$_[0]', "'$attr_name'", '$_[1]')
- . "\n}";
- my $sub = eval $code;
- confess "Could not eval code:\n$code\nbecause: $@" if $@;
- return $sub;
+ return sub {
+ Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
+ ->get_meta_instance
+ ->set_slot_value($_[0], $attr_name, $_[1]);
+ };
}
sub generate_predicate_method {
my $self = shift;
- #my $meta_class = $self->associated_class;
- my $meta_instance = $self->associated_class->get_meta_instance;
my $attr_name = $self->name;
- #return sub {
- # defined $meta_class->get_meta_instance
- # ->get_slot_value($_[0], $attr_name) ? 1 : 0;
- #};
-
- my $code = "sub {\n"
- . 'defined '
- . $meta_instance->inline_get_slot_value('$_[0]', "'$attr_name'", '$_[1]')
- . ' ? 1 : 0;'
- . "\n}";
- my $sub = eval $code;
- confess "Could not eval code:\n$code\nbecause: $@" if $@;
- return $sub;
+ return sub {
+ defined Class::MOP::Class->initialize(Scalar::Util::blessed($_[0]))
+ ->get_meta_instance
+ ->get_slot_value($_[0], $attr_name) ? 1 : 0;
+ };
}
sub process_accessors {
use warnings;
use metaclass (
+ ':attribute_metaclass' => 'InsideOutClass::Attribute',
':instance_metaclass' => 'InsideOutClass::Instance'
);
use strict;
use warnings;
use metaclass (
- ':instance_metaclass' => 'InsideOutClass::Instance'
+ ':attribute_metaclass' => 'InsideOutClass::Attribute',
+ ':instance_metaclass' => 'InsideOutClass::Instance'
);
Baz->meta->add_attribute('bling' => (
use strict;
use warnings;
use metaclass (
- ':attribute_metaclass' => 'ArrayBasedStorage::Attribute',
':instance_metaclass' => 'ArrayBasedStorage::Instance',
);
use strict;
use warnings;
- use metaclass (
- ':attribute_metaclass' => 'ArrayBasedStorage::Attribute',
+ use metaclass (
':instance_metaclass' => 'ArrayBasedStorage::Instance',
);