如果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的属性。

我可以发誓我已经看过模块或执行此操作的东西,但是找不到。

最佳答案

对我而言,作品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

10-04 11:40