perl - Argument for builder subroutine in a moose object -


I currently represent builder method in all objects that extend one of my base classes. For the problem I am facing, I need all the items or either have to read any of their qualities or pass any value.

  # In the role: 'const_string' = & gt; (ISA = & gt; 'str', is => 'ro', default = & gt; 'test');); 'Atr' = & gt; (ISA = & gt; 'str', is => 'ro', builder => '_builder',)); '_builder' is required; # In extended object - desired 1 sub _builder {my ($ self) = shift; # $ Self In Self- & gt; Const_string} # in the extended object - the desired 2 sub _builder {my ($ arg1, $ arg2) = @_; $ $ Anyone can be passed? }  

Is it possible or should I have to do it any other way?

You can not pass arguments for build build methods, they are automatically called by Moose internals , And passed only one argument - the object reference itself The builder should be able to return its value based on what sees in $ self , or any other environment in it.

What type of logic builder do you want to pass through? Can you pass these values ​​to Object Constructor instead, and store them in other attributes?

  # in object #: other_trat_a = & gt; (Is = & gt; 'ro', isa = & gt; 'str',); Other_tra_b = & gt; (Is = & gt; 'ro', isa = & gt; 'str',); Based on the sub builder (my $ self = shift; #other_tr_a_a and other_atr_b}, calculates something # # object # 2 has been created as follows: My $ obj = Class2-> New (other_ATR_A => Value ', other_attr_b = & gt;' value ');  

Also keep in mind that if you have properties built on the basis of the values ​​of other attributes, then you should call them lazy , otherwise the builders / default will run immediately on the creation of the object, and In an undetermined order will delay their definitions before installing them lazy, as long as they do not need the first time.


Comments