Câu hỏi đầu tiên của tôi trên trang này, tôi đến nhanh chóng. Tôi là một nhà phát triển, tôi chủ yếu sử dụng Python và Perl. Tôi đam mê và tôi thực sự thích sự phát triển.Cách sử dụng Loại :: Đường dẫn :: Nhỏ với Moo
Câu hỏi đầu tiên của tôi là về Perl, Moo và Type :: Tiny. Loại :: Tiny là một mô-đun tuyệt vời để sử dụng với Moo tất nhiên, nhưng tôi sẽ quay trở lại chủ đề này trong một câu hỏi khác.
Tôi đã phát hiện ra các loại :: Đường dẫn :: Tiny một mô-đun cưỡng chế cho Moose và Moo, vì vậy tôi đã cố gắng tạo một thư mục thuộc tính trong lớp như mô tả trong documentation, khi dự án của tôi ở Moose nó hoạt động. tôi di chuyển trong Moo, nó không còn hoạt động:
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,
);
trong các thử nghiệm thư mục của tôi:
my $articles = MahewinBlogEngine->articles(directory => getcwd() . '/t/articles');
Lỗi là:
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.
này là bình thường, bởi vì với Moo việc cưỡng chế là một coderef vì vậy tôi cố gắng:
has 'directory' => (
is => 'rw',
isa => AbsPath,
required => 1,
coerce => sub { return "Path" }
);
Lỗi là:
value "Path" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 99.
Nếu tôi không có ép buộc:
value "/home/hobbestigrou/MahewinBlogEngine/t/articles" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 89.
tôi m xin lỗi cho câu hỏi đơn giản này, tôi phải ngu ngốc và bỏ lỡ một cái gì đó, nhưng tôi không thấy những gì có lẽ tôi đã thiếu một cái gì đó trong doc.
Cảm ơn
Có thư mục cần phải là một 'con đường :: đối tượng Tiny'? ... 'coerce => sub {return Đường dẫn :: Tiny-> new ($ _ [0])}'? – mob
Cảm ơn nó hoạt động tuyệt vời. – Hobbestigrou