2 package # hide the package from PAUSE
10 our $VERSION = '0.05';
12 use base 'Class::MOP::Attribute';
14 sub initialize_instance_slot {
15 my ($self, $meta_instance, $instance, $params) = @_;
17 # if the attr has an init_arg, use that, otherwise,
18 # use the attributes name itself as the init_arg
19 my $init_arg = $self->init_arg();
21 if ( exists $params->{$init_arg} ) {
22 my $val = $params->{$init_arg};
23 $meta_instance->set_slot_value($instance, $self->name, $val);
27 sub accessor_metaclass { 'LazyClass::Method::Accessor' }
29 package # hide the package from PAUSE
30 LazyClass::Method::Accessor;
37 our $VERSION = '0.01';
39 use base 'Class::MOP::Method::Accessor';
43 sub _generate_accessor_method {
44 my $attr = (shift)->associated_attribute;
46 my $attr_name = $attr->name;
47 my $meta_instance = $attr->associated_class->get_meta_instance;
50 if (scalar(@_) == 2) {
51 $meta_instance->set_slot_value($_[0], $attr_name, $_[1]);
54 unless ( $meta_instance->is_slot_initialized($_[0], $attr_name) ) {
55 my $value = $attr->has_default ? $attr->default($_[0]) : undef;
56 $meta_instance->set_slot_value($_[0], $attr_name, $value);
59 $meta_instance->get_slot_value($_[0], $attr_name);
64 sub _generate_reader_method {
65 my $attr = (shift)->associated_attribute;
67 my $attr_name = $attr->name;
68 my $meta_instance = $attr->associated_class->get_meta_instance;
71 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
73 unless ( $meta_instance->is_slot_initialized($_[0], $attr_name) ) {
74 my $value = $attr->has_default ? $attr->default($_[0]) : undef;
75 $meta_instance->set_slot_value($_[0], $attr_name, $value);
78 $meta_instance->get_slot_value($_[0], $attr_name);
82 package # hide the package from PAUSE
88 our $VERSION = '0.01';
90 use base 'Class::MOP::Instance';
92 sub initialize_all_slots {}
102 LazyClass - An example metaclass with lazy initialization
109 ':attribute_metaclass' => 'LazyClass::Attribute',
110 ':instance_metaclass' => 'LazyClass::Instance',
113 BinaryTree->meta->add_attribute('node' => (
118 BinaryTree->meta->add_attribute('left' => (
120 default => sub { BinaryTree->new() }
123 BinaryTree->meta->add_attribute('right' => (
125 default => sub { BinaryTree->new() }
130 $class->meta->new_object(@_);
135 my $btree = BinaryTree->new();
136 # ... $btree is an empty hash, no keys are initialized yet
140 This is an example metclass in which all attributes are created
141 lazily. This means that no entries are made in the instance HASH
142 until the last possible moment.
144 The example above of a binary tree is a good use for such a
145 metaclass because it allows the class to be space efficient
146 without complicating the programing of it. This would also be
147 ideal for a class which has a large amount of attributes,
148 several of which are optional.
152 Stevan Little E<lt>stevan@iinteractive.comE<gt>
154 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
156 =head1 COPYRIGHT AND LICENSE
158 Copyright 2006-2008 by Infinity Interactive, Inc.
160 L<http://www.iinteractive.com>
162 This library is free software; you can redistribute it and/or modify
163 it under the same terms as Perl itself.