इस साइट पर मेरा पहला सवाल है, मैं जल्दी आ गया हूं। मैं एक डेवलपर हूं, मैं मुख्य रूप से पायथन और पर्ल का उपयोग करता हूं। मैं भावुक हूं और मुझे वास्तव में विकास पसंद है।प्रकार का उपयोग कैसे करें :: पथ :: म्यू के साथ छोटे
मेरा पहला सवाल पर्ल, म्यू और टाइप :: टिनी के बारे में है। टाइप :: टिनी कोर्स के लिए म्यू के साथ उपयोग के लिए एक महान मॉड्यूल है, लेकिन मैं इस विषय पर दूसरे प्रश्न में वापस आऊंगा।
मैंने खोज :: पथ :: मूस और म्यू के लिए छोटे मॉड्यूल जबरन की खोज की, इसलिए मैंने documentation में वर्णित मेरी कक्षा में एक विशेषता निर्देशिका बनाने की कोशिश की, क्योंकि मेरी परियोजना मूस में थी, लेकिन तब से मैं मू में ले जाया गया, यह अब काम करता है:
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,
);
मेरी परीक्षण निर्देशिका में:
my $articles = MahewinBlogEngine->articles(directory => getcwd() . '/t/articles');
त्रुटि है:
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.
यह सामान्य है, क्योंकि मू साथ बलात्कार एक coderef है तो मैं करने की कोशिश की:
has 'directory' => (
is => 'rw',
isa => AbsPath,
required => 1,
coerce => sub { return "Path" }
);
त्रुटि है:
value "Path" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 99.
अगर मैं कोई विवश है:
value "/home/hobbestigrou/MahewinBlogEngine/t/articles" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 89.
मैं मुझे इस सरल सवाल के लिए खेद है, मुझे बेवकूफ होना चाहिए और कुछ याद करना चाहिए, लेकिन मुझे नहीं लगता कि शायद मुझे डॉक्टर में कुछ याद आ रहा था।
धन्यवाद
निर्देशिका एक 'पथ :: Tiny' वस्तु होने की जरूरत है? ... 'coerce => सब {वापसी पथ :: छोटा-> नया ($ _ [0])}'? – mob
धन्यवाद यह बहुत अच्छा काम करता है। – Hobbestigrou