File: Class.pm

package info (click to toggle)
libmoosex-undeftolerant-perl 0.21-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 348 kB
  • sloc: perl: 493; sh: 6; makefile: 5
file content (40 lines) | stat: -rw-r--r-- 1,014 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package MooseX::UndefTolerant::Class;

our $VERSION = '0.21';

# applied to metaclass, for Moose >= 1.9900

use strict;
use warnings;

use Moose::Role;

# TODO: this code should be in the attribute trait, in the inlined version of
# initialize_instance_slot, but this does not yet exist!

around _inline_init_attr_from_constructor => sub {
    my $orig = shift;
    my $self = shift;
    my ($attr, $idx) = @_;

    my @source = $self->$orig(@_);

    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'};",
        '}',
        ) : (),
    );
};

no Moose::Role;
1;