Moje pierwsze pytanie na tej stronie, przychodzę szybko. Jestem programistą, używam głównie Pythona i Perla. Jestem pasjonatem i bardzo lubię ten rozwój.Jak korzystać z typów :: Ścieżka :: Tiny with Moo
Moje pierwsze pytanie dotyczy Perla, Moo i Type :: Tiny. Typ :: Tiny to świetny moduł do użytku z Moo, ale powrócę do tego tematu w innym pytaniu.
Odkryłem Types :: Path :: Tiny moduł wymuszający dla Moose i Moo, więc próbowałem utworzyć katalog atrybutów w mojej klasie, jak to opisano w documentation, ponieważ mój projekt był w Moose, ale działa, przeniosłem w Moo, to już nie działa:
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,
);
w moim katalogu badań:
my $articles = MahewinBlogEngine->articles(directory => getcwd() . '/t/articles');
błędu jest:
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.
Jest to normalne, ponieważ z Moo przymus jest coderef więc próbowałem:
has 'directory' => (
is => 'rw',
isa => AbsPath,
required => 1,
coerce => sub { return "Path" }
);
Błąd jest:
value "Path" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 99.
Jeśli mam żadnego zmuszania:
value "/home/hobbestigrou/MahewinBlogEngine/t/articles" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 89.
I Przepraszam za to proste pytanie, muszę być głupi i coś przegapić, ale nie widzę, co może mi brakowało coś w dokumencie.
Dzięki
Czy katalog musi być 'Ścieżka :: przedmiot Tiny'? ... 'coerce => sub {return Path :: Tiny-> new ($ _ [0])}'? – mob
Dzięki to działa świetnie. – Hobbestigrou