Subject: | coercing arrays doesn't appear to work |
The following is a script that should work, at the bottom is the command that doesn't. In a
nut shell I have an array of integers, and I want to convert that array of integers into an array
of strings. Coercion works correctly, but then ultimately returns undef.
use strict;
use 5.010;
use MooseX::Types -declare => [qw(
ArrayRefOfInt
ArrayRefOfIntAsStr
IntAsStr
)];
use MooseX::Types::Moose qw/Int ArrayRef Str/;
use Data::Dumper;
subtype ArrayRefOfInt,
as ArrayRef[Int];
subtype ArrayRefOfIntAsStr,
as ArrayRef[IntAsStr];
subtype IntAsStr,
as Str,
where { $_ =~ m/^\d{15}$/ },
message { 'Not a valid int string.' };
coerce IntAsStr,
from Int, via { sprintf("%015d", ($_ + 1000000000)) };
coerce ArrayRefOfIntAsStr,
from ArrayRefOfInt, via {
my $array_ref_of_int = shift;
my @array_of_int_as_str = map { to_IntAsStr($_) } @{$array_ref_of_int};
return \@array_of_int_as_str;
};
my @some_array_of_int = qw(1 -2 3 4);
say Dumper(to_ArrayRefOfIntAsStr( \@some_array_of_int )); # makes it through coercion, but
returns undef