0

那是我跟踪 Moose::Util::TypeConstraints 异常的几个小时,我不明白它在哪里检查类型并告诉我名称不正确。我将错误跟踪到一个简化的示例以尝试定位问题,它只是告诉我我没有得到它。

我遇到了 Moose::Util::TypeConstraints 错误吗?

aoffice:new alex$ perl -c ../codesnippets/typeconstrainterror.pl 
../codesnippets/typeconstrainterror.pl syntax OK
aoffice:new alex$ perl -d ../codesnippets/typeconstrainterror.pl 
(...)
DB<1> r
Something::File::LocalFile=HASH(0x100d1bfa8) contains invalid characters for a type name. Names can contain alphanumeric character, ":", and "."
 at /opt/local/lib/perl5/vendor_perl/5.10.1/darwin-multi-2level/Moose/Util/TypeConstraints.pm line 508
    Moose::Util::TypeConstraints::_create_type_constraint('Something::File::LocalFile=HASH(0x100d1bfa8)', undef, undef, undef, undef) called at /opt/local/lib/perl5/vendor_perl/5.10.1/darwin-multi-2level/Moose/Util/TypeConstraints.pm line 285
    Moose::Util::TypeConstraints::type('Something::File::LocalFile=HASH(0x100d1bfa8)') called at ../codesnippets/typeconstrainterror.pl line 7
    Something::File::is_slink('Something::File::LocalFile=HASH(0x100d1bfa8)') called at ../codesnippets/typeconstrainterror.pl line 33
Debugged program terminated.  Use q to quit or R to restart,
  use o inhibit_exit to avoid stopping after program termination,
  h q, h R or h o to get additional info.  

下面是崩溃的代码:

package Something::File;
use Moose;
has 'type' =>(is=>'ro', isa=>'Str', writer=>'_set_type' );

sub is_slink {
    my $self = shift;
    return ( $self->type eq 'slink' );
}

no Moose;
__PACKAGE__->meta->make_immutable;
1;


package Something::File::LocalFile;
use Moose;
use Moose::Util::TypeConstraints;

extends 'Something::File';

subtype 'PositiveInt'
     => as 'Int'
     => where { $_ >0 }
     => message { 'Only positive greater than zero integers accepted' };

no Moose;
__PACKAGE__->meta->make_immutable;
1;


my $a = Something::File::LocalFile->new;
# $a->_set_type('slink');
print $a->is_slink ." end\n"; 
4

2 回答 2

4

从对霍布的回答的评论中提升这一点。

发生的事情是您type继承到Something::File::LocalFilevia的方法名称extends 'Something::File';与.typeMoose::Util::TypeConstraints

我相信,如果你namespace::autoclean在里面使用,Something::File::LocalFile你会看到这个问题消失了。或者,如果您将导出限制Moose::Util::TypeConstraints为仅您需要的功能(即use Moose::Util::TypeConstraints qw(subtype)问题也会消失。

于 2010-06-10T14:26:33.710 回答
2

看起来像是Moose::Util::TypeConstraints::type意外导入Something::File并破坏了您的type属性的访问器。由于 TypeConstraints 的type方法期望将类型名称作为其第一个参数,而不是 的实例Something::File,因此它会抛出奇怪的错误消息(在尝试对您的实例进行字符串化之后)。

我不确定您粘贴的代码示例会如何发生这种情况,但回溯似乎非常明确。也许您正在运行的代码与您粘贴的代码不同,或者也许我在凌晨 3 点有点密集。

于 2010-06-10T08:20:54.233 回答