Perl Moose::Util::TypeConstraints 错误?关于名称具有无效字符的错误是什么?
我跟踪 Moose::Util::TypeConstraints 异常已经好几个小时了,我不明白它在哪里检查类型并告诉我名称不正确。我将错误跟踪到一个简化的示例以尝试找到问题,但它只是表明我不明白。
我遇到了 Moose::Util::TypeConstraints bug 吗?
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";
That has been hours i am tracking a Moose::Util::TypeConstraints exceptions, i don't understand where it gets to check a type and tells me that the name is incorrect. I tracked the error to a reduced example to try to locate the problem, and it just shows me that i do not get it.
Did i get to a Moose::Util::TypeConstraints bug ?
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.
Below, the code that crashes:
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";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从对霍布答案的评论中提升这一点。
发生的情况是,通过
extends 'Something::File';
继承到Something::File::LocalFile
的方法名称type
是与Moose::Util::TypeConstraints
导出的type
函数发生冲突。我相信,如果您在内部使用
namespace::autoclean
Something::File::LocalFile
你会发现这个问题消失了。或者,如果您将Moose::Util::TypeConstraints
的导出限制为仅您需要的函数(即使用 Moose::Util::TypeConstraints qw(subtype)
问题也会消失。Elevating this from a comment on hobb's answer.
What's happening is that your method name
type
that is inherited intoSomething::File::LocalFile
viaextends 'Something::File';
is colliding with thetype
function exported byMoose::Util::TypeConstraints
.I believe that if you use
namespace::autoclean
insideSomething::File::LocalFile
you'll see this problem go away. Alternatively if you limit the exports fromMoose::Util::TypeConstraints
to only the functions you need (ieuse Moose::Util::TypeConstraints qw(subtype)
the problem will also go away.看起来
Moose::Util::TypeConstraints::type
意外地导入到Something::File
中并破坏了type
的访问器属性。由于 TypeConstraints 的type
方法期望获取类型名称作为其第一个参数,而不是Something::File
的实例,因此它会抛出奇怪的错误消息(在尝试字符串化您的实例)。我不确定您粘贴的代码示例会如何发生这种情况,但回溯似乎非常明确。也许您正在运行的代码与您粘贴的代码不同,或者也许我只是在凌晨 3 点有点糊涂。
It looks like
Moose::Util::TypeConstraints::type
is accidentally getting imported intoSomething::File
and clobbering the accessor for yourtype
attribute. Since TypeConstraints'type
method expects to get a type name as its first argument, and not an instance ofSomething::File
, it throws that weird error message (after attempting to stringify your instance).I'm not sure how this would happen with the code sample you pasted, but the backtrace seems pretty unambiguous. Perhaps the code you're running is different from what you pasted, or perhaps I'm just a little bit dense at 3am.