为什么我得到“属性(类)不传递类型约束,因为:'MooseX :: Runnable :: Invocation :: RunnableClass'的验证失败”?

时间:2017-02-09 14:29:59

标签: perl moose

我正在尝试按照MooseX::Runnable docs中的示例创建一个可运行的Moose程序:

package LaborData::Data::DBIxTest;
use Moose;
use Modern::Perl;
use LaborData::Data::Schema;
with 'DB', 'MooseX::Runnable';

sub run {
  my ($self, $name) = @_; 
  say $name;
}

但是当我跑步时

mx-run LaborData::Data::DBIxTest.pm hi

在命令行上,我收到此错误:

Attribute (class) does not pass the type constraint because: Validation failed for 'MooseX::Runnable::Invocation::RunnableClass' with value "LaborData::Data::DBIxTest.pm" at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Util.pm line 49
    Moose::Util::throw_exception('ValidationFailedForTypeConstraint', 'type', 'MooseX::Types::TypeDecorator=HASH(0x38728e8)', 'value', 'LaborData::Data::DBIxTest.pm', 'attribute', 'Moose::Meta::Attribute=HASH(0x38bd4e8)') called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Attribute.pm line 1265
    Moose::Meta::Attribute::verify_against_type_constraint('Moose::Meta::Attribute=HASH(0x38bd4e8)', 'LaborData::Data::DBIxTest.pm', 'instance', 'MooseX::Runnable::Invocation::MxRun=HASH(0x38a13d8)') called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Attribute.pm line 1252
    Moose::Meta::Attribute::_coerce_and_verify('Moose::Meta::Attribute=HASH(0x38bd4e8)', 'LaborData::Data::DBIxTest.pm', 'MooseX::Runnable::Invocation::MxRun=HASH(0x38a13d8)') called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Attribute.pm line 511
    Moose::Meta::Attribute::initialize_instance_slot('Moose::Meta::Attribute=HASH(0x38bd4e8)', 'Moose::Meta::Instance=HASH(0x3881ad0)', 'MooseX::Runnable::Invocation::MxRun=HASH(0x38a13d8)', 'HASH(0x37fc528)') called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Class.pm line 524
    Class::MOP::Class::_construct_instance('Moose::Meta::Class=HASH(0x3481260)', 'HASH(0x37fc528)') called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Class/MOP/Class.pm line 494
    Class::MOP::Class::new_object('Moose::Meta::Class=HASH(0x3481260)', 'HASH(0x37fc528)') called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Meta/Class.pm line 274
    Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0x3481260)', 'HASH(0x37fc528)') called at /usr/lib/x86_64-linux-gnu/perl5/5.20/Moose/Object.pm line 23
    Moose::Object::new('MooseX::Runnable::Invocation::MxRun', 'class', 'LaborData::Data::DBIxTest.pm', 'plugins', 'HASH(0x387f1a0)', 'parsed_args', 'MooseX::Runnable::Util::ArgParser=HASH(0x3486408)') called at /usr/local/bin/mx-run line 28
    main::run at /usr/local/bin/mx-run line 9

我可以从脚本中正确加载模块而不会出错。我也可以使用technique found here.

来运行程序

1 个答案:

答案 0 :(得分:2)

穆斯错误消息很难解读,但这是关键部分:

  

'MooseX :: Runnable :: Invocation :: RunnableClass'的验证失败,其值为“LaborData :: Data :: DBIxTest.pm”

LaborData::Data::DBIxTest.pm不是有效的类名。您必须删除.pm,例如

mx-run LaborData::Data::DBIxTest hi

如果你很好奇,有效的类名由Params :: Util :: _ CLASS确定,看起来像this

sub _CLASS ($) {
        (defined $_[0] and ! ref $_[0] and $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s) ? $_[0] : undef;
}

换句话说,一个或多个字母数字字符,不以数字开头,并且可选地以::分隔。