麋 - 从Num强迫到ArrayRef [Num]?

时间:2011-09-24 18:36:42

标签: perl moose

好的,我做错了什么 - 穆斯无视我的强制行为:

package moo;

use Moose;
use Moose::Util::TypeConstraints;

subtype Bar => as 'ArrayRef[Num]';

coerce 'Bar' =>
  from 'Num' => via { [ 10 ] }; # this doesn't seem to be getting called

has x => (
  is => 'rw',
  isa => 'Bar',
);

package main;

my $m1 = moo->new(x => [ 3 ]);  # works
my $m2 = moo->new(x => 5);      # doesn't work

1 个答案:

答案 0 :(得分:7)

也许您在定义coerce => 1属性时忘记了x

has x => ( is  => 'rw', isa => 'Bar', coerce => 1 );

`