我尝试按照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.运行该程序

最佳答案

Moose错误消息可能很难破译,但这是关键:


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


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;
}


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

关于perl - 为什么得到“属性(类)未通过类型约束,因为:'MooseX::Runnable::Invocation::RunnableClass'验证失败”?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42139327/

10-09 06:16