Ah.. it was called ProduceStoreArray because there's also a ProduceStoreHash.. well...
[gitmo/Moose.git] / lib / Moose / Cookbook / Snack / Perl5ObjsVsMooseObjs.pod
index 3c17875..2f75e4d 100644 (file)
@@ -11,16 +11,15 @@ objects and Moose objects
     package Moose::Demo;
     use Moose; # automagically sets 'strict' and 'warnings'
 
-    has q(script_name) => ( is => q(rw), required => 1);
+    has 'script_name' => ( is => 'rw', required => 1);
 
     package main;
-    use Moose; # needed for the call to 'blessed' below
     
     # '$0' is the name of this script, set automatically by Perl
     my $demo = Moose::Demo->new( script_name => $0 );
 
-    print qq(My name is ) . $demo->script_name . qq(\n);
-    print qq(I am a ) . blessed $demo . qq( type of object\n);
+    print "My name is " . $demo->script_name . "\n";
+    print "I am a " . $demo->meta->name . " type of object\n";
 
 =head1 DESCRIPTION
 
@@ -50,21 +49,25 @@ Let's take a look and find out...
         # verify that the user passed in the 'script_name' attribute
         if ( exists $args{script_name} ) {
             $self->script_name($args{script_name});
-        } else {
-            die q(ERROR: can't create object without 'script_name' );
-        } # if ( exists $args{script_name} )
+        } 
+        else {
+            die "ERROR: can't create object without 'script_name' ";
+        }
 
         # return the object reference back to the caller
         return $self;
-    } # sub new
+    }
     
     sub script_name {
         my $self = shift;
-        # check for arguments; use the argument if passed in, otherwise 
-        # return the existing value (if any)
-        if (@_) { $self->{script_name} = shift }
+        # check for arguments; use the argument 
+        # if passed in, otherwise return the 
+        # existing value (if any)
+        if (@_) { 
+            $self->{script_name} = shift;
+        }
         return $self->{script_name};
-    } # sub script_name
+    }
 
     package main;
     use strict;
@@ -72,8 +75,8 @@ Let's take a look and find out...
 
     my $demo = Perl5::Demo->new( script_name => $0 );
 
-    print qq(My name is ) . $demo->script_name . qq(\n);
-    print qq(I am a ) . ref($demo) . qq( type of object\n);
+    print "My name is " . $demo->script_name . "\n";
+    print "I am a " . ref($demo) . " type of object\n";
 
 Looks more complex, right?  Moose does a lot of the labor when working with
 Perl objects, so that you don't have to.  What are some of the specific
@@ -104,14 +107,17 @@ to do this for ourselves in the Perl 5 example.
 =head3 Difference #3 - Determining an object's class name
 
 The C<ref()> function in Perl 5 is how you determine an object's class name.
-The proper way to do this with Moose is C<$object-E<gt>meta-E<gt>name>
-B<FIXME> $obj->meta->name
+The proper way to do this with Moose is C<$object-E<gt>meta-E<gt>name>;
     
     # an object's class name in Perl 5 OO
-    print qq(I am a ) . ref($demo) . qq( type of object\n);
+    print "I am a " . ref($demo) . " type of object\n";
 
     # an object's class name in Moose
-    print qq(I am a ) . blessed $demo->meta->name . qq( type of object\n);
+    print "I am a " . $demo->meta->name . " type of object\n";
+
+Moose builds on C<Class::MOP> to provide a rich introspection API that
+goes way beyond just getting the class name. Check out the
+C<Class::MOP> documentation for more details.
 
 =head3 Difference #4 - Assigning values to Moose object attributes
 
@@ -119,7 +125,7 @@ When you wish to assign a value directly to an object attribute for a Perl 5
 object, you can either create an object method that handles the value for you;
 
     package Perl5Object;
-    sub set_x { # some code here }
+    sub set_x { # some code here that sets 'x' }
     package main;
     # later on...
     $self->set_x(0);
@@ -139,16 +145,16 @@ labeld B<Accessors>, but briefly:
     # later on...
     $self->x(0);    
 
-The syntax shown for the Perl 5 object (C<$self-E<gt>{x} = 0>) will also work
-on the Moose object, as Moose objects are blessed hashes just like the average
-Perl object is.  However, if you access the object's hash reference directly
-via the latter syntax:
-
-1) Moose will no longer be to enforce having that attribute be read-only if
-you used (C<is =E<gt> ro>) in the object's declaration.
+The syntax shown for the Perl 5 object (C<$self-E<gt>{x} = 0>) will
+also work on the Moose object, as Moose objects are, by default,
+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.
 
-2) You break that object's encapsulation, which is one of the reasons you want
-to use objects in the first place, right?
+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?
 
 =head1 SEE ALSO
 
@@ -157,6 +163,7 @@ to use objects in the first place, right?
 =item L<Moose::Cookbook::Recipe1> - The 'Point' object example
 
 =item L<Moose::Util::TypeConstraints> - Type constraints that Moose can use
+and the tools to extend them or create your own.
 
 =item L<Moose::Cookbook::WTF> - For when things go wrong with Moose
 
@@ -168,9 +175,11 @@ Brian Manning <elspicyjack at gmail dot com>
 
 =head1 COPYRIGHT AND LICENSE
 
-Copyright (c)2008 by Brian Manning
+Copyright 2006-2008 by Infinity Interactive, Inc.
+
+L<http://www.iinteractive.com>
 
-This documentation is free software; you can redistribute it and/or modify
+This library is free software; you can redistribute it and/or modify
 it under the same terms as Perl itself.
 
 =cut