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
# 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;
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
=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
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);
# 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
=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
=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