5

My first question on this site, I come quickly. I'm a developer, I mainly use Python and Perl. I am passionate and I really like the development.

My first question is about Perl, Moo, and Type::Tiny. Type::Tiny is a great module for use with Moo of course, but I will return to this subject in another question.

I discovered Types::Path::Tiny a module coercions for Moose and Moo, so I tried to make a attribute directory in my class like as described in the documentation, as my project was in Moose it work, but since I moved in Moo, it no longer works:

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,
);

In my tests directory:

my $articles = MahewinBlogEngine->articles( directory => getcwd() . '/t/articles' );

Error is:

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.

This is normal, because with Moo the coercion is a coderef so I tried:

has 'directory' => (
    is       => 'rw',
    isa      => AbsPath,
    required => 1,
    coerce   => sub { return "Path" }
 );

Error is:

value "Path" did not pass type constraint "Path" (not isa Path::Tiny) (in $self->{"directory"}) at (eval 355) line 99.

If I have no coerce:

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'm sorry for this simple question, I must be stupid and miss something, but I do not see what maybe I was missing something in the doc.

Thanks

shanet
  • 7,246
  • 3
  • 34
  • 46
Hobbestigrou
  • 1,777
  • 1
  • 13
  • 16
  • 1
    Does the directory need to be a `Path::Tiny` object? ... `coerce => sub { return Path::Tiny->new($_[0]) }` ? – mob Aug 14 '13 at 22:38

1 Answers1

6

There is no reason to have use strict; and use warnings; if you have use Moo; as it does that for you.

You also have to give Moo a code reference for the coerce element, not a true value.
The way you get that with Type::Tiny is by calling $type->coercion.

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,
);
for( qw'/home ./ ./Documents Documents' ){
  use feature 'say';
  say $_, "\t", MahewinBlogEngine::Common->new( directory => $_ )->directory;
}
/home   /home
./      /home/user
./Documents     /home/user/Documents
Documents       /home/user/Documents
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129
  • 1
    Hi, you're right there is no need to use strict and warnings pragma with Moo, thanks it's cool. – Hobbestigrou Aug 20 '13 at 13:12
  • Still, you may want to use the [strictures](https://metacpan.org/pod/strictures) module. From the [Moo](https://metacpan.org/pod/Moo#INCOMPATIBILITIES-WITH-MOOSE) POD: _"Handling of warnings: when you use Moo we enable strict and warnings, in a similar way to Moose. The authors recommend the use of `strictures`, which enables FATAL warnings, and several extra pragmas when used in development: [indirect](https://metacpan.org/pod/indirect), [multidimensional](https://metacpan.org/pod/multidimensional), and [bareword::filehandles](https://metacpan.org/pod/bareword::filehandles)."_ – Jay Allen Dec 14 '15 at 09:14
  • @JayAllen Moo used to enable strictures, but that was found to be too problematic for CPAN modules if there were any warnings added to perl. – Brad Gilbert Dec 14 '15 at 14:35
  • Sure if you're developing a CPAN module. But if you're just using it in your own code, strictures is a good thing. – Jay Allen Dec 14 '15 at 16:47
  • @JayAllen I have code that I'm glad didn't `use strictures` because it was basically working fine, all I had to do was handle an `undef` case. I left this in the code because I actually needed to do something extra somewhere else because of an addition to the input which was the underlying cause of the `undef`. So I let it keep issuing the warning until I got around to adding it as a reminder. If I had used strictures, I would have just done enough to stop the warnings. So I will **never** be using it in my own code. ( If I had to deal with credit cards maybe I would ) – Brad Gilbert Dec 14 '15 at 18:18