在设置之前,如何在驼鹿属性设置器中处理参数?
我的驼鹿对象具有一个属性,该属性是字符串的阵列。我想通过仅传递单个字符串('string'
),而不是单个字符串的arrayref(['code> ['String'''String' ]
)。
has 'my_list' => (
is => 'rw',
isa => 'ArrayRef[Str]',
);
解决这个问题的正确方法是什么?通过触发
?
我不确定是否在对象构造函数和属性设置程序中或仅在构造函数中都需要它。
My Moose object has an attribute that is an arrayref of strings. I want to make it possible to set it to a single-element list by passing only a single string ('string'
), instead of an arrayref of a single string (['string']
).
has 'my_list' => (
is => 'rw',
isa => 'ArrayRef[Str]',
);
What is the proper way of solving this? Through a trigger
?
I'm not sure yet if I'll need this in both the object constructor and the attribute setter, or only in the constructor.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决此问题的最佳方法是使用 类型强制< /a>(从另一种类型创建一种类型)。
请注意,强制转换为标准 Moose 类型不是一个好主意,因此我们还创建了一个子类型。
The best way to approach this is by using type coercion (creating one type from another).
Note, that it's a bad idea to coerce into standard Moose types, so we also create a subtype.