# loaded in the symbol table
next if _is_class_already_loaded($super);
# otherwise require it ...
- ($super->require)
+ # NOTE:
+ # just in case the class we are
+ # loading has a locally defined
+ # &require, we make sure that we
+ # use the on in UNIVERSAL
+ ($super->UNIVERSAL::require)
|| confess "Could not load module '$super' because : " . $UNIVERSAL::require::ERROR;
}
}
=head1 SYNOPSIS
package Point;
- use strict;
- use warnings;
use Moose;
has 'x' => (isa => 'Int', is => 'ro');
}
package Point3D;
- use strict;
- use warnings;
use Moose;
extends 'Point';
places. And now, onto the code:
As with all Perl 5 classes, a Moose class is defined in a package.
-Of course we always use C<strict> and C<warnings> (don't forget
-that a kitten will die if you don't) and then we C<use Moose>.
+Moose now handles turning C<strict> and C<warnings> on for you, so
+all you need do is say C<use Moose>, and no kittens will die.
By loading Moose, we are enabeling the Moose "environment" to be
loaded within our package. This means that we export some functions
=head1 SYNOPSIS
package BankAccount;
- use strict;
- use warnings;
use Moose;
has 'balance' => (isa => 'Int', is => 'rw', default => 0);
}
package CheckingAccount;
- use strict;
- use warnings;
use Moose;
extends 'BankAccount';
=head1 SYNOPSIS
package BinaryTree;
- use strict;
- use warnings;
use Moose;
has 'node' => (is => 'rw', isa => 'Any');
In the second recipe the B<BankAccount>'s C<balance> slot had a
default value of C<0>. Since Perl will copy strings and numbers
by value, this was all we had to say. But for any other item
-(ARRAY ref, HASH ref, object instance, etc) Perl will copy by
-reference. This means that if I were to do this:
+(ARRAY ref, HASH ref, object instance, etc) you would need to
+wrap this into a CODE reference, so this:
has 'foo' => (is => 'rw', default => []);
-Every single instance of that class would get a pointer to the
-same ARRAY ref in their C<foo> slot. This is almost certainly
-B<not> the behavior you intended. So, the solution is to wrap
-these defaults into an anon-sub, like so:
+is actually illegal in Moose. Instead, what you really want is
+to do this:
has 'foo' => (is => 'rw', default => sub { [] });
=head1 SYNOPSIS
package Address;
- use strict;
- use warnings;
use Moose;
use Moose::Util::TypeConstraints;
has 'zip_code' => (is => 'rw', isa => 'USZipCode');
package Company;
- use strict;
- use warnings;
use Moose;
use Moose::Util::TypeConstraints;
};
package Person;
- use strict;
- use warnings;
use Moose;
has 'first_name' => (is => 'rw', isa => 'Str', required => 1);
}
package Employee;
- use strict;
- use warnings;
use Moose;
extends 'Person';
=head1 SYNOPSIS
package Request;
- use strict;
- use warnings;
use Moose;
use Moose::Util::TypeConstraints;
And of course, our coercions do nothing unless they are told to,
like so:
-
- has 'base' => (is => 'rw', isa => 'Uri', coerce => 1);
- has 'uri' => (is => 'rw', isa => 'Uri', coerce => 1);
+
+ has 'base' => (is => 'rw', isa => 'Uri', coerce => 1);
+ has 'uri' => (is => 'rw', isa => 'Uri', coerce => 1);
As you can see, re-using the coercion allows us to enforce a
consistent and very flexible API across multiple accessors.
=head1 SYNOPSIS
package Eq;
- use strict;
- use warnings;
use Moose::Role;
requires 'equal_to';
}
package Comparable;
- use strict;
- use warnings;
use Moose::Role;
with 'Eq';
}
package Printable;
- use strict;
- use warnings;
use Moose::Role;
requires 'to_string';
package US::Currency;
- use strict;
- use warnings;
use Moose;
with 'Comparable', 'Printable';