Hello Monks,
Following a previous conversation (for which I thank all the Monks):
href="http://www.perlmonks.org/?node_id=887227
I have started to use MooseX::SimpleConfig
to have object constructions from config files.
But I have a problem related to types:
I would like to have an ArrayRef of objects, but I forced to have an ArrayRef of hashes.
How I can force a type to be another one?
Here there is some code, just to have an idea.
I inserted only the essential parts.
YAML FILE:
pages:
- Page:
URL: http://www.perlmonks.org/
- Page:
URL: http://www.perl.org/
Page Class:
package Page;
our $VERSION=0.01;
use Moose;
has 'URL' => (is => 'ro', isa => 'Str',required => 1);
__PACKAGE__->meta->make_immutable;
1;
Objects container class (with ArrayRef):
package PagesGenerator;
our $VERSION=0.01;
use Moose;
with 'MooseX::SimpleConfig';
use Page;
has 'pages'=>(
is=>'rw',
isa=>'ArrayRef[Page]',
default => sub { [ ] },
);
__PACKAGE__->meta->make_immutable;
1;
And finally on main script, the object construction:
use strict;
use warnings;
our $VERSION=0.01;
use FindBin qw($Bin);
my $p=PagesGenerator->new_with_config(configfile => "$Bin/pages-temp.y
+aml");
But, while page is an
ArrayRef[Page] I have this error:
Attribute (pages) does not pass the type constraint because: Validatio
+n failed for 'ArrayRef[Page]' with value ARRAY(0x22422e8) at /usr/loc
+al/share/perl/5.10.0/MooseX/ConfigFromFile.pm line 44
MooseX::ConfigFromFile::new_with_config('PagesGenerator', 'configf
+ile', 'pages-temp.yaml') called at navigator.pl line 14
If I choose an
ArrayRef[HashRef], all works fine.
The problem is that I need exaclty an
ArrayRef[Page].
So I could coerce one type in another.
I readed something about coercion in Moose,
but I don't understand how to use it in my case.
Any suggestions is welcome.
Thank you!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.