perl – Can MooseX :: Method :: Signature可以与非Moose类一起使用吗?

前端之家收集整理的这篇文章主要介绍了perl – Can MooseX :: Method :: Signature可以与非Moose类一起使用吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_6@ 我尝试了以下内容

package Foo;
sub new {
    my ($type) = @_;
    return bless {},$type;
}

package Bar;
use Moose;

package Baz;
use Moose;
use MooseX::Method::Signatures;

method exec1 (Foo $f,Bar $b) {
    ...;
}
method exec2 ($f where {$_->isa('Foo')},Bar $b) {
    ...;
}

我发现exec2可以工作,但对于exec1,存在编译时错误.如果Foo是Mouse对象(这些是我无法更改的类),则会发生同样的错误.

'Foo' could not be parsed to a type constraint - maybe you need to pre-declare the type with class_type at C:/strawberry/perl/site/lib/Parse/Method/Signatures/TypeConstraint.pm line 74

但有没有办法可以使用exec1签名?

解决方法

方法声明之前添加

use Moose::Util::TypeConstraints;
BEGIN { class_type 'Foo'; }

错误很明显,对于MXMS无论如何;)

猜你在找的Perl相关文章