# verify that the user passed in the 'script_name' attribute
if ( exists $args{script_name} ) {
$self->script_name($args{script_name});
- } else {
+ }
+ else {
die "ERROR: can't create object without 'script_name' ";
- } # if ( exists $args{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;
# an object's class name in Moose
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
When you wish to assign a value directly to an object attribute for a Perl 5
# 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 Infinity Interactive, Inc., 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