Question

As continue of this answer wow im fighting with the my own Moose "type library" - so trying to use "MooseX::Types".

Based on the above MooseX::Types docs, and "hoobs" comment to the above answer, I defined my own "types" as next:

package MyTypes;
use 5.016;
use Moose;
use MooseX::Types -declare => [qw( Dir File )];    
use MooseX::Types::Moose qw( Str );

use Path::Class::Dir;
use Path::Class::File;

class_type Dir, { class => 'Path::Class::Dir' };
coerce Dir, from Str, via { Path::Class::Dir->new($_) };

class_type File, { class => 'Path::Class::File' };
coerce File, from Str, via { Path::Class::File->new($_) };

1;

and used it in my package

package MyDir;
use Moose;
use warnings;
use MyTypes qw(Dir);    #to get the Dir type and its coercion

has 'path' => (
    is => 'ro',
    isa => Dir,      # Dir is defined in the package MyTypes
    required => 1,
);
1;

and tried with the next short script

use 5.016;
use warnings;
use MyDir;
my $d = MyDir->new(path => "/tmp");

Error:

Attribute (path) does not pass the type constraint because: Validation failed for 'MyTypes::Dir' with value /tmp (not isa Path::Class::Dir) at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Attribute.pm line 1279.
    Moose::Meta::Attribute::verify_against_type_constraint(Moose::Meta::Attribute=HASH(0x7f9e9b1c2618), "/tmp", "instance", MyDir=HASH(0x7f9e9b826bb8)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Attribute.pm line 1266
    Moose::Meta::Attribute::_coerce_and_verify(Moose::Meta::Attribute=HASH(0x7f9e9b1c2618), "/tmp", MyDir=HASH(0x7f9e9b826bb8)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Attribute.pm line 536
    Moose::Meta::Attribute::initialize_instance_slot(Moose::Meta::Attribute=HASH(0x7f9e9b1c2618), Moose::Meta::Instance=HASH(0x7f9e9b1c3588), MyDir=HASH(0x7f9e9b826bb8), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Class/MOP/Class.pm line 525
    Class::MOP::Class::_construct_instance(Moose::Meta::Class=HASH(0x7f9e9b9e6990), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Class/MOP/Class.pm line 498
    Class::MOP::Class::new_object(Moose::Meta::Class=HASH(0x7f9e9b9e6990), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Meta/Class.pm line 284
    Moose::Meta::Class::new_object(Moose::Meta::Class=HASH(0x7f9e9b9e6990), HASH(0x7f9e9b826a98)) called at /Users/me/perl5/perlbrew/perls/perl-5.16.3/lib/site_perl/5.16.3/darwin-2level/Moose/Object.pm line 28
    Moose::Object::new("MyDir", "path", "/tmp") called at t.pl line 5

So, doesn't accept the 'Str' and don't do the coercion.

What is wrong in the above few lines? I'm pretty sure than it is really very small bug, because i followed the MooseX::Types docs (at least i hope) - but unable to find the error.

I'm starting be really hopeless with Moose, please HELP...

Ps: My goal is defining all my own "types" in one place (package) and use it everywhere where i need them with one single "use...".

Was it helpful?

Solution

You need to tell Moose that it's OK to use coercion on that attribute. You do this by adding coerce into the attribute definition:

has 'path' => (
    is => 'ro',
    isa => Dir,      # Dir is defined in the package MyTypes
    required => 1,
    coerce => 1,
    );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top