| 1 | 1 | ▶ | use 5.008008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package Marlin::X::UndefTolerant; | ||
| 6 | |||
| 7 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 8 | our $VERSION = '0.020000'; | ||
| 9 | |||
| 10 | 0 | ▶ | use Marlin::Util qw( true false ); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Types::Common qw( -types ); | ||
| 12 | |||
| 13 | use Marlin -with => 'Marlin::X'; | ||
| 14 | |||
| 15 | sub adjust_setup_steps { | ||
| 16 | my $plugin = shift; | ||
| 17 | my $steps = shift; | ||
| 18 | |||
| 19 | my $callback = __PACKAGE__ . '::setup_for_undef_tolerant'; | ||
| 20 | |||
| 21 | # Insert the 'setup_for_undef_tolerant' step directly | ||
| 22 | # after 'canonicalize_attributes'. We want the attributes | ||
| 23 | # to be in a predicatable format, but we don't want them | ||
| 24 | # to have been used for anything yet! | ||
| 25 | @$steps = map { | ||
| 26 | ( $_ eq 'canonicalize_attributes' ) | ||
| 27 | ? ( 'canonicalize_attributes', $callback ) | ||
| 28 | : ( $_ ); | ||
| 29 | } @$steps; | ||
| 30 | }; | ||
| 31 | |||
| 32 | sub setup_for_undef_tolerant { | ||
| 33 | my $plugin = shift; | ||
| 34 | my $marlin = shift; | ||
| 35 | |||
| 36 | # Loop through attributes... | ||
| 37 | for my $attr ( @{ $marlin->attributes } ) { | ||
| 38 | |||
| 39 | # Skip attributes which have it explicitly set to true or false. | ||
| 40 | next if exists $attr->{undef_tolerant}; | ||
| 41 | |||
| 42 | # Skip attributes which aren't initialized in the constructor. | ||
| 43 | next if exists($attr->{init_arg}) && !defined($attr->{init_arg}); | ||
| 44 | |||
| 45 | # Skip any required attributes! | ||
| 46 | next if $attr->{required}; | ||
| 47 | |||
| 48 | # Skip any attributes where undef is a legitimate value. | ||
| 49 | my $type = $attr->{isa}; | ||
| 50 | next if $type && $type->check(undef); | ||
| 51 | |||
| 52 | # If we got this far, default the attribute to be undef-tolerant. | ||
| 53 | $attr->{undef_tolerant} = true; | ||
| 54 | } | ||
| 55 | |||
| 56 | return $marlin; | ||
| 57 | } | ||
| 58 | |||
| 59 | __PACKAGE__ | ||
| 60 | __END__ | ||