开发者

How can I create a structure after a Moose object was initialized?

开发者 https://www.devze.com 2023-01-20 18:44 出处:网络
I\'m using Moose to write an object modul开发者_StackOverflowe. I currently have a few mandatory fields:

I'm using Moose to write an object modul开发者_StackOverflowe.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消