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';
41 sub generate_accessor_method {
42 my $attr = (shift)->associated_attribute;
44 my $attr_name = $attr->name;
45 my $meta_instance = $attr->associated_class->get_meta_instance;
48 if (scalar(@_) == 2) {
49 $meta_instance->set_slot_value($_[0], $attr_name, $_[1]);
52 unless ( $meta_instance->is_slot_initialized($_[0], $attr_name) ) {
53 my $value = $attr->has_default ? $attr->default($_[0]) : undef;
54 $meta_instance->set_slot_value($_[0], $attr_name, $value);
57 $meta_instance->get_slot_value($_[0], $attr_name);
62 sub generate_reader_method {
63 my $attr = (shift)->associated_attribute;
65 my $attr_name = $attr->name;
66 my $meta_instance = $attr->associated_class->get_meta_instance;
69 confess "Cannot assign a value to a read-only accessor" if @_ > 1;
71 unless ( $meta_instance->is_slot_initialized($_[0], $attr_name) ) {
72 my $value = $attr->has_default ? $attr->default($_[0]) : undef;
73 $meta_instance->set_slot_value($_[0], $attr_name, $value);
76 $meta_instance->get_slot_value($_[0], $attr_name);
80 package # hide the package from PAUSE
86 our $VERSION = '0.01';
88 use base 'Class::MOP::Instance';
90 sub initialize_all_slots {}
100 LazyClass - An example metaclass with lazy initialization
107 ':attribute_metaclass' => 'LazyClass::Attribute',
108 ':instance_metaclass' => 'LazyClass::Instance',
111 BinaryTree->meta->add_attribute('$:node' => (
116 BinaryTree->meta->add_attribute('$:left' => (
118 default => sub { BinaryTree->new() }
121 BinaryTree->meta->add_attribute('$:right' => (
123 default => sub { BinaryTree->new() }
128 $class->meta->new_object(@_);
133 my $btree = BinaryTree->new();
134 # ... $btree is an empty hash, no keys are initialized yet
138 This is an example metclass in which all attributes are created
139 lazily. This means that no entries are made in the instance HASH
140 until the last possible moment.
142 The example above of a binary tree is a good use for such a
143 metaclass because it allows the class to be space efficient
144 without complicating the programing of it. This would also be
145 ideal for a class which has a large amount of attributes,
146 several of which are optional.
150 Stevan Little E<lt>stevan@iinteractive.comE<gt>
152 Yuval Kogman E<lt>nothingmuch@woobling.comE<gt>
154 =head1 COPYRIGHT AND LICENSE
156 Copyright 2006, 2007 by Infinity Interactive, Inc.
158 L<http://www.iinteractive.com>
160 This library is free software; you can redistribute it and/or modify
161 it under the same terms as Perl itself.