From: Dave Rolsky Date: Tue, 2 Sep 2008 16:10:47 +0000 (+0000) Subject: Add some examples of object construction to the synopsis, since this X-Git-Tag: 0.57~29 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d68bf053844f1fa2513de22acd02d5de44fb6dd1;p=gitmo%2FMoose.git Add some examples of object construction to the synopsis, since this is pretty important, and shouldn't be buried most of the way down the text. --- diff --git a/lib/Moose/Cookbook/Basics/Recipe1.pod b/lib/Moose/Cookbook/Basics/Recipe1.pod index 28f88de..2c19b78 100644 --- a/lib/Moose/Cookbook/Basics/Recipe1.pod +++ b/lib/Moose/Cookbook/Basics/Recipe1.pod @@ -31,6 +31,14 @@ Moose::Cookbook::Basics::Recipe1 - The (always classic) B example. $self->z(0); }; + .... + + # hash or hashrefs are ok for the constructor + my $point1 = Point->new(x => 5, y => 7); + my $point2 = Point->new({x => 5, y => 7}); + + my $point3d = Point3D->new(x => 5, y => 42, z => -5); + =head1 DESCRIPTION This is the classic Point example. It is taken directly from the Perl @@ -140,13 +148,17 @@ is often a question of style as much as functionality. Since B inherits from L, it will also inherit the default L constructor: - my $point = Point ->new(x => 1, y => 2); - my $point3d = Point3D->new(x => 1, y => 2, z => 3); + my $point1 = Point->new(x => 5, y => 7); + my $point2 = Point->new({x => 5, y => 7}); + + my $point3d = Point3D->new(x => 5, y => 42, z => -5); The C constructor accepts a named argument pair for each -attribute defined by the class. In this particular example, the -attributes are required, and calling C without them will throw an -error. +attribute defined by the class, which you can provide as a hash or +hash reference. In this particular example, the attributes are +required, and calling C without them will throw an error. + + my $point = Point->new( x => 5 ); # no y, kaboom! From here on, we can use C<$point> and C<$point3d> just as you would any other Perl 5 object. For a more detailed example of what can be