Subject: | Moose enum triggers compile-time error when used in Kavorka signature |
i'm not really sure that this is a Moose problem per-se, but the error seems to be generated from the inlined code from Moose::Meta::TypeConstraint::Enum. unfortunately, due to the compile-time nature of the problem, i have exhausted my ability to trace it, and i am hoping for some help, or direction.
when using a Moose enum TypeConstraint in a Kavorka signature:
fun print_color(ColorEnum :$color!) {
print "$color\n";
}
the code dies with:
~$ perl -I./lib -Mstrict -Mwarnings -mMy::Shapes -e exit
Global symbol "%enums0" requires explicit package name at lib/My/Shapes.pm line 10.
Compilation failed in require.
BEGIN failed--compilation aborted.
when switching the Kavorka Signature to use a Str subtype:
fun print_color(ColorStr :$color!) {
print "$color\n";
}
the code loads and runs fine, and behaves as expected.
~$ perl -I./lib -Mstrict -Mwarnings -e 'use My::Shapes qw(); my $color = "black"; My::Shapes::print_color( color=> $color ); my $obj = My::Shapes->new( color=> $color ); print $obj->color()."\n"; '
black
black
~/$ perl -I./lib -Mstrict -Mwarnings -e 'use My::Shapes qw(); my $color = "pink"; My::Shapes::print_color( color=> $color ); my $obj = My::Shapes->new( color=> $color ); print $obj->color()."\n"; '
Value "pink" did not pass type constraint "ColorStr" at lib/My/Shapes.pm line 10
Value "pink" did not pass type constraint "ColorStr"
"ColorStr" is defined as: sub { package My::Shapes::TypeConstraints; /^(?:white|black|brown)$/; }
the error produced seems to be related to the dynamic variable names used in the code generated for the validation routines. and i'm guessing that it is triggered by a scoping issue during some kind of dynamic loading of related modules during Kavorka parsing/initialization. but all of this is a semi-educated guess, based on poking around and inserting some debugging code.
TIA
Subject: | TypeConstraints.pm |
package My::Shapes::TypeConstraints;
use strict;
use Kavorka qw();
use Moose::Util::TypeConstraints qw/ subtype as where enum /;
enum 'ColorEnum' => [qw/ white black brown /];
subtype 'ColorStr',
as 'Str',
where {
/^(?:white|black|brown)$/
};
1;