首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用类型::Path::Tiny Moo

如何使用类型::Path::Tiny Moo
EN

Stack Overflow用户
提问于 2013-08-14 22:21:11
回答 1查看 1.1K关注 0票数 5

我在这个网站上的第一个问题,我很快就来了。我是一个开发人员,我主要使用Python和Perl。我很有激情,我真的很喜欢这种发展。

我的第一个问题是关于Perl、Moo和Type::Tiny。类型:微型当然是与Moo一起使用的一个很好的模块,但是我将在另一个问题中回到这个主题。

我发现了类型::Path::用于Moose和Moo的微型模块强制收集器,所以我试图像在文档中所描述的那样在类中创建一个属性目录,就像我的项目在Moose中工作一样,但是由于我在Moo中迁移,它不再工作了:

代码语言:javascript
复制
package MahewinBlogEngine::Common;

use strict;
use warnings;

use feature "state";

use Moo;
use Types::Path::Tiny qw/Path AbsPath/;

use CHI;
use MahewinBlogEngine::Renderer;

use Type::Params qw( compile );
use Types::Standard qw( slurpy Object Str HashRef ArrayRef );


=attr directory

rw, required, Str. The directory contain articles.

=cut

has 'directory' => (
    is       => 'rw',
    isa      => AbsPath,
    required => 1,
    coerce   => 1,
);

在我的测试目录中:

代码语言:javascript
复制
my $articles = MahewinBlogEngine->articles( directory => getcwd() . '/t/articles' );

错误是:

代码语言:javascript
复制
Invalid coerce '1' for MahewinBlogEngine::Common->directory not a coderef or code-convertible object at /home/hobbestigrou/perl5/perlbrew/perls/perl-5.19.1/lib/site_perl/5.19.1/Method/Generate/Accessor.pm line 618.
Compilation failed in require at /home/hobbestigrou/perl5/perlbrew/perls/perl-5.19.1/lib/site_perl/5.19.1/Module/Runtime.pm line 317.
Compilation failed in require at /home/hobbestigrou/MahewinBlogEngine/lib/MahewinBlogEngine.pm line 8.
BEGIN failed--compilation aborted at /home/hobbestigrou/MahewinBlogEngine/lib/MahewinBlogEngine.pm line 8.
Compilation failed in require at ./benchmark.pl line 10.
BEGIN failed--compilation aborted at ./benchmark.pl line 10.

这是正常的,因为对Moo来说,强制是一个代码,所以我尝试了:

代码语言:javascript
复制
has 'directory' => (
    is       => 'rw',
    isa      => AbsPath,
    required => 1,
    coerce   => sub { return "Path" }
 );

错误是:

代码语言:javascript
复制
value "Path" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 99.

如果我没有胁迫:

代码语言:javascript
复制
value "/home/hobbestigrou/MahewinBlogEngine/t/articles" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 89.

对于这个简单的问题,我很抱歉,我一定很蠢,我错过了一些东西,但是我不知道我在医生身上可能遗漏了什么。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-16 20:21:56

如果您有use strict;use warnings;,那么就没有理由使用use Moo;了。

您还必须为Moo提供一个coerce元素的代码引用,而不是一个真值。

使用类型::微型的方法是调用$type->coercion

代码语言:javascript
复制
package MahewinBlogEngine::Common;

# not needed with Moo
# use strict;
# use warnings;

use Moo;
use Types::Path::Tiny qw/AbsPath/;

...

has 'directory' => (
    is       => 'rw',
    isa      => AbsPath,
    required => 1,
    coerce   => AbsPath->coercion,
);
代码语言:javascript
复制
for( qw'/home ./ ./Documents Documents' ){
  use feature 'say';
  say $_, "\t", MahewinBlogEngine::Common->new( directory => $_ )->directory;
}
代码语言:javascript
复制
/home   /home
./      /home/user
./Documents     /home/user/Documents
Documents       /home/user/Documents
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18243304

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档