11 use base 'Class::MOP::Class';
13 sub construct_instance {
14 my ($class, %params) = @_;
16 foreach my $attr (map { $_->{attribute} } $class->compute_all_applicable_attributes()) {
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 = $attr->has_init_arg() ? $attr->init_arg() : $attr->name;
20 # try to fetch the init arg from the %params ...
22 $val = $params{$init_arg} if exists $params{$init_arg};
23 # now add this to the instance structure
24 # only if we have found a value at all
25 $instance->{$attr->name} = $val if defined $val;
30 package LazyClass::Attribute;
35 use Class::MOP 'meta';
37 our $VERSION = '0.01';
39 use base 'Class::MOP::Attribute';
41 sub generate_accessor_method {
42 my ($self, $attr_name) = @_;
44 if (scalar(@_) == 2) {
45 $_[0]->{$attr_name} = $_[1];
48 if (!exists $_[0]->{$attr_name}) {
49 my $attr = $self->associated_class->get_attribute($attr_name);
50 $_[0]->{$attr_name} = $attr->has_default ? $attr->default($_[0]) : undef;
57 sub generate_reader_method {
58 my ($self, $attr_name) = @_;
60 if (!exists $_[0]->{$attr_name}) {
61 my $attr = $self->associated_class->get_attribute($attr_name);
62 $_[0]->{$attr_name} = $attr->has_default ? $attr->default($_[0]) : undef;
76 LazyClass - An example metaclass with lazy initialization
83 LazyClass->initialize($_[0] => (
84 ':attribute_metaclass' => 'LazyClass::Attribute'
88 BinaryTree->meta->add_attribute('$:node' => (
93 BinaryTree->meta->add_attribute('$:left' => (
95 default => sub { BinaryTree->new() }
98 BinaryTree->meta->add_attribute('$:right' => (
100 default => sub { BinaryTree->new() }
105 bless $class->meta->construct_instance(@_) => $class;
110 my $btree = BinaryTree->new();
111 # ... $btree is an empty hash, no keys are initialized yet
115 This is an example metclass in which all attributes are created
116 lazily. This means that no entries are made in the instance HASH
117 until the last possible moment.
119 The example above of a binary tree is a good use for such a
120 metaclass because it allows the class to be space efficient
121 without complicating the programing of it. This would also be
122 ideal for a class which has a large amount of attributes,
123 several of which are optional.
127 Stevan Little E<lt>stevan@iinteractive.comE<gt>
129 =head1 COPYRIGHT AND LICENSE
131 Copyright 2006 by Infinity Interactive, Inc.
133 L<http://www.iinteractive.com>
135 This library is free software; you can redistribute it and/or modify
136 it under the same terms as Perl itself.