禁止在Moose类中使用非属性参数

时间:2014-09-16 04:46:22

标签: perl parameters constructor moose

如果在Moose的构造函数调用中有额外的参数不是属性,有没有办法死?例如,这个:

package Shoe;
use Moose;
has 'size'  => (is => 'ro', isa => 'Num');
has 'color' => (is => 'ro', isa => 'Str', default => 'brown');
1;

会死在

my $sneaker = Shoe->new(size => 11, colour => 'white');

因为colour不是Shoe的属性。

我可以发誓我已经看过一个模块或者其他什么来做这件事,但我找不到它。

1 个答案:

答案 0 :(得分:6)

对我而言MooseX::StrictConstructor

package Shoe;
use Moose;
use MooseX::StrictConstructor; # <-- that's all what need
has 'size'  => (is => 'ro', isa => 'Num');
has 'color' => (is => 'ro', isa => 'Str');
1;

package main;
my $sneaker = Shoe->new(size => 11, colour => 'white'); #blows up