package MyPath;
use strict;
use warnings;
use Moose;
has 'path' => (
is => 'ro',
isa => 'Path::Class::Dir',
required => 1,
);
1;
但要創建這個對象有兩種方式,如:
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)
當用「STR」稱呼它 - 希望將其在MyPath包內部轉換爲Class :: Path :: Dir,因此,兩個:$o1->path
和$o2->path
應該返回祝福Path::Class::Dir
當我試圖擴展defin銀行足球比賽到下一個:
has 'path' => (
is => 'ro',
isa => 'Path::Class::Dir|Str', #allowing both attr types
required => 1,
);
它不工作,仍然需要「有點」轉換Str
到Path::Class::Dir
自動-內部的package MyPath
...
可能有人給我一些提示?
編輯:基於Oesor的提示,我發現比我更需要像成才:
coerce Directory,
from Str, via { Path::Class::Dir->new($_) };
has 'path' => (
is => 'ro',
isa => 'Directory',
required => 1,
);
但仍然還沒有知道如何正確地使用它...
一些更多的提示嗎?
謝謝 - 非常好。 – novacik
你的'子類型'Path :: Class :: Dir'...'聲明可以更自然地寫成'class_type'Path :: Class :: Dir';'。 – hobbs