Perl Moose::Util::TypeConstraints 错误?关于名称具有无效字符的错误是什么?

发布于 2024-09-05 05:31:09 字数 2034 浏览 2 评论 0原文

我跟踪 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南街女流氓 2024-09-12 05:31:09

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

发生的情况是,通过 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 into Something::File::LocalFile via extends 'Something::File'; is colliding with the type function exported by Moose::Util::TypeConstraints.

I believe that if you use namespace::autoclean inside Something::File::LocalFile you'll see this problem go away. Alternatively if you limit the exports from Moose::Util::TypeConstraints to only the functions you need (ie use Moose::Util::TypeConstraints qw(subtype) the problem will also go away.

好菇凉咱不稀罕他 2024-09-12 05:31:09

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

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

It looks like Moose::Util::TypeConstraints::type is accidentally getting imported into Something::File and clobbering the accessor for your type attribute. Since TypeConstraints' type method expects to get a type name as its first argument, and not an instance of Something::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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文