Question

The synopsis for MooseX::Types::URI says:

use MooseX::Types::URI qw(Uri FileUri DataUri);

So, my example code:

package My;
use 5.014;
use warnings;
use Moose;
use MooseX::Types::URI qw(Uri);

has 'url' => (
    is => 'rw',
    isa => 'Maybe[Uri]',
    coerce => 1,
);

package main;
my $m0 = My->new(); #without url - ok

Error:

You cannot coerce an attribute (url) unless its type (Maybe[Uri]) has a coercion at ./maybe line 9.

With:

my $m1 = My->new( url => 'http://example.com' );

different error:

You cannot coerce an attribute (url) unless its type (Maybe[Uri]) has a coercion at ./maybe line 9. Attribute (url) does not pass the type constraint because: Validation failed for 'Maybe[Uri]' with value http://example.com at


Ok, so next try - without '':

has 'url' => (
    is => 'rw',
    isa => Maybe[Uri],
    coerce => 1,
);

Error:

syntax error at ./maybe line 11, near "Maybe["
Execution of ./maybe aborted due to compilation errors.

Another try, added the use MooseX::Types::Moose qw( Maybe ); :

use MooseX::Types::Moose qw( Maybe );
use MooseX::Types::URI qw(Uri);

has 'url' => (
    is => 'rw',
    isa => Maybe[Uri],
    coerce => 1,
);

...
my $m1 = My->new( url => 'http://example.com' );

You cannot coerce an attribute (url) unless its type (Maybe[MooseX::Types::URI::Uri]) has a coercion at ./maybe line 9.

Attribute (url) does not pass the type constraint because: Validation failed for 'Maybe[MooseX::Types::URI::Uri]' with value http://example.com at

Don't works anything. Don't want anymore more tryings, what are unsuccessful at all.

I really want LEARN how to decide how to use the already developed CPAN defined MooseX Types, when to use the '' and when not, and how the above will works...

Could please someone explain me, what I'm doing wrong? What I missed from from any doccu, especially from:

or what else should I read?

An hopeless Moose beginner... ;(

Edit

My question is based on this answer (and others too), where - for example:

use MooseX::Types::Moose qw( ArrayRef );
use MooseX::Types::Path::Class qw( Dir );

has 'volumes' => (
    is => 'ro',
    isa => ArrayRef[Dir],
    required => 1,
    coerce => 1,
);

The bareword "syntax" is a must. I only changed the ArrayRef to Maybe and Dir to Uri.

So, the main question remains, how an ordinary perl beginner can decide when can/must use the "bareword" syntax and when the "string"-syntax?

Why the ArrayRef[Dir] is valid (and must) and the Maybe[Uri] not? Why such inconsistency? To understand this, i must study the sources for Moose*? HOW TO understand this? What I should READ?

Was it helpful?

Solution

Could please someone explain me, what I'm doing wrong?

Nothing. Like the message says, there's no rule defining how to coerce a Str to Maybe[Uri]. You'd have to define one.

That said, I don't know if that's possible. The alternative is to use

'Undef|Uri'

instead of

'Maybe[Uri]'

Ok, so next try - without ''

Maybe[...] is simply not valid Perl syntax.

Another try, added the use MooseX::Types::Moose qw( Maybe );

Actually, this works for me (i.e. gives the coercion error rather than a syntax error). As it should, it creates a function that returns the string Maybe[Uri] when passed a reference to an array containing the string Uri (Maybe( [ Uri() ] ) aka Maybe[Uri]).


I really want LEARN how to decide how to use the already developed CPAN defined MooseX Types, when to use the '' and when not, and how the above will works...

You have to provide a string that's the name of a type. Usually that done using a string literal (e.g. 'Str'), but any expression will do (e.g. chr(53).chr(74).chr(72)).


Notes:

  1. I once provide a patch to add coercion support to Maybe, but it was rejected because they don't like Maybe. (At least, that's what they told me.)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top