I'm looking for a way to set up some helper methods from within a parent Moose class, rather than a standalone utility class. If it is possible, it would be a more transparent way of adding Moose sugar to modules, as it does not require explicitly requiring any helper modules (as everything would come via the extends
declaration).
Based on the example provided in the documentation, this is roughly what I am going for:
package Parent;
use Moose;
Moose::Exporter->setup_import_methods(
with_meta => [ 'has_rw' ],
as_is => [ 'thing' ],
also => 'Moose',
);
sub has_rw {
my ( $meta, $name, %options ) = @_;
$meta->add_attribute(
$name,
is => 'rw',
%options,
);
}
# then later ...
package Child;
use Moose;
extends 'Parent';
has 'name';
has_rw 'size';
thing;
However this does not work:
perl -I. -MChild -wle'$obj = Child->new(size => 1); print $obj->size'
String found where operator expected at Child.pm line 10, near "has_rw 'size'" (Do you need to predeclare has_rw?) syntax error at Child.pm line 10, near "has_rw 'size'" Bareword "thing" not allowed while "strict subs" in use at Child.pm line 12. Compilation failed in require. BEGIN failed--compilation aborted.
PS. I've also tried moving the export magic into a role (with Role;
rather than extends Parent;
) but the same errors occur.