Skip to content

Instantly share code, notes, and snippets.

@chrisa
Created January 14, 2014 13:44
Show Gist options
  • Save chrisa/8418481 to your computer and use it in GitHub Desktop.
Save chrisa/8418481 to your computer and use it in GitHub Desktop.
~/Projects/venda/app ➔ diff -u venda-siteperl/MooseX/UndefTolerant/Class.pm Class.pm
--- venda-siteperl/MooseX/UndefTolerant/Class.pm 2014-01-14 13:43:33.000000000 +0000
+++ Class.pm 2014-01-14 13:42:25.000000000 +0000
@@ -22,19 +22,22 @@
my $init_arg = $attr->init_arg;
my $type_constraint = $attr->type_constraint;
- my $tc_says_clean = ($type_constraint && !$type_constraint->check(undef) ? 1 : 0);
# FIXME: not properly sanitizing field names - e.g. consider a field name "Z'ha'dum"
- return ($tc_says_clean ? (
- "if ( exists \$params->{'$init_arg'} && defined \$params->{'$init_arg'} ) {",
- ) : (),
- @source,
- $tc_says_clean ? (
- '} else {',
- "delete \$params->{'$init_arg'};",
- '}',
- ) : (),
- );
+
+ if ($type_constraint && !$type_constraint->check(undef)) {
+ unshift @source,
+ "if ( exists \$params->{'$init_arg'} && defined \$params->{'$init_arg'} ) {";
+
+ if (my @default = $self->_inline_init_attr_from_default($attr, $idx)) {
+ push @source, '} else {', @default, '}';
+ }
+ else {
+ push @source, '} else {', "delete \$params->{'$init_arg'};", '}';
+ }
+ }
+
+ return @source;
};
no Moose::Role;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment