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?