Skip to content

Instantly share code, notes, and snippets.

@JaHIY
Last active September 16, 2015 13:53
Show Gist options
  • Save JaHIY/a7251ada625a517e396e to your computer and use it in GitHub Desktop.
Save JaHIY/a7251ada625a517e396e to your computer and use it in GitHub Desktop.
wrap compile function in Type::Params
#!/usr/bin/env perl
use strict;
use warnings;
use utf8;
use 5.010;
use Data::Dumper;
use Type::Params qw(compile);
use Types::Standard -types;
sub _compile {
my $spec_ref = shift;
my @spec_keys = keys %{$spec_ref};
my $check = compile(@{$spec_ref}{@spec_keys});
return sub {
my $params_ref = shift;
my @params_keys = keys %{$params_ref};
my %temp;
@temp{@spec_keys} = $check->(@{$params_ref}{@spec_keys});
my %params;
my @intersect = grep { defined } @{ { map { $_ => $_ } @spec_keys } }{ @params_keys };
@params{@intersect} = @temp{@intersect};
return %params;
};
}
sub main {
my $input = shift;
state $check = _compile({
a => Str,
b => Int,
d => Maybe[Str],
});
my %params = $check->($input);
print Dumper \%params;
}
main({
a => 1,
b => 2,
c => 3,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment