| 1 | package Method::Delegation; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Sub::Install; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | our @EXPORT = qw(delegate); ## no critic | |||||||||||||||||||||||||||
| 10 | 2 | our $VERSION = '0.03'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 12 | ▶ | sub delegate { | ||||||||||||||||||||||||||
|
Subroutine
delegate: called CC=16 SCAR=29.5
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 12 | my %arg_for = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 12 | my ( $package, undef, undef ) = caller; | |||||||||||||||||||||||||||
| 16 | 12 | my $delegate = delete $arg_for{to}; | |||||||||||||||||||||||||||
| 17 | 12 | my $methods = delete $arg_for{methods}; | |||||||||||||||||||||||||||
| 18 | 12 | my $args = delete $arg_for{args}; | |||||||||||||||||||||||||||
| 19 | 12 | my $if_true = delete $arg_for{if_true}; | |||||||||||||||||||||||||||
| 20 | 12 | my $else_return = delete $arg_for{else_return}; | |||||||||||||||||||||||||||
| 21 | 12 | my $override = delete $arg_for{override}; | |||||||||||||||||||||||||||
| 22 | 12 | my $maybe_to = delete $arg_for{maybe_to}; | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 12 | ▶ | if ( keys %arg_for ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 25 | 0 | my $unknown = join ', ' => sort keys %arg_for; | |||||||||||||||||||||||||||
| 26 | 0 | croak("Unknown keys supplied to delegate(): $unknown"); | |||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 12 | ▶ | if ($maybe_to) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 5 | ▶ | if ($delegate) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | 1 | croak( | |||||||||||||||||||||||||||
| 32 | "You supplied both 'maybe_to' and 'to'. I don't know which to use."); | ||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | 4 | ▶ | if ($if_true) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 0 | croak( | |||||||||||||||||||||||||||
| 36 | "You supplied both 'maybe_to' and 'if_true'. I don't know which to use." | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | 4 | ( $delegate, $if_true ) = ($maybe_to) x 2; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 11 | ▶ | $delegate or croak("You must supply a 'to' argument to delegate()"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 10 | ▶ | $methods or croak("You must supply a 'methods' argument to delegate()"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 9 | $methods = _normalize_methods( $methods, $delegate ); | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 8 | ▶ | if ( defined $else_return && !defined $if_true ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $else_return and not defined $if_true
MC/DC0%0/2
defined $else_return and not defined $if_true
defined $else_return
not defined $if_true
Truth table66%2/3
A and not(B)
A= defined $else_returnB= not defined $if_true
| |||||||||||||||||||||||||||||
| 46 | 0 | croak( | |||||||||||||||||||||||||||
| 47 | "You must supply a 'if_true' argument if 'else_return' is defined"); | ||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 8 | _assert_valid_method_name($delegate); | |||||||||||||||||||||||||||
| 51 | 8 | ▶ | _assert_valid_method_name($if_true) if defined $if_true; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 8 | ▶ | while ( my ( $method, $to ) = each %$methods ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 54 | 10 | _assert_valid_method_name($method); | |||||||||||||||||||||||||||
| 55 | 9 | _assert_valid_method_name($to); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 9 | my $coderef; | |||||||||||||||||||||||||||
| 58 | 9 | ▶ | if ($if_true) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 7 | ▶ | if ($args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 0 | ▶ | $coderef = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 61 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 0 | ▶ | if ( $self->$if_true ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 0 | return $self->$delegate->$to(@_); | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return defined $else_return ? $else_return : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 0 | }; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | else { | ||||||||||||||||||||||||||||
| 69 | 5 | ▶ | $coderef = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 70 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 5 | ▶ | if ( $self->$if_true ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 2 | return $self->$delegate->$to; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 3 | ▶ | return defined $else_return ? $else_return : (); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 7 | }; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | else { | ||||||||||||||||||||||||||||
| 79 | 2 | ▶ | if ($args) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 1 | ▶ | $coderef = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 82 | 1 | return $self->$delegate->$to(@_); | |||||||||||||||||||||||||||
| 83 | 1 | }; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||
| 86 | 1 | ▶ | $coderef = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 1 | return $self->$delegate->$to; | |||||||||||||||||||||||||||
| 89 | 1 | }; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | { | ||||||||||||||||||||||||||||
| 94 | 2 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | 9 | ▶ | if ( !$override && defined *{"${package}::$method"}{CODE} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $override and defined *{"${package}::$method";}{"CODE"}
MC/DC0%0/2
not $override and defined *{"${package}::$method";}{"CODE"}
not $override
defined *{"${package}::$method";}{"CODE"}
Truth table33%1/3
not(A) and B
A= not $overrideB= defined *{"${package}::$method";}{"CODE"}
| |||||||||||||||||||||||||||||
| 96 | 0 | croak( | |||||||||||||||||||||||||||
| 97 | "Package '$package' already has a method named '$method'"); | ||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 9 | _install_delegate( $coderef, $package, $method ); | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 9 | ▶ | sub _normalize_methods { | ||||||||||||||||||||||||||
|
Subroutine
_normalize_methods: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 106 | 9 | my ( $methods, $delegate ) = @_; | |||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 9 | ▶ | if ( 'ARRAY' eq ref $methods ) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 109 | 2 | $methods = { map { $_ => $_ } @$methods }; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | elsif ( !ref $methods ) { | ||||||||||||||||||||||||||||
| 112 | 3 | $methods = { $methods => $methods }; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | elsif ( 'HASH' ne ref $methods ) { | ||||||||||||||||||||||||||||
| 115 | 1 | croak("I don't know how to delegate to '$delegate' from '$methods'"); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 8 | ▶ | unless ( keys %$methods ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | croak("You have not provideed any methods to delegate"); | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 8 | return $methods; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 9 | ▶ | sub _install_delegate { | ||||||||||||||||||||||||||
|
Subroutine
_install_delegate: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 125 | 9 | my ( $coderef, $package, $method ) = @_; | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 9 | Sub::Install::install_sub( | |||||||||||||||||||||||||||
| 128 | { | ||||||||||||||||||||||||||||
| 129 | code => $coderef, | ||||||||||||||||||||||||||||
| 130 | into => $package, | ||||||||||||||||||||||||||||
| 131 | as => $method, | ||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | ); | ||||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | 32 | ▶ | sub _assert_valid_method_name { | ||||||||||||||||||||||||||
|
Subroutine
_assert_valid_method_name: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 137 | 32 | my $name = shift; | |||||||||||||||||||||||||||
| 138 | 32 | ▶ | if ( $name =~ /^[a-z_][a-z0-9_]*$/i ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 139 | 31 | return $name; | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 1 | croak("Illegal method name: '$name'"); | |||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 2 | 1; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||