From: Brian Manning Date: Mon, 12 May 2008 07:14:36 +0000 (+0000) Subject: - fixed extra comma in Perl5ObjsVsMooseObjs X-Git-Tag: 0_55~180 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=defceb0c10844abe5f21a399b43476217fa73d4d;p=gitmo%2FMoose.git - fixed extra comma in Perl5ObjsVsMooseObjs - fixed quoting of an argument in one of the examples - added BUILD snack --- diff --git a/lib/Moose/Cookbook/Snack/BUILD.pod b/lib/Moose/Cookbook/Snack/BUILD.pod new file mode 100644 index 0000000..48ce92a --- /dev/null +++ b/lib/Moose/Cookbook/Snack/BUILD.pod @@ -0,0 +1,116 @@ + +=pod + +=head1 NAME + +Moose::Cookbook::Snack::BUILD - Overriding the I method to customize +the behaivor of Moose object creation + +=head1 SYNOPSIS + + package Build::Demo; + use Moose; + + has 'example_file' => ( is => 'rw', required => 1); + + sub BUILD { + my $self = shift; + # create the object only if the file does exist + if ( -e $self->example_file ) { + return $self; + } + else { + die('ERROR: file _' . $self->example_file . '_ does not exist'); + } + } # sub BUILD + + package main; + use Moose; + + # '$0' is the name of this script, set automatically by Perl + # this works + my $first_test = Build::Demo->new( example_file => $0 ); + # this should fail (unless there's a file named 'foo' + # in the current directory) + my $second_test = Build::Demo->new( example_file => 'foo' ); + +=head1 DESCRIPTION + +The C method allows you to write your own custom constructors for +your Moose objects. + +=head2 Creating new objects in Perl and Moose + +By convention, most objects in Perl are created by calling a C method +that they have created: + + package My::Perl::Class; + + sub new { + # object initialization code goes here... + } + + package main; + my $object = My::Perl::Class->new(); + +Moose is no different in this respect. However, since Moose handles the +C method for you, how do you change the default behaivor of the +C method in Moose? This is what the C method was designed +for. + + package My::Moose::Class; + + sub BUILD { + # object initialization code goes here... + } + + package main; + my $object = My::Moose::Class->new(); + +=head2 Why would you want a custom constructor? + +If your object needs to verify some behaivor or internal state before it is +created, a good time to do that is when the object is being created. Why +waste resources (CPU, memory) on objects that won't work because of missing +resources? + +=head2 What is 'BUILDALL'? + +(Taken from L) The C method will call every BUILD +method in the inheritance hierarchy, and pass it a hash-ref of the the %params +passed to new. + +=head1 SEE ALSO + +=over 4 + +=item L - The base object for Moose (BUILDALL) + +=item L - Frequently asked questions about Moose (How do +I write custom constructors with Moose?) + +=item L - Subtypes, and modeling a simple Company +class heirarchy (Example usage of BUILD in action) + +=item L - For when things go wrong with Moose ('Roles' +ѕection describes BUILD/BUILDALL) + + + +The L section entitled B for more info about how +the BUILD/BUILDALL methods work. + +=back + +=head1 AUTHOR + +Brian Manning + +=head1 COPYRIGHT AND LICENSE + +Copyright (c)2008 by Brian Manning + +This documentation is free software; you can redistribute it and/or modify +it under the same terms as Perl itself. + +=cut diff --git a/lib/Moose/Cookbook/Snack/HashRef.pod b/lib/Moose/Cookbook/Snack/HashRef.pod index 0bc89bd..d933530 100644 --- a/lib/Moose/Cookbook/Snack/HashRef.pod +++ b/lib/Moose/Cookbook/Snack/HashRef.pod @@ -104,7 +104,7 @@ delete the desired key/value pairs, then assign your modified copy back to the C attribute. Here's an example of appending new key/value pars: my %fruit_aisle_copy = %{$store->fruit_aisle}; - my $avocado = Fruit->new( species => 'P. americana) ); + my $avocado = Fruit->new( species => 'P. americana' ); $fruit_aisle_copy{avocado} = $avocado; $store->fruit_aisle( \%fruit_aisle_copy ); diff --git a/lib/Moose/Cookbook/Snack/Perl5ObjsVsMooseObjs.pod b/lib/Moose/Cookbook/Snack/Perl5ObjsVsMooseObjs.pod index 56dbd19..2f75e4d 100644 --- a/lib/Moose/Cookbook/Snack/Perl5ObjsVsMooseObjs.pod +++ b/lib/Moose/Cookbook/Snack/Perl5ObjsVsMooseObjs.pod @@ -151,7 +151,7 @@ blessed hashes just like the average Perl object is. However, if you access the object's hash reference directly via the latter syntax you will have several problems. -First, Moose, will no longer be able to enforce attribute constraints, +First, Moose will no longer be able to enforce attribute constraints, such as read-only or type constraints. Second, you've broken that object's encapsulation, and encapsulation is one of the reasons you want to use objects in the first place, right?