6 Moose::Cookbook::Snack::BUILD - Custom initialization methods for Moose objects
13 # once the object has been created, don't allow
14 # changes to 'example_file'
15 has 'example_file' => (is => 'ro', required => 1);
19 # create the object only if the 'example_file' exists
20 if (-e $self->example_file) {
24 die('ERROR: file _' . $self->example_file . '_ does not exist');
31 # the name of this script, which works
32 my $first_test = Build::Demo->new(example_file => $0);
33 # this should fail (unless there's a file named 'foo'
34 # in the current directory)
35 my $second_test = Build::Demo->new(example_file => 'foo');
39 The C<BUILD()> method allows you to write your own initialization methods for
42 =head2 Creating new objects in Perl and Moose
44 By convention, most objects in Perl are created by calling a C<new()> method
47 package My::Perl::Class;
50 # object blessing and initialization code goes here...
54 my $object = My::Perl::Class->new();
56 Moose is no different in this respect. However, since Moose handles the
57 C<new()> method for you, how do you change the default behaivor of the
58 C<new()> method in Moose? This is what the C<BUILD()> method was designed
61 package My::Moose::Class;
64 # object initialization code goes here...
68 my $object = My::Moose::Class->new();
70 =head2 Why would you want a custom constructor?
72 If your object needs to verify some behaivor or internal state before it is
73 created, a good time to do that is when the object is being created. Why
74 waste resources (CPU, memory) on objects that won't work because of missing
77 =head2 When would you use an Moose type constraint instead of a custom constructor?
79 Using type constraints via L<Moose::Util::TypeConstraints>, you can verify
80 simple relationships when an object is created:
87 You would want to use the C<BUILD()> method in order to verify more complex
90 package IsoscelesTriangle;
93 =head2 BUILD method is run only if it is defined in the object
95 If your object does not have a C<BUILD> method, then Moose will skip trying to
98 =head2 What is 'BUILDALL'?
100 (Taken from L<Moose::Object>) The C<BUILDALL> method will call every BUILD
101 method in the inheritance hierarchy, and pass it a hash-ref of the the
102 C<%params> passed to the C<new()> method.
108 =item L<Moose::Object> - The base object for Moose (BUILDALL)
110 =item L<Moose::Cookbook::FAQ> - Frequently asked questions about Moose
111 (How do I write custom constructors with Moose?)
113 =item L<Moose::Cookbook::Recipe4> - Subtypes, and modeling a simple
114 Company class heirarchy (Example usage of BUILD in action)
116 =item L<Moose::Cookbook::WTF> - For when things go wrong with Moose
117 ('Roles' section describes BUILD/BUILDALL)
123 Brian Manning <elspicyjack at gmail dot com>
125 =head1 COPYRIGHT AND LICENSE
127 Copyright 2006-2008 by Infinity Interactive, Inc.
129 L<http://www.iinteractive.com>
131 This library is free software; you can redistribute it and/or modify
132 it under the same terms as Perl itself.