Perl Moose :: Util :: TypeConstraints错误?这个名字有什么错误,有无效的字符?

时间:2010-06-10 05:38:24

标签: perl moose

我一直在跟踪一个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"; 

2 个答案:

答案 0 :(得分:4)

提升对霍布答案的评论。

正在发生的事情是,通过type继承到Something::File::LocalFile的方法名称extends 'Something::File';type导出的Moose::Util::TypeConstraints函数发生冲突。

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

答案 1 :(得分:2)

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

我不确定您粘贴的代码示例会发生什么情况,但回溯看起来非常明确。也许您运行的代码与您粘贴的代码不同,或者我可能在凌晨3点稍微密集。