| 1 | #!/usr/bin/perl | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Extender - Reference-Scalar-Object method Extender. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # (C) 2024 OnEhIppY - Domero Software | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | package Extender; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = '1.01'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | our @EXPORT = qw(Extend Extends GlobExtends Alias AddMethod Decorate ApplyRole InitHook Unload); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 3 | ▶ | sub Extend { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Extend: called CC=6 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 3 | my ($object, $module, @methods) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | # Check if the module is already loaded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 3 | ▶ | unless (exists $INC{$module} || defined *{"${module}::"}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $INC{$module} or defined *{"${module}::";}
MC/DC0%0/2
exists $INC{$module} or defined *{"${module}::";}
exists $INC{$module}
defined *{"${module}::";}
Truth table33%1/3
A or B
A= exists $INC{$module}B= defined *{"${module}::";}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 0 | eval "require $module"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 0 | ▶ | return undef if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | # Get list of functions exported by the module | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | # Add each specified function (or all if none specified) as a method to the object | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | ▶ | foreach my $func ($#methods > -1 ? @methods : @{"${module}::EXPORT"}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 0 | ▶ | *{ref($object) . "::$func"} = sub { unshift @_, $object; goto &{"${module}::$func"} }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 | return $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 3 | ▶ | sub Extends { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Extends: called CC=7 SCAR=29.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 3 | my ($object, %extend) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 3 | for my $name (keys %extend) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | # Create the method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 4 | ▶ | my $package = ref($object) || $object; # Get the package or class name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
ref $object || $object
MC/DC0%0/2
ref $object || $object
ref $object
$object
Truth table33%1/3
A || B
A= ref $objectB= $object
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | 4 | ▶ | if (ref $extend{$name} eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | # If $extend{$name} is a coderef, directly assign it | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | *{$package . "::$name"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=2.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 0 | return $extend{$name}->($self, @_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | elsif (ref $extend{$name} eq 'SCALAR' && defined ${$extend{$name}} && ref ${$extend{$name}} eq 'CODE') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
ref $extend{$name} eq "SCALAR" and defined ${$extend{$name};} and ref ${$extend{$name};} eq "CODE"
ref $extend{$name} eq "SCALAR" and defined ${$extend{$name};}
MC/DC0%0/3
ref $extend{$name} eq "SCALAR" and defined ${$extend{$name};} and ref ${$extend{$name};} eq "CODE"
ref $extend{$name} eq "SCALAR"
defined ${$extend{$name};}
ref ${$extend{$name};} eq "CODE"
Truth table0%0/4
A and B and C
A= ref $extend{$name} eq "SCALAR"B= defined ${$extend{$name};}C= ref ${$extend{$name};} eq "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | # If $method_ref is a reference to a scalar containing a coderef | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | *{$package . "::$name"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 0 | return ${$extend{$name}}->($self, @_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | die "Invalid method reference provided for $name. Expected CODE or reference to CODEREF but got ".(ref($extend{$name}))."."; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 3 | return $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | sub Alias { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Alias: called CC=9 SCAR=25.3
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | my ($object, $existing_method, $new_name) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | # Check if $object is a blessed reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 1 | ▶ | die "Not a valid object reference" unless ref $object && ref $object ne 'HASH' && ref $object ne 'ARRAY' && ref $object ne 'SCALAR'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%3/9
ref $object and ref $object ne "HASH" and ref $object ne "ARRAY" and ref $object ne "SCALAR"
ref $object and ref $object ne "HASH" and ref $object ne "ARRAY"
ref $object and ref $object ne "HASH"
MC/DC0%0/4
ref $object and ref $object ne "HASH" and ref $object ne "ARRAY" and ref $object ne "SCALAR"
ref $object
ref $object ne "HASH"
ref $object ne "ARRAY"
ref $object ne "SCALAR"
Truth table20%1/5
A and B and C and D
A= ref $objectB= ref $object ne "HASH"C= ref $object ne "ARRAY"D= ref $object ne "SCALAR"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | # Validate $existing_method | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | ▶ | die "Invalid method name. Method name must be a string" unless defined $existing_method && $existing_method =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $existing_method and $existing_method =~ /^\w+$/
MC/DC0%0/2
defined $existing_method and $existing_method =~ /^\w+$/
defined $existing_method
$existing_method =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $existing_methodB= $existing_method =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | # Validate $new_name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | die "Invalid alias name. Alias name must be a string" unless defined $new_name && $new_name =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $new_name and $new_name =~ /^\w+$/
MC/DC0%0/2
defined $new_name and $new_name =~ /^\w+$/
defined $new_name
$new_name =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $new_nameB= $new_name =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | # Create the alias within the package where $object is blessed into | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | 1 | my $pkg = ref($object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 1 | *{$pkg . "::$new_name"} = \&{$pkg . "::$existing_method"}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | 1 | return $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 1 | ▶ | sub AddMethod { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
AddMethod: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 1 | my ($object, $method_name, $code_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | # Validate method name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | 1 | ▶ | die "Method name must be a string" unless defined $method_name && $method_name =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $method_name and $method_name =~ /^\w+$/
MC/DC0%0/2
defined $method_name and $method_name =~ /^\w+$/
defined $method_name
$method_name =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $method_nameB= $method_name =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | # Validate code reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 1 | ▶ | die "Code reference required" unless ref($code_ref) eq 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | 1 | *{ref($object) . "::$method_name"} = $code_ref; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 1 | return $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub Decorate { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Decorate: called CC=6 SCAR=19.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | my ($object, $method_name, $decorator) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | # Check if $object is an object or a class name | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | my $is_object = ref($object) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | # Fetch the original method reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 1 | my $original_method; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 1 | ▶ | if ($is_object) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 1 | my $coderef = $object->can($method_name); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 1 | ▶ | die "Method $method_name does not exist in the object" unless $coderef; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | 1 | $original_method = $coderef; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | $original_method = *{$object . '::' . $method_name}{CODE}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | die "Method $method_name does not exist in the package" unless defined $original_method; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | # Replace the method with a decorated version | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 1 | ▶ | if ($is_object) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 1 | my $class = ref $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 1 | ▶ | *{$class . "::$method_name"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 1 | return $decorator->($self, $original_method, @_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | ▶ | *{$object . "::$method_name"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | return $decorator->($self, $original_method, @_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 1 | return $object | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | 3 | ▶ | sub ApplyRole { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ApplyRole: called CC=10 SCAR=23.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | 3 | my ($object, $role_class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | 3 | ▶ | die "Object must be provided for role application" unless defined $object; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 3 | ▶ | die "Role class must be specified" unless defined $role_class && $role_class =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $role_class and $role_class =~ /^\w+$/
MC/DC0%0/2
defined $role_class and $role_class =~ /^\w+$/
defined $role_class
$role_class =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $role_classB= $role_class =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | # Ensure role class is loaded | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | ▶ | unless (exists $INC{$role_class} || defined *{"${role_class}::"}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $INC{$role_class} or defined *{"${role_class}::";}
MC/DC50%1/2
exists $INC{$role_class} or defined *{"${role_class}::";}
exists $INC{$role_class}
defined *{"${role_class}::";}
Truth table66%2/3
A or B
A= exists $INC{$role_class}B= defined *{"${role_class}::";}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 1 | eval "require $role_class"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 1 | ▶ | return undef if $@; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | # Apply the role's methods to the object if the apply method exists | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 2 | eval { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 2 | my $apply_method = $role_class->can('apply'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | 2 | ▶ | if ($apply_method) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 1 | $apply_method->($role_class, $object); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | 1 | die "Role $role_class does not implement apply method"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 2 | ▶ | if ($@) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | if ($@ =~ /Role $role_class does not implement apply method/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | return undef; # Return gracefully if the apply method is missing | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 0 | die "Failed to apply role $role_class to object: $@"; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | 1 | return $object | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 2 | ▶ | sub InitHook { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
InitHook: called CC=7 SCAR=20.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 2 | my ($class, $hook_name, $hook_code) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | # Validate arguments | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | 2 | ▶ | die "Class name must be specified" unless defined $class && $class =~ /^\w+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $class and $class =~ /^\w+$/
MC/DC0%0/2
defined $class and $class =~ /^\w+$/
defined $class
$class =~ /^\w+$/
Truth table33%1/3
A and B
A= defined $classB= $class =~ /^\w+$/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 2 | ▶ | die "Unsupported hook name '$hook_name'" unless $hook_name =~ /^(INIT|DESTRUCT)$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | # Initialize hooks array if not already present | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 2 | ▶ | $class->{"_${hook_name}_hooks"} ||= []; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$class{"_${hook_name}_hooks"} ||= []
MC/DC0%0/1
$$class{"_${hook_name}_hooks"} ||= []
$$class{"_${hook_name}_hooks"}
Truth table50%1/2
A
A= $$class{"_${hook_name}_hooks"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | # Register the hook code | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 2 | push @{$class->{"_${hook_name}_hooks"}}, $hook_code; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | # If INIT hook, wrap the new method to execute hooks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | 2 | ▶ | if ($hook_name eq 'INIT') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 1 | my $original_new = $class->can('new'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 1 | ▶ | *{$class . "::new"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | 2 | my $self = $original_new->(@_); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | 2 | ▶ | for my $hook (@{$class->{"_INIT_hooks"} || []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$class->{'_INIT_hooks'} || []
MC/DC0%0/1
$class->{'_INIT_hooks'} || []
$class->{'_INIT_hooks'}
Truth table50%1/2
A
A= $class->{'_INIT_hooks'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 2 | $hook->($self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 2 | return $self; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # If DESTRUCT hook, wrap the DESTROY method to execute hooks | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | elsif ($hook_name eq 'DESTRUCT') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | 1 | my $original_destroy = $class->can('DESTROY'); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | 1 | ▶ | no warnings 'redefine'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 1 | ▶ | *{$class . "::DESTROY"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | 1 | ▶ | for my $hook (@{$class->{"_DESTRUCT_hooks"} || []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$class->{'_DESTRUCT_hooks'} || []
MC/DC0%0/1
$class->{'_DESTRUCT_hooks'} || []
$class->{'_DESTRUCT_hooks'}
Truth table50%1/2
A
A= $class->{'_DESTRUCT_hooks'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 1 | $hook->($self); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | ▶ | $original_destroy->($self) if $original_destroy && ref($self); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$original_destroy and ref $self
MC/DC0%0/2
$original_destroy and ref $self
$original_destroy
ref $self
Truth table33%1/3
A and B
A= $original_destroyB= ref $self
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | return $class; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | 1 | ▶ | sub Unload { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
Unload: called CC=9 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | 1 | my ($object, @methods) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | # Check if $object is a valid reference and not a CODE reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | my $ref_type = ref $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 1 | ▶ | die "Not a valid object reference" unless $ref_type && $ref_type ne 'CODE'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ref_type and $ref_type ne "CODE"
MC/DC0%0/2
$ref_type and $ref_type ne "CODE"
$ref_type
$ref_type ne "CODE"
Truth table33%1/3
A and B
A= $ref_typeB= $ref_type ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | # Validate @methods | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 1 | ▶ | die "No methods specified for unloading" unless @methods; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | # Determine the package or type of the reference | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | 1 | my $pkg = ref $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | ▶ | if ($ref_type eq 'GLOB') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | # Use the GLOB reference directly as the package | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | $pkg = *{$object}{PACKAGE}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | 1 | ▶ | die "Cannot determine package for object reference" unless $pkg; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | 1 | foreach my $method (@methods) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 1 | ▶ | next unless defined $method; # Skip if method is undefined | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | # Check if the method exists in the package's symbol table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 1 | ▶ | if (exists ${$pkg."::"}{$method}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | # Remove the method from the package's symbol table | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | 1 | delete ${$pkg."::"}{$method}; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | 1 | return $object; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | ################################################################################ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | 1 | 1; # EOF Extender.pm (C) 2024 OnEhIppY - Domero Sofware | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||