tags:

views:

44

answers:

1

I'm using Moose to write an object module.

I currently have a few mandatory fields:

has ['length'] => (
    is       => 'ro',
    isa      => 'Int',
    required => 1,
);

has ['is_verified'] => (
    is       => 'ro',
    isa      => 'Bool',
    required => 1,
);

has ['url'] => (
    is       => 'ro',
    isa      => 'Str',
    required => 1,
);

After the object was initialized with those fields, I would like to create some structure and use it from the object methods.

how (where) should I do that?

+3  A: 

There are (at least) two possibilities:

  1. You can create a BUILD sub. It gets called automatically after the object is initialized.

  2. You create a normal attribute and mark it lazy. Then you provide a sub that creates this attribute: either builder or default. You can read more about this in the manual.

musiKk
+1 Thank you. Just to clarify, `BUILD` is documented here: http://search.cpan.org/~drolsky/Moose-1.17/lib/Moose/Manual/Construction.pod. Now, where should I 'put' my structure (say, subref)? Should I define another 'has' for it and mark it as not required?
David B
I'd create another attribute (with `has`), make it not required and read-only. If you use 1., you can give it a private writer (with an underscore as first character) for writing inside of `BUILD`. If you use 2., you just need the builder/default return the desired value.
musiKk