| 1 | package Class::Multi; | ||
| 2 | |||
| 3 | # | ||
| 4 | # Copyright (C) 2005,2014,2017 by Kevin Cody-Little <kcody@cpan.org> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This software may be modified and/or redistributed under the | ||
| 8 | # terms of the GNU Lesser General Public License v2.1 or later. | ||
| 9 | # | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | Class::Multi - Multiple inheritance support functions. | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | =over | ||
| 18 | |||
| 19 | =item * A flexible inheritance traversal function. | ||
| 20 | |||
| 21 | =item * A calling-class-relative metaphor of C<can()>. | ||
| 22 | |||
| 23 | =item * A calling-class-relative metaphor of C<SUPER::>. | ||
| 24 | |||
| 25 | =back | ||
| 26 | |||
| 27 | =cut | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use warnings; | ||
| 31 | |||
| 32 | use Carp; | ||
| 33 | use Exporter; | ||
| 34 | |||
| 35 | # old syntax for PERL 5.004 compat | ||
| 36 | use vars qw( $VERSION @ISA @EXPORT_OK ); | ||
| 37 | |||
| 38 | $VERSION = '1.04'; | ||
| 39 | @ISA = qw( Exporter ); | ||
| 40 | @EXPORT_OK = qw( &walk &walk_width &walk_depth &other &otherpkg ); | ||
| 41 | |||
| 42 | |||
| 43 | =head1 Inheritance Traversal Function | ||
| 44 | |||
| 45 | =head2 C<< walk_depth( \&testsub, CLASS, @avoid ) >> | ||
| 46 | |||
| 47 | =head2 C<< walk_width( \&testsub, CLASS, @avoid ) >> | ||
| 48 | |||
| 49 | =head2 C<< walk_width_up( \&testsub, CLASS, @avoid ) >> | ||
| 50 | |||
| 51 | Executes the supplied code reference once for each superclass of the supplied | ||
| 52 | derived class, until the code reference returns true. | ||
| 53 | |||
| 54 | walk_depth uses the same depth-first search order that Perl uses internally. | ||
| 55 | |||
| 56 | walk_width uses a breadth-first search order that is more appropriate for | ||
| 57 | diamond inheritance situations. Thus, and also since Perl already provides | ||
| 58 | methods that use depth-first, all of the other functions in this package | ||
| 59 | use the breadth-first search order. | ||
| 60 | |||
| 61 | walk_width_up uses breadth-first, but starts with the base class and works | ||
| 62 | its way toward the derived. This is more appropriate for a constructive | ||
| 63 | purpose where the base class should initialize first, where walk_width | ||
| 64 | is more appropriate for a destructive purpose where the base class | ||
| 65 | should be called last. | ||
| 66 | |||
| 67 | If an @avoid list is supplied, the code reference will not be executed until | ||
| 68 | all classes in that list have been seen, whichever direction it's going. | ||
| 69 | |||
| 70 | =head2 C<< walk_width { BLOCK } $derived >> | ||
| 71 | |||
| 72 | Executes the { BLOCK } once each for $derived and its superclasses. | ||
| 73 | |||
| 74 | =head2 C<< walk_width { BLOCK } $derived, $derived >> | ||
| 75 | |||
| 76 | Executes the { BLOCK } only for $derived's superclasses. | ||
| 77 | |||
| 78 | =head2 C<< walk_width { BLOCK } $derived, __PACKAGE__ >> | ||
| 79 | |||
| 80 | Executes the { BLOCK } only for classes that are inherited after the | ||
| 81 | class in which the expression is found. | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | my $walk_raw = sub { | ||
| 86 | my $width = shift; | ||
| 87 | my $callout = shift; | ||
| 88 | my $derived = shift; | ||
| 89 | my %looking = map { $_ => 1 } @_; | ||
| 90 | |||
| 91 | # prototyping will catch PEBKACs involving $callout | ||
| 92 | return unless defined $derived && length( $derived ); | ||
| 93 | |||
| 94 | # the class search is breadth-first, by fifo queue | ||
| 95 | my @queue = ( $derived ); | ||
| 96 | |||
| 97 | my ( %trail, $class, $rc ); | ||
| 98 | while ( $class = shift @queue ) { | ||
| 99 | next unless defined( $class ) && length( $class ); | ||
| 100 | |||
| 101 | # canonize main:: fudgery | ||
| 102 | substr( $class, 0, 2 ) = 'main::' | ||
| 103 | if substr( $class, 0, 2 ) eq '::'; | ||
| 104 | |||
| 105 | # push $class's supers to queue | ||
| 106 | { | ||
| 107 | no strict 'refs'; # access symbol table | ||
| 108 | |||
| 109 | if ( $width ) { | ||
| 110 | push @queue, @{$class.'::ISA'}; | ||
| 111 | } | ||
| 112 | |||
| 113 | else { | ||
| 114 | unshift @queue, @{$class.'::ISA'}; | ||
| 115 | } | ||
| 116 | |||
| 117 | } | ||
| 118 | |||
| 119 | # skip it if we've seen it | ||
| 120 | # visit each class only once - "web" inheritance | ||
| 121 | next if exists $trail{$class}; | ||
| 122 | $trail{$class}++; | ||
| 123 | |||
| 124 | # found a class in the avoidance list | ||
| 125 | if ( exists $looking{$class} ) { | ||
| 126 | delete $looking{$class}; | ||
| 127 | next; | ||
| 128 | } | ||
| 129 | |||
| 130 | # the avoidance list isn't empty, do not execute | ||
| 131 | next if %looking; | ||
| 132 | |||
| 133 | # call the given code reference | ||
| 134 | { | ||
| 135 | local $_ = $class; | ||
| 136 | $rc = &$callout( $class ); | ||
| 137 | } | ||
| 138 | |||
| 139 | # if something nonzero was returned, the loop is done | ||
| 140 | return $rc if $rc; | ||
| 141 | |||
| 142 | } | ||
| 143 | |||
| 144 | return undef; | ||
| 145 | }; | ||
| 146 | |||
| 147 | sub walk_width(&$;@) { | ||
| 148 | &$walk_raw( 1, @_ ); | ||
| 149 | } | ||
| 150 | |||
| 151 | sub walk_depth(&$;@) { | ||
| 152 | &$walk_raw( 0, @_ ); | ||
| 153 | } | ||
| 154 | |||
| 155 | sub walk(&$;@) { | ||
| 156 | confess( "Class::Multi::walk is deprecated. Use walk_width or walk_depth instead.\n" ); | ||
| 157 | &$walk_raw( 0, @_ ); | ||
| 158 | } | ||
| 159 | |||
| 160 | sub walk_width_up(&$;@) { | ||
| 161 | my ( $callout, $derived, @avoid ) = @_; | ||
| 162 | |||
| 163 | my @classes; | ||
| 164 | walk_width { push @classes, $_; 0 } $derived, @avoid; | ||
| 165 | |||
| 166 | my $rc; | ||
| 167 | while ( my $class = pop @classes ) { | ||
| 168 | |||
| 169 | # call the given code reference | ||
| 170 | local $_ = $class; | ||
| 171 | $rc = &$callout( $class ); | ||
| 172 | |||
| 173 | # if something nonzero was returned, the loop is done | ||
| 174 | return $rc if $rc; | ||
| 175 | } | ||
| 176 | |||
| 177 | return; | ||
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | =head1 Multi-Inherited Method Search | ||
| 182 | |||
| 183 | =head2 C<< other( $this, METHOD ) >> | ||
| 184 | |||
| 185 | C<other> checks if the object or class $this has a method called 'METHOD', | ||
| 186 | that occurs -AFTER- the calling class in the inheritance tree. | ||
| 187 | |||
| 188 | Usage and semantics are otherwise identical to C<UNIVERSAL::can> | ||
| 189 | |||
| 190 | The calling class is inferred via C<caller()>. | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | sub other($$) { | ||
| 195 | my ( $this, $name ) = @_; | ||
| 196 | my ( $origin, $caller ); | ||
| 197 | |||
| 198 | # a valid class or instance must be supplied | ||
| 199 | $origin = ref( $this ) || $this or return; | ||
| 200 | |||
| 201 | # we must be called from code that has a package reference | ||
| 202 | $caller = caller() or return; | ||
| 203 | |||
| 204 | no strict 'refs'; | ||
| 205 | |||
| 206 | # symbol table lookup would be undef if the method doesn't exist | ||
| 207 | return walk_width { | ||
| 208 | *{$_.'::'.$name}{CODE}; | ||
| 209 | } $origin, $caller; | ||
| 210 | } | ||
| 211 | |||
| 212 | =head2 C<< otherpkg( $this, METHOD ) >> | ||
| 213 | |||
| 214 | Identical to C<other>, except the package name is returned instead of | ||
| 215 | the desired method's code reference. | ||
| 216 | |||
| 217 | =cut | ||
| 218 | |||
| 219 | sub otherpkg($$) { | ||
| 220 | my ( $this, $name ) = @_; | ||
| 221 | my ( $origin, $caller ); | ||
| 222 | |||
| 223 | # a valid class or instance must be supplied | ||
| 224 | $origin = ref( $this ) || $this or return; | ||
| 225 | |||
| 226 | # we must be called from code that has a package reference | ||
| 227 | $caller = caller() or return; | ||
| 228 | |||
| 229 | no strict 'refs'; | ||
| 230 | |||
| 231 | # symbol table lookup would be undef if the method doesn't exist | ||
| 232 | return walk_width { | ||
| 233 | ( *{$_.'::'.$name}{CODE} ) ? $_ : undef; | ||
| 234 | } $origin, $caller; | ||
| 235 | } | ||
| 236 | |||
| 237 | =head2 C<< otherrun( $this, METHOD, @myargs ) >> | ||
| 238 | |||
| 239 | Identical to C<other>, except the function is run and its result returned | ||
| 240 | instead of the desired method's code reference. | ||
| 241 | |||
| 242 | Equivalent to C<< &{other( $this, METHOD )}( $this, @myargs ); >>. | ||
| 243 | |||
| 244 | =cut | ||
| 245 | |||
| 246 | sub otherrun($$) { | ||
| 247 | my $this = shift; | ||
| 248 | my $name = shift; | ||
| 249 | my ( $origin, $caller, $func ); | ||
| 250 | |||
| 251 | # a valid class or instance must be supplied | ||
| 252 | $origin = ref( $this ) || $this or return; | ||
| 253 | |||
| 254 | # we must be called from code that has a package reference | ||
| 255 | $caller = caller() or return; | ||
| 256 | |||
| 257 | # symbol table lookup would be undef if the method doesn't exist | ||
| 258 | { no strict 'refs'; | ||
| 259 | |||
| 260 | $func = walk_width { *{$_.'::'.$name}{CODE}; } $origin, $caller; | ||
| 261 | } | ||
| 262 | |||
| 263 | return $func ? &$func( $this, @_ ) : undef; | ||
| 264 | } | ||
| 265 | |||
| 266 | |||
| 267 | package OTHER; | ||
| 268 | use warnings; | ||
| 269 | use strict; | ||
| 270 | |||
| 271 | =head1 Multi-Inherited Breadth-First Chain-Up Call | ||
| 272 | |||
| 273 | =head2 C<< $this->OTHER::mymethod( @myargs ); >> | ||
| 274 | =head2 C<< $this->OTHER::MAY::mymethod( @myargs ); >> | ||
| 275 | =head2 C<< $this->OTHER::MAY::UP::mymethod( @myargs ); >> | ||
| 276 | =head2 C<< $this->OTHER::UP::mymethod( @myargs ); >> | ||
| 277 | |||
| 278 | Syntactic sugar. | ||
| 279 | |||
| 280 | Equivalent to C<< &{other( $this, 'mymethod' )}( $this, @myargs ); >>. | ||
| 281 | |||
| 282 | Like C<SUPER::>, C<OTHER::> expects the requested method to exist. | ||
| 283 | If it does not, an exception is thrown. | ||
| 284 | |||
| 285 | See next section for an explanation of the flags. When OTHER | ||
| 286 | is used without ALL, the behavior of HERE is implied. | ||
| 287 | |||
| 288 | =cut | ||
| 289 | |||
| 290 | =head1 Multi-Inherited Iterative Method Call | ||
| 291 | |||
| 292 | =head2 C<< $this->OTHER::ALL::mymethod( @myargs ); >> | ||
| 293 | =head2 C<< $this->OTHER::ALL::MAY::mymethod( @myargs ); >> | ||
| 294 | =head2 C<< $this->OTHER::ALL::MAY::UP::mymethod( @myargs ); >> | ||
| 295 | =head2 C<< $this->OTHER::ALL::UP::mymethod( @myargs ); >> | ||
| 296 | |||
| 297 | =head2 C<< $this->OTHER::ALL::HERE::mymethod( @myargs ); >> | ||
| 298 | =head2 C<< $this->OTHER::ALL::HERE::MAY::mymethod( @myargs ); >> | ||
| 299 | =head2 C<< $this->OTHER::ALL::HERE::MAY::UP::mymethod( @myargs ); >> | ||
| 300 | =head2 C<< $this->OTHER::ALL::HERE::UP::mymethod( @myargs ); >> | ||
| 301 | |||
| 302 | Syntactic sugar. | ||
| 303 | |||
| 304 | Calls every implementation of the requested method in all of | ||
| 305 | $this's base classes. The method must exist at least once. | ||
| 306 | |||
| 307 | If you do not want an exception to be thrown if no method is | ||
| 308 | found, use the MAY modifier. | ||
| 309 | |||
| 310 | If you want the subclasses to be called from the base to | ||
| 311 | the derived, rather than the normal derived-to-base order, | ||
| 312 | use the UP modifier. | ||
| 313 | |||
| 314 | If you want the subclasses to be searched starting from the | ||
| 315 | one that made the call, rather than $this (or $this's deepest | ||
| 316 | base class, if UP is also given), use the HERE modifier. | ||
| 317 | |||
| 318 | If you do not want to provide an empty method in your base | ||
| 319 | class to satisfy that, and don't like the ::MAY syntax, then | ||
| 320 | you can wrap an OTHER::ALL invocation in an eval { } call. But, | ||
| 321 | remember to localize @_ before you do so. | ||
| 322 | |||
| 323 | =cut | ||
| 324 | |||
| 325 | use Carp; | ||
| 326 | |||
| 327 | use vars qw( $AUTOLOAD ); | ||
| 328 | |||
| 329 | sub AUTOLOAD { | ||
| 330 | my $from = caller(); | ||
| 331 | my $auto = $AUTOLOAD; | ||
| 332 | my $this = shift; | ||
| 333 | my @args = @_; | ||
| 334 | |||
| 335 | # a valid class or instance must be supplied | ||
| 336 | my $origin = ref( $this ) || $this or return; | ||
| 337 | |||
| 338 | # find our name and intent | ||
| 339 | my @part = split /::/, $auto; | ||
| 340 | my $name = pop @part; | ||
| 341 | shift @part; | ||
| 342 | |||
| 343 | my $down = 1; | ||
| 344 | my $once = 1; | ||
| 345 | my $here = 1; | ||
| 346 | my $must = 1; | ||
| 347 | |||
| 348 | while ( my $part = shift @part ) { | ||
| 349 | |||
| 350 | if ( $part eq 'MAY' ) { | ||
| 351 | $must = 0; | ||
| 352 | } | ||
| 353 | |||
| 354 | elsif ( $part eq 'ALL' ) { | ||
| 355 | $once = 0; | ||
| 356 | $here = 0; | ||
| 357 | } | ||
| 358 | |||
| 359 | elsif ( $part eq 'HERE' ) { | ||
| 360 | $here = 1; | ||
| 361 | } | ||
| 362 | |||
| 363 | elsif ( $part eq 'UP' ) { | ||
| 364 | $down = 0; | ||
| 365 | } | ||
| 366 | |||
| 367 | else { | ||
| 368 | confess( "Unknown OTHER:: flag '$part'" ); | ||
| 369 | } | ||
| 370 | |||
| 371 | } | ||
| 372 | |||
| 373 | if ( $here and not $from ) { | ||
| 374 | # we must be called from code that has a package reference | ||
| 375 | return; # FIXME squawk about this... | ||
| 376 | } | ||
| 377 | |||
| 378 | my @walk = $here ? ( $from ) : (); | ||
| 379 | |||
| 380 | my @class; | ||
| 381 | |||
| 382 | Class::Multi::walk_width { push @class, $_; 0 } $origin, @walk; | ||
| 383 | |||
| 384 | if ( not $down ) { | ||
| 385 | @class = reverse @class; | ||
| 386 | } | ||
| 387 | |||
| 388 | my $count = 0; | ||
| 389 | |||
| 390 | while ( my $class = shift @class ) { | ||
| 391 | |||
| 392 | my $func; | ||
| 393 | |||
| 394 | { | ||
| 395 | no strict 'refs'; | ||
| 396 | $func = *{"$class\::$name"}{CODE}; | ||
| 397 | } | ||
| 398 | |||
| 399 | next unless $func; | ||
| 400 | |||
| 401 | if ( $once ) { | ||
| 402 | return &$func( $this, @args ); | ||
| 403 | } else { | ||
| 404 | &$func( $this, @args ); | ||
| 405 | } | ||
| 406 | |||
| 407 | $count++; | ||
| 408 | |||
| 409 | } | ||
| 410 | |||
| 411 | if ( $must and not $count ) { | ||
| 412 | if ( $from ) { | ||
| 413 | confess( "No method '$name' after '$from' in '$origin'.\n" ); | ||
| 414 | } else { | ||
| 415 | confess( "No method '$name' in '$origin'.\n" ); | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | return $once ? undef : $count; | ||
| 420 | }; | ||
| 421 | |||
| 422 | # flags: | ||
| 423 | # | ||
| 424 | # MAY: don't throw exception if no function is found | ||
| 425 | # ALL: iterate over the whole inheritance tree | ||
| 426 | # HERE: start at the caller's class rather than the object's | ||
| 427 | # UP: iterate base to derived rather than derived to base | ||
| 428 | # | ||
| 429 | # HERE is implied when ALL is absent | ||
| 430 | # | ||
| 431 | # must come in this order | ||
| 432 | # ALL:HERE::MAY::UP | ||
| 433 | # | ||
| 434 | |||
| 435 | { # private lexicals begin | ||
| 436 | |||
| 437 | my $inst_other = sub { | ||
| 438 | |||
| 439 | return unless @_; | ||
| 440 | |||
| 441 | my $auto = join( '::', 'OTHER', @_, 'AUTOLOAD' ); | ||
| 442 | |||
| 443 | no strict 'refs'; | ||
| 444 | |||
| 445 | *{$auto} = *{'OTHER::AUTOLOAD'}{CODE}; | ||
| 446 | |||
| 447 | }; | ||
| 448 | |||
| 449 | |||
| 450 | &$inst_other( qw( MAY ) ); | ||
| 451 | &$inst_other( qw( MAY UP ) ); | ||
| 452 | &$inst_other( qw( UP ) ); | ||
| 453 | |||
| 454 | &$inst_other( qw( ALL ) ); | ||
| 455 | &$inst_other( qw( ALL MAY ) ); | ||
| 456 | &$inst_other( qw( ALL MAY UP ) ); | ||
| 457 | &$inst_other( qw( ALL UP ) ); | ||
| 458 | |||
| 459 | &$inst_other( qw( ALL HERE ) ); | ||
| 460 | &$inst_other( qw( ALL HERE MAY ) ); | ||
| 461 | &$inst_other( qw( ALL HERE MAY UP ) ); | ||
| 462 | &$inst_other( qw( ALL HERE UP ) ); | ||
| 463 | |||
| 464 | } # private lexicals end | ||
| 465 | |||
| 466 | |||
| 467 | 1; | ||
| 468 | |||
| 469 | =head1 AUTHORS | ||
| 470 | |||
| 471 | =over | ||
| 472 | |||
| 473 | =item Kevin Cody-Little <kcody@cpan.org> | ||
| 474 | |||
| 475 | =back | ||
| 476 | |||
| 477 | =cut |