Commits

Toby Inkster  committed 24c4e08

fix implementations for some facets

  • Participants
  • Parent commits 49a9cce

Comments (0)

Files changed (1)

File lib/Types/XSD.pm

 	return undef;
 }
 
+our @patterns; my $pattern_i = -1;
 my %facets = (
 	length => sub {
 		my ($o, $var) = @_;
 	pattern => sub {
 		my ($o, $var) = @_;
 		return unless exists $o->{pattern};
-		my $p = delete $o->{pattern};
-		$p =~ s/^"/"\^/;
-		$p =~ s/"$/\$"/;
-		sprintf('%s =~ m%ssm', $var, $p);
+		$patterns[++$pattern_i] = delete $o->{pattern};
+		sprintf('%s =~ $Types::XSD::patterns[%d]', $var, $pattern_i);
 	},
 	enumeration => sub {
 		my ($o, $var) = @_;
 	totalDigits => sub {
 		my ($o, $var) = @_;
 		return unless exists $o->{totalDigits};
-		sprintf('do { my $tmp = %s; ($tmp=~tr/0-9//) <= %d }', $var, delete $o->{totalDigits});
+		sprintf('do { no warnings "uninitialized"; my $tmp = %s; ($tmp=~tr/0-9//) <= %d }', $var, delete $o->{totalDigits});
 	},
 	fractionDigits => sub {
 		my ($o, $var) = @_;
 		return unless exists $o->{fractionDigits};
-		sprintf('do { my (undef, $tmp) = split /\\./, %s; ($tmp=~tr/0-9//) <= %d }', $var, delete $o->{fractionDigits});
+		sprintf('do { no warnings "uninitialized"; my (undef, $tmp) = split /\\./, %s; ($tmp=~tr/0-9//) <= %d }', $var, delete $o->{fractionDigits});
 	},
 );
 
 	
 	$self->{inline_generator} = $inline_generator;
 	$self->{constraint_generator} = sub {
-		eval sprintf(
+		my $sub = sprintf(
 			'sub { %s }',
 			$inline_generator->(@_)->($self, '$_[0]'),
 		);
+		eval($sub) or croak "could not build sub: $@\n\nCODE: $sub\n";
 	};
 #	$self->{name_generator} = sub {
 #		my ($s, %a) = @_;
 		"@code";
 	};
 	
-	my $type = declare(
-		$name,
+	my $type = "Type::Tiny"->new(
+		name       => $name,
+		library    => __PACKAGE__,
 		constraint => eval sprintf('sub { %s }', $inlined->(undef, '$_')),
 		inlined    => $inlined,
 	);
+	__PACKAGE__->add_type($type);
+	
 	facet(
 		qw( pattern whiteSpace enumeration maxInclusiveDT maxExclusiveDT minInclusiveDT minExclusiveDT ),
 		$type,