Skip Menu |

This queue is for tickets about the MooseX-Types CPAN distribution.

Report information
The Basics
Id: 54957
Status: resolved
Priority: 0/
Queue: MooseX-Types

People
Owner: Nobody in particular
Requestors: RIZEN [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: Important
Broken in: 0.21
Fixed in: (no value)



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
I've uploaded the script as an attachment so it retains all of it's formatting.
Subject: array_coercion.pl
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}; \@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
One more note. The following works: say Dumper(ArrayRefOfIntAsStr->coerce( \@some_array_of_int )); # but this one works Even though this doesn't: say Dumper(to_ArrayRefOfIntAsStr( \@some_array_of_int )); # makes it through coercion, but returns undef
Subject: Re: [rt.cpan.org #54957] coercing arrays doesn't appear to work
Date: Wed, 24 Feb 2010 17:54:45 -0500
To: JT Smith via RT <bug-MooseX-Types [...] rt.cpan.org>
From: Rafael Kitover <rkitover [...] io.com>
Coercions work on $_ not $_[0], so: coerce ArrayRefOfIntAsStr, from ArrayRefOfInt, via { [ map to_IntAsStr($_), @$_ ] }; On Wed, Feb 24, 2010 at 05:47:51PM -0500, JT Smith via RT wrote: Show quoted text
> Queue: MooseX-Types > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=54957 > > > I've uploaded the script as an attachment so it retains all of it's formatting.
Show quoted text
> 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}; > \@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 >
Turns out this was my fault. I was defining IntAsStr after I defined ArrayRef[IntAsStr]. Flipping those around solved the problem.