I think the only One way to avoid this is to use MooseX::Declare
.
MooseX::Declare
is a macro
which turns below into your example:
use MooseX::Declare;
class Person {
# attributes
# methods
}
It automatically inserts namespace::autoclean
and makes the class immutable.
For extending classes you do:
class Person extends Human { ... }
And for adding roles you do:
class Person with BlueEyeRole { ... }
And you can easily combine these:
class Person extends Human with BlueEyeRole { ... }
You also get some other defined keywords, for eg. method
:
class Person {
has 'name' => (is => 'rw', isa => 'Str');
method hello { "Hello " . $self->name }
}
If you did want to make your class mutable then its:
class Person is mutable { ... }
Maybe there is a technical reason why it isn't possible or why it shouldn't be done?
Technically it would be difficult to pull this all together. MooseX::Declare
makes use of Devel::Declare
to build the necessarily syntax for the Perl to interpret.
So if the boiler plate is an issue for you then consider using MooseX::Declare
. I've used it on a lot of projects with no issues and find it ideal when quickly sketching together a class based app. However most of the time I'm happy with the boilerplate and so stick with standard Moose
.