| 1 | package Class::CompoundMethods; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use vars qw($VERSION @EXPORT_OK %EXPORT_TAGS %METHODS); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Exporter (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | *import = \&Exporter::import; | |||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | @EXPORT_OK = qw(append_method prepend_method ); | |||||||||||||||||||||||||||||||||||||||||
| 9 | 3 | %EXPORT_TAGS = ( all => \@EXPORT_OK ); | |||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | 3 | ▶ | use B qw( svref_2object ); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||||||
| 13 | # use Smart::Comments; | ||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||
| 15 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | Class::CompoundMethods - Create methods from components | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | 0.05 | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | $VERSION = '0.05'; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||
| 33 | package Object; | ||||||||||||||||||||||||||||||||||||||||||
| 34 | use Class::CompoundMethods 'append_method'; | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # This installs both versioning_hook and auditing_hook into the | ||||||||||||||||||||||||||||||||||||||||||
| 37 | # method Object::pre_insert. | ||||||||||||||||||||||||||||||||||||||||||
| 38 | append_method( pre_insert => "versioning_hook" ); | ||||||||||||||||||||||||||||||||||||||||||
| 39 | append_method( pre_insert => "auditing_hook" ); | ||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||
| 43 | This allows you to install more than one method into a single method | ||||||||||||||||||||||||||||||||||||||||||
| 44 | name. I created this so I could install both versioning and auditing | ||||||||||||||||||||||||||||||||||||||||||
| 45 | hooks into another module's object space. So instead of creating a | ||||||||||||||||||||||||||||||||||||||||||
| 46 | single larger method which incorporates the functionality of both | ||||||||||||||||||||||||||||||||||||||||||
| 47 | hooks I created C<append_method()>/C<insert_method()> to install a | ||||||||||||||||||||||||||||||||||||||||||
| 48 | wrapper method as needed. | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | If only one method is ever installed into a space, it is installed | ||||||||||||||||||||||||||||||||||||||||||
| 51 | directly with no wrapper. Once there are two or more components, a | ||||||||||||||||||||||||||||||||||||||||||
| 52 | hook method is installed which will call each component in order. | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | =head1 PUBLIC METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | =item append_method( $method_name, $method ) | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | append_method( $method_name, $method ); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||
| 62 | This function takes two parameters - a method name and the method to install. | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | C<$method_name> may be fully qualified. If not, Class::CompoundMethods | ||||||||||||||||||||||||||||||||||||||||||
| 65 | looks for your method in your current package. | ||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | append_method( 'Object::something', ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | append_method( 'something', ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||
| 70 | C<$method> may be either a code reference or a method name. It may be | ||||||||||||||||||||||||||||||||||||||||||
| 71 | fully qualified. | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | append_method( ..., sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 74 | append_method( ..., \ &some_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 75 | append_method( ..., 'Object::some_hook' ); | ||||||||||||||||||||||||||||||||||||||||||
| 76 | append_method( ..., 'some_hook' ); | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | 4 | ▶ | sub append_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
append_method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | # This takes a method and adds it onto the end of all previous methods | ||||||||||||||||||||||||||||||||||||||||||
| 83 | 4 | my ( $method_name, $method_to_install ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | 4 | return _x_method( | |||||||||||||||||||||||||||||||||||||||||
| 86 | { method_name => $method_name, | ||||||||||||||||||||||||||||||||||||||||||
| 87 | method_to_install => $method_to_install, | ||||||||||||||||||||||||||||||||||||||||||
| 88 | add_method => \&_append_method, | ||||||||||||||||||||||||||||||||||||||||||
| 89 | existing_method => \&_append_method, | ||||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =pod | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||
| 97 | =item prepend_method( $method_name, $method ) | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | prepend_method( $method_name, $method ); | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | This function takes two parameters - a method name and the method to install. | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | C<$method_name> may be fully qualified. If not, Class::CompoundMethods | ||||||||||||||||||||||||||||||||||||||||||
| 104 | looks for your method in your current package. | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | prepend_method( 'Object::something', ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | prepend_method( 'something', ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||
| 109 | C<$method> may be either a code reference or a method name. It may be | ||||||||||||||||||||||||||||||||||||||||||
| 110 | fully qualified. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | prepend_method( ..., sub { ... } ); | ||||||||||||||||||||||||||||||||||||||||||
| 113 | prepend_method( ..., \ &some_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 114 | prepend_method( ..., 'Object::some_hook' ); | ||||||||||||||||||||||||||||||||||||||||||
| 115 | prepend_method( ..., 'some_hook' ); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||
| 119 | 4 | ▶ | sub prepend_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
prepend_method: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # This takes a method and inserts before all other methods into the method | ||||||||||||||||||||||||||||||||||||||||||
| 122 | # slot. | ||||||||||||||||||||||||||||||||||||||||||
| 123 | 4 | my ( $method_name, $method_to_install ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||||||
| 125 | 4 | return _x_method( | |||||||||||||||||||||||||||||||||||||||||
| 126 | { method_name => $method_name, | ||||||||||||||||||||||||||||||||||||||||||
| 127 | method_to_install => $method_to_install, | ||||||||||||||||||||||||||||||||||||||||||
| 128 | add_method => \&_prepend_method, | ||||||||||||||||||||||||||||||||||||||||||
| 129 | existing_method => \&_prepend_method, | ||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||
| 134 | # =pod | ||||||||||||||||||||||||||||||||||||||||||
| 135 | # | ||||||||||||||||||||||||||||||||||||||||||
| 136 | # =item method_list( $method_name ) | ||||||||||||||||||||||||||||||||||||||||||
| 137 | # | ||||||||||||||||||||||||||||||||||||||||||
| 138 | # =cut | ||||||||||||||||||||||||||||||||||||||||||
| 139 | # | ||||||||||||||||||||||||||||||||||||||||||
| 140 | # sub method_list { | ||||||||||||||||||||||||||||||||||||||||||
| 141 | # | ||||||||||||||||||||||||||||||||||||||||||
| 142 | # # Modifying the $METHODS{...} array only works when the stub function is | ||||||||||||||||||||||||||||||||||||||||||
| 143 | # # installed into the method slot. I haven't documented this | ||||||||||||||||||||||||||||||||||||||||||
| 144 | # # function and you shouldn't be using it ... unless you modify | ||||||||||||||||||||||||||||||||||||||||||
| 145 | # # ->_x_method to always install the stub method in which case it | ||||||||||||||||||||||||||||||||||||||||||
| 146 | # # becomes safe to count on ->method_list. | ||||||||||||||||||||||||||||||||||||||||||
| 147 | # my ($method_name) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 148 | # | ||||||||||||||||||||||||||||||||||||||||||
| 149 | # return $METHODS{$method_name} || []; | ||||||||||||||||||||||||||||||||||||||||||
| 150 | # } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | =head2 EXAMPLES | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | =item Example 1 | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | use Class::CompoundMethods qw(append_method); | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | # This installs both versioning_hook and auditing_hook into the | ||||||||||||||||||||||||||||||||||||||||||
| 163 | # method Object::pre_insert. | ||||||||||||||||||||||||||||||||||||||||||
| 164 | append_method( 'Object::something' => \ &versioning_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | package Object; | ||||||||||||||||||||||||||||||||||||||||||
| 167 | prepend_method( 'something' => \ &auditing_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | =item Example 2 | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | package GreenPartyDB::Database; | ||||||||||||||||||||||||||||||||||||||||||
| 172 | use Class::CompoundMethods qw(append_method); | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | my @versioned_tables = ( ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 175 | my @audited_tables = ( ... ); | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | for my $table ( @versioned_tables ) { | ||||||||||||||||||||||||||||||||||||||||||
| 178 | my $package = __PACKAGE__ . "::" . $table; | ||||||||||||||||||||||||||||||||||||||||||
| 179 | append_method( $package . "::pre_insert", \ &versioning_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 180 | append_method( $package . "::pre_update", \ &versioning_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 181 | append_method( $package . "::pre_delete", \ &versioning_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | for my $table ( @audited_tables ) { | ||||||||||||||||||||||||||||||||||||||||||
| 185 | my $package = __PACKAGE__ . "::" . $table; | ||||||||||||||||||||||||||||||||||||||||||
| 186 | append_method( $package . "::pre_insert", \ &auditing_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 187 | append_method( $package . "::pre_update", \ &auditing_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 188 | append_method( $package . "::pre_delete", \ &auditing_hook ); | ||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||
| 193 | =head2 EXPORT | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | This class optionally exports the C<append_method> and | ||||||||||||||||||||||||||||||||||||||||||
| 196 | C<prepend_method> functions. It also uses the ':all' tag. | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | use Class::CompoundMethods qw( append_method ); | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | use Class::CompoundMethods qw( :all ); | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | Copyright (c) 2005 Joshua ben Jore All rights reserved. This program | ||||||||||||||||||||||||||||||||||||||||||
| 205 | is free software; you can redistribute it and/or modify it under the | ||||||||||||||||||||||||||||||||||||||||||
| 206 | same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | "Joshua ben Jore" <jjore@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | RFC Class::AppendMethods L<http://www.perlmonks.org/index.pl?node_id=252199> | ||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | Installing chained methods L<http://www.perlmonks.org/index.pl?node_id=251908> | ||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||
| 220 | ## PRIVATE FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||
| 222 | 5 | ▶ | sub _append_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_append_method: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | 5 | my ($p) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 224 | 5 | push @{ $p->{stash} }, $p->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 225 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | 5 | ▶ | sub _prepend_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_prepend_method: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 229 | 5 | my ($p) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 230 | 5 | unshift @{ $p->{stash} }, $p->{method}; | |||||||||||||||||||||||||||||||||||||||||
| 231 | 5 | return; | |||||||||||||||||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||||||||||||||||
| 234 | 16 | ▶ | sub _function_package { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_function_package: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 235 | 16 | my ($sub) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 16 | return eval { svref_2object($sub)->STASH->NAME; }; | |||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | 8 | ▶ | sub _x_method { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_x_method: called CC=13 SCAR=26.4
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||
| 242 | # This is a general function used by ->prepend_method and | ||||||||||||||||||||||||||||||||||||||||||
| 243 | # ->append_method to alter a method slot. The four arguments are | ||||||||||||||||||||||||||||||||||||||||||
| 244 | # the method name to install, the slot to write to and two | ||||||||||||||||||||||||||||||||||||||||||
| 245 | # functions for managing the $METHODS{...} array. | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # method_name: | ||||||||||||||||||||||||||||||||||||||||||
| 248 | # This may be either a fully qualified or unqualified method name. | ||||||||||||||||||||||||||||||||||||||||||
| 249 | # eg: 'GreenPartyDB::Database::person::pre_insert' | ||||||||||||||||||||||||||||||||||||||||||
| 250 | # vs | ||||||||||||||||||||||||||||||||||||||||||
| 251 | # 'pre_insert' (and the calling method was done from within the | ||||||||||||||||||||||||||||||||||||||||||
| 252 | # 'GreenPartyDB::Database::person' namespace) | ||||||||||||||||||||||||||||||||||||||||||
| 253 | # | ||||||||||||||||||||||||||||||||||||||||||
| 254 | # Perhaps in the future it would be useful to also support methods in | ||||||||||||||||||||||||||||||||||||||||||
| 255 | # the form 'package->method' /^ ([^ -]*) \s* -> \s* ([\w+])/x . | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | # method_to_install: | ||||||||||||||||||||||||||||||||||||||||||
| 258 | # This may be either an fully qualified/unqualified method name or a code | ||||||||||||||||||||||||||||||||||||||||||
| 259 | # reference. | ||||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||||
| 261 | # add_method: | ||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | # existing_method: | ||||||||||||||||||||||||||||||||||||||||||
| 264 | 8 | my ($p) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 265 | my ( $method_name, $method_to_install, $add_method, $existing_method ) | ||||||||||||||||||||||||||||||||||||||||||
| 266 | 8 | = @{$p} | |||||||||||||||||||||||||||||||||||||||||
| 267 | 8 | {qw(method_name method_to_install add_method existing_method )}; | |||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | # Get the package of the user of Class::CompoundMethods. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 8 | my ( $package, $filename, $line ); | |||||||||||||||||||||||||||||||||||||||||
| 271 | { | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | # Look upwards in the call stack until I either run out of | ||||||||||||||||||||||||||||||||||||||||||
| 274 | # stack or find something that isn't from this package. | ||||||||||||||||||||||||||||||||||||||||||
| 275 | 8 | my $cx = 1; | |||||||||||||||||||||||||||||||||||||||||
| 276 | 8 | ▶ | ++$cx until __PACKAGE__ ne caller $cx; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 277 | 8 | ( $package, $filename, $line ) = caller $cx; | |||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | ### Context: $cx | ||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | 3 | ▶ | no strict 'refs'; ## no critic | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | 8 | local $^W; | |||||||||||||||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||||||||||||||||
| 285 | # If the method name isn't qualified then I assume it exists in the | ||||||||||||||||||||||||||||||||||||||||||
| 286 | # caller's package. | ||||||||||||||||||||||||||||||||||||||||||
| 287 | 8 | ▶ | unless ( $method_name =~ /::/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | ### Fixing up target $method_name from $package | ||||||||||||||||||||||||||||||||||||||||||
| 289 | 2 | $method_name = "${package}::$method_name"; | |||||||||||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | ### Target method name: $method_name | ||||||||||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||||||||||
| 294 | # If I was given a method name then fetch the code | ||||||||||||||||||||||||||||||||||||||||||
| 295 | # reference from the named slot | ||||||||||||||||||||||||||||||||||||||||||
| 296 | 8 | ▶ | unless ( ref $method_to_install ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | # If the method is not qualified with a package name then grab the | ||||||||||||||||||||||||||||||||||||||||||
| 299 | # method from the caller's own package. | ||||||||||||||||||||||||||||||||||||||||||
| 300 | 6 | ▶ | unless ( $method_to_install =~ /::/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 301 | ### Fixing up source: $method_to_install, from: $package | ||||||||||||||||||||||||||||||||||||||||||
| 302 | 2 | $method_to_install = "${package}::$method_to_install"; | |||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||
| 305 | ### Source symref: $method_to_install | ||||||||||||||||||||||||||||||||||||||||||
| 306 | 6 | ▶ | defined &$method_to_install | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 307 | or die "Couldn't get $method_to_install in $filename at $line.\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | 6 | $method_to_install = \&$method_to_install; | |||||||||||||||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | # Track the list of references to install | ||||||||||||||||||||||||||||||||||||||||||
| 313 | 8 | ▶ | my $methods_to_call = $METHODS{$method_name} ||= []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$Class::CompoundMethods::METHODS{$method_name} ||= []
MC/DC100%1/1
$Class::CompoundMethods::METHODS{$method_name} ||= []
$Class::CompoundMethods::METHODS{$method_name}
Truth table100%2/2
A
A= $Class::CompoundMethods::METHODS{$method_name}
| |||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | # Protect against clobbering whatever was there previously. Its ok | ||||||||||||||||||||||||||||||||||||||||||
| 316 | # to clobber it if its just the hook method or if its already in | ||||||||||||||||||||||||||||||||||||||||||
| 317 | # the list of things C::CM knows to call as a component. | ||||||||||||||||||||||||||||||||||||||||||
| 318 | 8 | ▶ | if ( defined &$method_name | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
defined &$method_name and __PACKAGE__ ne _function_package(\&$method_name) and not scalar grep({$_ == \&$method_name;} @$methods_to_call)
defined &$method_name and __PACKAGE__ ne _function_package(\&$method_name)
MC/DC33%1/3
defined &$method_name and __PACKAGE__ ne _function_package(\&$method_name) and not scalar grep({$_ == \&$method_name;} @$methods_to_call)
defined &$method_name
__PACKAGE__ ne _function_package(\&$method_name)
not scalar grep({$_ == \&$method_name;} @$methods_to_call)
Truth table50%2/4
A and B and not(C)
A= defined &$method_nameB= __PACKAGE__ ne _function_package(\&$method_name)C= not scalar grep({$_ == \&$method_name;} @$methods_to_call)
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | and ( __PACKAGE__ ne _function_package( \&$method_name ) ) | ||||||||||||||||||||||||||||||||||||||||||
| 320 | 0 | and not scalar grep { $_ == \&$method_name } @$methods_to_call ) | |||||||||||||||||||||||||||||||||||||||||
| 321 | { | ||||||||||||||||||||||||||||||||||||||||||
| 322 | ### Saving original method | ||||||||||||||||||||||||||||||||||||||||||
| 323 | 2 | $existing_method->( | |||||||||||||||||||||||||||||||||||||||||
| 324 | { stash => $methods_to_call, | ||||||||||||||||||||||||||||||||||||||||||
| 325 | method => \&$method_name, | ||||||||||||||||||||||||||||||||||||||||||
| 326 | package => $package, | ||||||||||||||||||||||||||||||||||||||||||
| 327 | filename => $filename, | ||||||||||||||||||||||||||||||||||||||||||
| 328 | line => $line | ||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | ### Saving original method | ||||||||||||||||||||||||||||||||||||||||||
| 334 | 8 | $add_method->( | |||||||||||||||||||||||||||||||||||||||||
| 335 | { stash => $methods_to_call, | ||||||||||||||||||||||||||||||||||||||||||
| 336 | method => $method_to_install, | ||||||||||||||||||||||||||||||||||||||||||
| 337 | package => $package, | ||||||||||||||||||||||||||||||||||||||||||
| 338 | filename => $filename, | ||||||||||||||||||||||||||||||||||||||||||
| 339 | line => $line | ||||||||||||||||||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||||||||||||||||
| 341 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | # Install the hook if there isn't one there aleady. | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 8 | ▶ | if ( __PACKAGE__ eq _function_package( \&$method_name ) ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||||||||||||||||
| 346 | ### Ignoring pre-existing multi-method hook. | ||||||||||||||||||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||||||||||||||||
| 348 | elsif ( 1 == @$methods_to_call ) { | ||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||
| 350 | ### Installing the single method. | ||||||||||||||||||||||||||||||||||||||||||
| 351 | 0 | *$method_name = $methods_to_call->[0]; | |||||||||||||||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||||||||||||||||
| 353 | elsif ( 1 < @$methods_to_call ) { | ||||||||||||||||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||||||||||||||||
| 355 | ### Installing the multi-method hook. | ||||||||||||||||||||||||||||||||||||||||||
| 356 | 8 | ▶ | *$method_name = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 357 | 8 | my ($self) = shift; | |||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||
| 359 | 8 | ▶ | if (wantarray) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 360 | 0 | return map $self->$_(@_), @$methods_to_call; | |||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||
| 362 | elsif ( defined wantarray ) { | ||||||||||||||||||||||||||||||||||||||||||
| 363 | 0 | return join( ' ', map $_->$_(@_), @$methods_to_call ); | |||||||||||||||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||||||||||||||||
| 365 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 366 | 8 | $self->$_(@_) for @$methods_to_call; | |||||||||||||||||||||||||||||||||||||||||
| 367 | 8 | return; | |||||||||||||||||||||||||||||||||||||||||
| 368 | } | ||||||||||||||||||||||||||||||||||||||||||
| 369 | 2 | }; | |||||||||||||||||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | # Return the method as a convenience (for who knows what, I don't know) | ||||||||||||||||||||||||||||||||||||||||||
| 373 | 8 | return \&{$method_name}; | |||||||||||||||||||||||||||||||||||||||||
| 374 | } | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | 3 | "Fine! Since you're too busy playing with people's minds, I'll just go off to the other room to play with myself!"; | |||||||||||||||||||||||||||||||||||||||||