2013-10-30 70 views
2

由於繼續使用this answer與我自己的穆斯「類型庫」戰鬥 - 所以試圖使用「MooseX::Types」。MooseX ::類型和強制錯誤

基於上述MooseX::Types文檔,以及「hoobs」上述答案的評論,我定義我自己的「類型」爲未來:

package MyTypes; 
use 5.016; 
use Moose; 
use MooseX::Types -declare => [qw(Dir File)];  
use MooseX::Types::Moose qw(Str); 

use Path::Class::Dir; 
use Path::Class::File; 

class_type Dir, { class => 'Path::Class::Dir' }; 
coerce Dir, from Str, via { Path::Class::Dir->new($_) }; 

class_type File, { class => 'Path::Class::File' }; 
coerce File, from Str, via { Path::Class::File->new($_) }; 

1; 

,並在我的包用它

package MyDir; 
use Moose; 
use warnings; 
use MyTypes qw(Dir); #to get the Dir type and its coercion 

has 'path' => (
    is => 'ro', 
    isa => Dir,  # Dir is defined in the package MyTypes 
    required => 1, 
); 
1; 

,並與下一個簡短的腳本試圖

use 5.016; 
use warnings; 
use MyDir; 
my $d = MyDir->new(path => "/tmp"); 

錯誤:

Attribute (path) does not pass the type constraint because: Validation failed for 'MyTypes::Dir' with value /tmp (not isa Path::Class::Dir) at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Attribute.pm line 1279. 
    Moose::Meta::Attribute::verify_against_type_constraint(Moose::Meta::Attribute=HASH(0x7f9e9b1c2618), "/tmp", "instance", MyDir=HASH(0x7f9e9b826bb8)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Attribute.pm line 1266 
    Moose::Meta::Attribute::_coerce_and_verify(Moose::Meta::Attribute=HASH(0x7f9e9b1c2618), "/tmp", MyDir=HASH(0x7f9e9b826bb8)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Attribute.pm line 536 
    Moose::Meta::Attribute::initialize_instance_slot(Moose::Meta::Attribute=HASH(0x7f9e9b1c2618), Moose::Meta::Instance=HASH(0x7f9e9b1c3588), MyDir=HASH(0x7f9e9b826bb8), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Class/MOP/Class.pm line 525 
    Class::MOP::Class::_construct_instance(Moose::Meta::Class=HASH(0x7f9e9b9e6990), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Class/MOP/Class.pm line 498 
    Class::MOP::Class::new_object(Moose::Meta::Class=HASH(0x7f9e9b9e6990), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Class.pm line 284 
    Moose::Meta::Class::new_object(Moose::Meta::Class=HASH(0x7f9e9b9e6990), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Object.pm line 28 
    Moose::Object::new("MyDir", "path", "/tmp") called at t.pl line 5 

所以,不接受'STR',不要做強制。

上面幾行有什麼錯?我很肯定,它確實是非常小的bug,,因爲我遵循了MooseX :: Types文檔(至少我希望) - 但無法找到錯誤。

我開始與駝鹿真的無藥可救,請幫助...

PS:我的目標是定義都是我自己「種」在一個地方(包),並在任何地方使用它,我需要他們一個「使用...」。

回答

2

您需要告訴穆斯,可以在該屬性上使用強制。您可以通過添加coerce到屬性定義做到這一點:

has 'path' => (
    is => 'ro', 
    isa => Dir,  # Dir is defined in the package MyTypes 
    required => 1, 
    coerce => 1, 
    ); 
+0

達姆,這是F @#^ $ @#^ @ *&^#*^@%^它的錯誤!我非常確定,它比在**'MooseX :: Types'文件中缺少** **。非常感謝你。 ;) – novacik