正确的方式定义和转换Moose属性类型

nov*_*cik 3 perl moose

有:

package MyPath;
use strict;
use warnings;
use Moose;

has 'path' => (
    is => 'ro',
    isa => 'Path::Class::Dir',
    required => 1,
);
1;
Run Code Online (Sandbox Code Playgroud)

但是想用两种方式创建这个对象,比如:

use strict;
use warnings;
use MyPath;
use Path::Class;
my $o1 = MyPath->new(path => dir('/string/path')); #as Path::Class::Dir
my $o2 = MyPath->new(path => '/string/path'); #as string (dies - on attr type)
Run Code Online (Sandbox Code Playgroud)

当用'Str'调用它时 - 想要在MyPath包内部将它转换为Class :: Path :: Dir,所以,两者:$o1->path并且$o2->path应该返回祝福Path::Class::Dir

当我尝试将定义扩展到下一个时:

has 'path' => (
    is => 'ro',
    isa => 'Path::Class::Dir|Str',    #allowing both attr types
    required => 1,
);
Run Code Online (Sandbox Code Playgroud)

它不起作用,仍然需要"稍微"转换StrPath::Class::Dir自动内部package MyPath...

有人可以给我一些提示吗?

编辑:根据Oesor的提示我找到了比我需要的东西:

coerce Directory,
    from Str,       via { Path::Class::Dir->new($_) };

has 'path' => (
    is => 'ro',
    isa => 'Directory',
    required => 1,
);
Run Code Online (Sandbox Code Playgroud)

但还是不知道如何正确使用它......

还有一些提示吗?

ike*_*ami 5

你正在寻找类型的coersion.

use Moose;
use Moose::Util::TypeConstraints;
use Path::Class::Dir;

subtype 'Path::Class::Dir',
   as 'Object',
   where { $_->isa('Path::Class::Dir') };

coerce 'Path::Class::Dir',
    from 'Str',
        via { Path::Class::Dir->new($_) };

has 'path' => (
    is       => 'ro',
    isa      => 'Path::Class::Dir',
    required => 1,
    coerce   => 1,
);
Run Code Online (Sandbox Code Playgroud)