| 1 | # Copyright (c) 2017, Mitchell Cooper | ||
| 2 | # Module represents an API module and provides an interface for managing one. | ||
| 3 | package Evented::API::Module; | ||
| 4 | |||
| 5 | use warnings; | ||
| 6 | use strict; | ||
| 7 | use 5.010; | ||
| 8 | |||
| 9 | use Evented::Object; | ||
| 10 | use parent 'Evented::Object'; | ||
| 11 | |||
| 12 | use Scalar::Util qw(blessed weaken); | ||
| 13 | use List::Util qw(first); | ||
| 14 | |||
| 15 | our $VERSION = '4.07'; | ||
| 16 | |||
| 17 | =head1 NAME | ||
| 18 | |||
| 19 | B<Evented::API::Module> - represents a module for use with | ||
| 20 | L<Evented::API::Engine>. | ||
| 21 | |||
| 22 | =head1 SYNOPSIS | ||
| 23 | |||
| 24 | # Module metadata | ||
| 25 | # | ||
| 26 | # @name: 'My::Module' | ||
| 27 | # @package: 'M::My::Module' | ||
| 28 | # @description: | ||
| 29 | # | ||
| 30 | # @depends.modules+ 'Some::Other' | ||
| 31 | # @depends.modules+ 'Another::Yet' | ||
| 32 | # | ||
| 33 | # @author.name: 'Mitchell Cooper' | ||
| 34 | # @author.website: 'https://github.com/cooper' | ||
| 35 | # | ||
| 36 | package M::My::Module; | ||
| 37 | |||
| 38 | use warnings; | ||
| 39 | use strict; | ||
| 40 | use 5.010; | ||
| 41 | |||
| 42 | # Auto-exported variables | ||
| 43 | our ($api, $mod); | ||
| 44 | |||
| 45 | # Default initializer | ||
| 46 | sub init { | ||
| 47 | say 'Loading ', $mod->name; | ||
| 48 | |||
| 49 | # indicates load success | ||
| 50 | return 1; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Default deinitializer | ||
| 54 | sub void { | ||
| 55 | say 'Bye!'; | ||
| 56 | |||
| 57 | # indicates unload success | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | # Package must return module object | ||
| 62 | $mod; | ||
| 63 | |||
| 64 | =head1 DESCRIPTION | ||
| 65 | |||
| 66 | =head2 Module directory structure | ||
| 67 | |||
| 68 | Modules must be placed within one of the search directories specified in the | ||
| 69 | L<Evented:API::Engine> C<mod_inc> consturctor option or the C<dirs> option | ||
| 70 | of C<< ->load_module >> and similar Engine methods. | ||
| 71 | |||
| 72 | A module in its simplest form is a directory with the C<.module> extension | ||
| 73 | and a single C<.pm> file of the same name. Double-colon namespace separators | ||
| 74 | are condensed to directories, as with normal Perl packages. For instance, a | ||
| 75 | module called My::Backend's main package file could be located at either of the | ||
| 76 | following: | ||
| 77 | |||
| 78 | $SEARCH_DIR/My/Backend.module/Backend.pm | ||
| 79 | $SEARCH_DIR/My/Backend/Backend.module/Backend.pm | ||
| 80 | |||
| 81 | The directory with the C<.module> extension is called the module directory. In | ||
| 82 | addition to the Perl package file, it can house other data associated with the | ||
| 83 | module, such as JSON-encoded metadata, submodules, and documentation. | ||
| 84 | |||
| 85 | $MODULE_DIR/Backend.pm # package file | ||
| 86 | $MODULE_DIR/Backend.json # metadata file | ||
| 87 | $MODULE_DIR/Backend.md # documentation | ||
| 88 | $MODULE_DIR/More.module/More.pm # submodule package | ||
| 89 | $MODULE_DIR/More.module/More.json # submodule metadata | ||
| 90 | |||
| 91 | =head2 Module metadata | ||
| 92 | |||
| 93 | Module metadata is extracted from comments in primary package file. | ||
| 94 | |||
| 95 | Metadata is written to a C<.json> file within the module directory | ||
| 96 | (when developer mode is enabled). These metadata files should be included in | ||
| 97 | distributions, as they contains version and author information. | ||
| 98 | |||
| 99 | B<Syntax> for metadata comments is as follows: | ||
| 100 | |||
| 101 | # @normal: "A normal option with a Perl-encoded value" | ||
| 102 | # @boolean # a boolean option! | ||
| 103 | # @list+ 'A value to add to a list' | ||
| 104 | # @list+ 'Another value to add to the list' | ||
| 105 | |||
| 106 | B<Supported options> | ||
| 107 | |||
| 108 | # @name: 'Some::Module' # module name | ||
| 109 | # @package: 'M::Some::Module' # main package | ||
| 110 | # @package+ 'M::Some::Module::Blah' # additional packages | ||
| 111 | # @version: '1.01' # module version | ||
| 112 | # @depends.modules+ 'Some::Mod::Dependency' # module dependencies | ||
| 113 | # @depends.modules+ 'A::B', 'C::D' # additional dependencies | ||
| 114 | # @depends.bases+ 'Some::Base::Dependency' # base dependencies | ||
| 115 | # @author.name: 'John Doe' # author name | ||
| 116 | # @author.website: 'http://john.example.com' # author URL | ||
| 117 | # @no_bless # don't bless | ||
| 118 | # @preserve_sym # don't erase symbols | ||
| 119 | |||
| 120 | These, I feel, require additional explanation: | ||
| 121 | |||
| 122 | =over | ||
| 123 | |||
| 124 | =item * | ||
| 125 | |||
| 126 | B<@version> - this obviously is a numerical module version. I'm mentioning it | ||
| 127 | here to tell you that you probably shouldn't bother using it, as the API Engine | ||
| 128 | offers automatic versioning (with developer mode enabled) if you simply omit | ||
| 129 | this option. | ||
| 130 | |||
| 131 | =item * | ||
| 132 | |||
| 133 | B<@depends.bases> - like C<@depends.modules>, except the C<Base::> prefix is | ||
| 134 | added. bases are modules which don't really offer any functionality on their | ||
| 135 | own but provide APIs for other modules to use. | ||
| 136 | |||
| 137 | =item * | ||
| 138 | |||
| 139 | B<@no_bless> - if true, the module object will not be blessed to the module's | ||
| 140 | primary package (which is the default behavior). this might be useful if your | ||
| 141 | module contains names conflicting with API Engine or uses autoloading. | ||
| 142 | |||
| 143 | =item * | ||
| 144 | |||
| 145 | B<@preserve_sym> - if true, the packages associated with the module will not be | ||
| 146 | deleted from the symbol table upon unload. this is worse for memory, but it | ||
| 147 | may be necessary for the "guts" of your program, particularly those parts which | ||
| 148 | deal with the loading and unloading of modules themselves. | ||
| 149 | |||
| 150 | =back | ||
| 151 | |||
| 152 | =head1 METHODS | ||
| 153 | |||
| 154 | =cut | ||
| 155 | |||
| 156 | sub new { | ||
| 157 | my ($class, %opts) = @_; | ||
| 158 | my $mod = bless \%opts, $class; | ||
| 159 | Evented::API::Events::add_events($mod); | ||
| 160 | return $mod; | ||
| 161 | } | ||
| 162 | |||
| 163 | =head2 $mod->name | ||
| 164 | |||
| 165 | B<Returns> module full name. | ||
| 166 | |||
| 167 | =head2 $mod->package | ||
| 168 | |||
| 169 | B<Returns> module's primary Perl package name. | ||
| 170 | |||
| 171 | =head2 $mod->api | ||
| 172 | |||
| 173 | B<Returns> associated L<API Engine | Evented::API::Engine>. | ||
| 174 | |||
| 175 | =head2 $mod->parent | ||
| 176 | |||
| 177 | B<Returns> parent module, if this is a submodule. | ||
| 178 | |||
| 179 | =head2 $mod->submodules | ||
| 180 | |||
| 181 | B<Returns> list of loaded submodules objects. | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | sub name { shift->{name}{full} } # full name | ||
| 186 | sub package { shift->{package}[0] } # main package | ||
| 187 | sub api { shift->{api} } | ||
| 188 | sub parent { shift->{parent} } | ||
| 189 | sub submodules { @{ shift->{submodules} || [] } } | ||
| 190 | |||
| 191 | =head2 $mod->Log($msg) | ||
| 192 | |||
| 193 | Used for logging associated with module. Use L<API Engine | Evented:API::Engine> | ||
| 194 | C<< ->Log() >> for messages not associated with a specific module. | ||
| 195 | |||
| 196 | B<Parameters> | ||
| 197 | |||
| 198 | =over | ||
| 199 | |||
| 200 | =item * | ||
| 201 | |||
| 202 | B<$msg> - text to log. | ||
| 203 | |||
| 204 | =back | ||
| 205 | |||
| 206 | =cut | ||
| 207 | |||
| 208 | sub Log { | ||
| 209 | my $mod = shift; | ||
| 210 | $mod->api->Log($mod->name, "@_"); | ||
| 211 | } | ||
| 212 | |||
| 213 | sub _log; | ||
| 214 | *_log = *Log; | ||
| 215 | |||
| 216 | =head2 $mod->get_symbol($sym) | ||
| 217 | |||
| 218 | Fetches the value of a symbol in module's main package. | ||
| 219 | |||
| 220 | B<Parameters> | ||
| 221 | |||
| 222 | =over | ||
| 223 | |||
| 224 | =item * | ||
| 225 | |||
| 226 | B<$sym> - string symbol; such as C<@list>, C<%hash>, or C<$scalar>. | ||
| 227 | |||
| 228 | =back | ||
| 229 | |||
| 230 | B<Returns> | ||
| 231 | |||
| 232 | The data in its native type (NOT a reference), or the zero value for that type | ||
| 233 | if no symbol table entry exists. | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | sub get_symbol { | ||
| 238 | my ($mod, $symbol) = @_; | ||
| 239 | return Evented::Object::Hax::get_symbol($mod->package, $symbol); | ||
| 240 | } | ||
| 241 | |||
| 242 | sub _do_init { | ||
| 243 | my $mod = shift; | ||
| 244 | my $api = $mod->api; | ||
| 245 | |||
| 246 | # fire module initialize. | ||
| 247 | $api->Log($mod->name, 'Initializing'); | ||
| 248 | $api->{indent}++; | ||
| 249 | my $init_fire = $mod->prepare('init')->fire('return_check'); | ||
| 250 | $api->{indent}--; | ||
| 251 | |||
| 252 | # init was stopped. cancel the load. | ||
| 253 | if (my $stopper = $init_fire->stopper) { | ||
| 254 | $mod->Log('init stopped: '.$init_fire->stop); | ||
| 255 | $mod->Log("Load FAILED: Initialization canceled by '$stopper'"); | ||
| 256 | |||
| 257 | $api->_abort_module_load($mod); | ||
| 258 | |||
| 259 | # fire unload so that bases can undo whatever was done up | ||
| 260 | # to the fail point of init. | ||
| 261 | bless $mod, 'Evented::API::Module'; | ||
| 262 | $mod->fire('unload'); | ||
| 263 | |||
| 264 | return; | ||
| 265 | } | ||
| 266 | |||
| 267 | # init was successful | ||
| 268 | return 1; | ||
| 269 | } | ||
| 270 | |||
| 271 | sub _do_void { | ||
| 272 | my ($mod, $unloading_submodule) = @_; | ||
| 273 | my $api = $mod->api; | ||
| 274 | |||
| 275 | # fire module void. | ||
| 276 | # consider: should this have return_check like init? | ||
| 277 | $mod->Log('Voiding'); | ||
| 278 | my $void_fire = $mod->fire('void'); | ||
| 279 | |||
| 280 | # init was stopped. cancel the unload. | ||
| 281 | my $stopper = $void_fire->stopper; | ||
| 282 | if (!$unloading_submodule && $stopper) { | ||
| 283 | $mod->Log("void stopped: ".$void_fire->stop); | ||
| 284 | $mod->Log("Can't unload: canceled by '$stopper'"); | ||
| 285 | return; | ||
| 286 | } | ||
| 287 | |||
| 288 | # if this is a submodule, it isn't allowed to refuse to unload. | ||
| 289 | elsif ($stopper) { | ||
| 290 | $mod->Log( | ||
| 291 | "Warning! This submodule has requested to remain ". | ||
| 292 | 'loaded, but submodules MUST be unloaded with their parent' | ||
| 293 | ); | ||
| 294 | } | ||
| 295 | |||
| 296 | # void was successful | ||
| 297 | return 1; | ||
| 298 | |||
| 299 | } | ||
| 300 | |||
| 301 | ################## | ||
| 302 | ### SUBMODULES ### | ||
| 303 | ################## | ||
| 304 | |||
| 305 | =head2 $mod->load_submodule($submod_name) | ||
| 306 | |||
| 307 | Loads a submodule. | ||
| 308 | |||
| 309 | This is generally used within the module initializer. | ||
| 310 | |||
| 311 | B<Parameters> | ||
| 312 | |||
| 313 | =over | ||
| 314 | |||
| 315 | =item * | ||
| 316 | |||
| 317 | B<$submod_name> - name of the submodule, without the parent module name prefix. | ||
| 318 | for instance if the full submodule name is C<My::Module::Sub>, this is simply | ||
| 319 | C<Sub>. | ||
| 320 | |||
| 321 | =back | ||
| 322 | |||
| 323 | B<Returns> | ||
| 324 | |||
| 325 | Submodule object on success, false otherwise. | ||
| 326 | |||
| 327 | =cut | ||
| 328 | |||
| 329 | sub load_submodule { | ||
| 330 | my ($mod, $mod_name) = @_; | ||
| 331 | $mod->Log("Loading submodule $mod_name"); | ||
| 332 | |||
| 333 | # call ->load_module with the search dir set to the | ||
| 334 | # parent module's main directory. | ||
| 335 | $mod->api->{indent}++; | ||
| 336 | my $ret = $mod->api->load_module($mod_name, [ $mod->{dir} ], 1); | ||
| 337 | $mod->api->{indent}--; | ||
| 338 | |||
| 339 | # add weakly to submodules list. hold weak reference to parent module. | ||
| 340 | if ($ret) { | ||
| 341 | my $a = $mod->{submodules} ||= []; | ||
| 342 | push @$a, $ret; | ||
| 343 | weaken($a->[$#$a]); | ||
| 344 | weaken($ret->{parent} = $mod); | ||
| 345 | } | ||
| 346 | |||
| 347 | return $ret; | ||
| 348 | } | ||
| 349 | |||
| 350 | =head2 $mod->unload_submodule($submod) | ||
| 351 | |||
| 352 | Unloads a submodule. | ||
| 353 | |||
| 354 | You do not have to call this in the parent module deinitializer. Only use this | ||
| 355 | method if you want to dynamically unload a submodule for some reason without | ||
| 356 | unloading the parent module. | ||
| 357 | |||
| 358 | B<Parameters> | ||
| 359 | |||
| 360 | =over | ||
| 361 | |||
| 362 | =item * | ||
| 363 | |||
| 364 | B<$submod> - submodule object or name, without the parent module name prefix. | ||
| 365 | for instance if the full submodule name is C<My::Module::Sub>, this is simply | ||
| 366 | C<Sub>. | ||
| 367 | |||
| 368 | =back | ||
| 369 | |||
| 370 | =cut | ||
| 371 | |||
| 372 | sub unload_submodule { | ||
| 373 | my ($mod, $submod, $reloading) = @_; | ||
| 374 | my $submod_name = $submod->name; | ||
| 375 | $mod->Log("Unloading submodule $submod_name"); | ||
| 376 | |||
| 377 | # unload | ||
| 378 | $mod->api->{indent}++; | ||
| 379 | |||
| 380 | # ($mod, $unload_dependents, $force, $unloading_submodule, $reloading) | ||
| 381 | # | ||
| 382 | # do not force, as that might unload the parent | ||
| 383 | # but do say we are unloading a submodule so it can be unloaded | ||
| 384 | # independently (which usually wouldn't be allowed) | ||
| 385 | # | ||
| 386 | $mod->api->unload_module($submod, undef, undef, 1, $reloading); | ||
| 387 | |||
| 388 | $mod->api->{indent}--; | ||
| 389 | |||
| 390 | # remove from submodules | ||
| 391 | if (my $submods = $mod->{submodules}) { | ||
| 392 | @$submods = grep { $_ != $submod } @$submods; | ||
| 393 | } | ||
| 394 | delete $submod->{parent}; | ||
| 395 | |||
| 396 | return 1; | ||
| 397 | } | ||
| 398 | |||
| 399 | =head2 $mod->add_companion_submodule($mod_name, $submod_name) | ||
| 400 | |||
| 401 | Registers a submodule (provided by this module) as a companion of another | ||
| 402 | top-level module. | ||
| 403 | |||
| 404 | Companion submodules are submodules which are automatically loaded and unloaded | ||
| 405 | as needed in conjunction with other top-level modules. | ||
| 406 | |||
| 407 | This is generally called from within the parent module initializer. | ||
| 408 | |||
| 409 | B<Parameters> | ||
| 410 | |||
| 411 | =over | ||
| 412 | |||
| 413 | =item * | ||
| 414 | |||
| 415 | B<$mod_name> - name of another top-level module to register the submodule as | ||
| 416 | a companion. | ||
| 417 | |||
| 418 | =item * | ||
| 419 | |||
| 420 | B<$submod_name> - name of the submodule to register a companion, without the | ||
| 421 | parent module name prefix. for instance if the full submodule name is | ||
| 422 | C<My::Module::Sub>, this is simply C<Sub>. | ||
| 423 | |||
| 424 | =back | ||
| 425 | |||
| 426 | =cut | ||
| 427 | |||
| 428 | sub add_companion_submodule { | ||
| 429 | my ($mod, $mod_name, $submod_name) = @_; | ||
| 430 | $mod->api->_add_companion_submodule_wait($mod, $mod_name, $submod_name); | ||
| 431 | } | ||
| 432 | |||
| 433 | #################### | ||
| 434 | ### DATA STORAGE ### | ||
| 435 | #################### | ||
| 436 | |||
| 437 | =head2 $mod->store($key, $value) | ||
| 438 | |||
| 439 | Stores a piece of data associated with module. | ||
| 440 | |||
| 441 | B<Parameters> | ||
| 442 | |||
| 443 | =over | ||
| 444 | |||
| 445 | =item * | ||
| 446 | |||
| 447 | B<$key> - name for fetching data later. | ||
| 448 | |||
| 449 | =item * | ||
| 450 | |||
| 451 | B<$value> - value to store. | ||
| 452 | |||
| 453 | =back | ||
| 454 | |||
| 455 | =cut | ||
| 456 | |||
| 457 | # store a piece of data specific to this module. | ||
| 458 | sub store { | ||
| 459 | my ($mod, $key, $value) = @_; | ||
| 460 | $mod->{store}{$key} = $value; | ||
| 461 | } | ||
| 462 | |||
| 463 | =head2 $mod->retrieve($key, $default_value) | ||
| 464 | |||
| 465 | Retrieves a piece of data associated with module. | ||
| 466 | |||
| 467 | B<Parameters> | ||
| 468 | |||
| 469 | =over | ||
| 470 | |||
| 471 | =item * | ||
| 472 | |||
| 473 | B<$key> - name associated with data to fetch. | ||
| 474 | |||
| 475 | =back | ||
| 476 | |||
| 477 | B<Returns> | ||
| 478 | |||
| 479 | Fetched data, undef if not found. | ||
| 480 | |||
| 481 | =cut | ||
| 482 | |||
| 483 | # fetch a piece of data specific to this module. | ||
| 484 | sub retrieve { | ||
| 485 | my ($mod, $key, $default_value) = @_; | ||
| 486 | return $mod->{store}{$key} //= $default_value; | ||
| 487 | } | ||
| 488 | |||
| 489 | =head2 $mod->list_store_add($key, $value) | ||
| 490 | |||
| 491 | Adds an entry to a list of data associated with module. | ||
| 492 | |||
| 493 | =over | ||
| 494 | |||
| 495 | =item * | ||
| 496 | |||
| 497 | B<$key> - name for fetching data later. | ||
| 498 | |||
| 499 | =item * | ||
| 500 | |||
| 501 | B<$value> - value to add. | ||
| 502 | |||
| 503 | =back | ||
| 504 | |||
| 505 | =cut | ||
| 506 | |||
| 507 | # adds the item to a list store. | ||
| 508 | # if the store doesn't exist, creates it. | ||
| 509 | sub list_store_add { | ||
| 510 | my ($mod, $key, $value) = @_; | ||
| 511 | push @{ $mod->{store}{$key} ||= [] }, $value; | ||
| 512 | } | ||
| 513 | |||
| 514 | =head2 $mod->list_store_remove_matches($key, $code, $max) | ||
| 515 | |||
| 516 | Removes entries satisfying a code from a list of data associated with module. | ||
| 517 | |||
| 518 | B<Parameters> | ||
| 519 | |||
| 520 | =over | ||
| 521 | |||
| 522 | =item * | ||
| 523 | |||
| 524 | B<$key> - name of list. | ||
| 525 | |||
| 526 | =item * | ||
| 527 | |||
| 528 | B<$code> - code reference passed each entry which should return true for | ||
| 529 | matches. | ||
| 530 | |||
| 531 | =item * | ||
| 532 | |||
| 533 | B<$max> - I<optional>, maximum number of entries to remove. | ||
| 534 | |||
| 535 | =back | ||
| 536 | |||
| 537 | B<Returns> | ||
| 538 | |||
| 539 | Number of items removed, false if none matched. | ||
| 540 | |||
| 541 | =cut | ||
| 542 | |||
| 543 | # remove a single item matching. | ||
| 544 | # $max = stop searching when removed this many (optional) | ||
| 545 | sub list_store_remove_matches { | ||
| 546 | my ($mod, $key, $sub, $max) = @_; | ||
| 547 | my @before = @{ $mod->{store}{$key} or return }; | ||
| 548 | my ($removed, @after) = 0; | ||
| 549 | while (my $item = shift @before) { | ||
| 550 | |||
| 551 | # it matches. add the remaining. | ||
| 552 | if ($sub->($item)) { | ||
| 553 | last if $max && $removed == $max; | ||
| 554 | next; | ||
| 555 | } | ||
| 556 | |||
| 557 | # no match. add and continue. | ||
| 558 | push @after, $item; | ||
| 559 | |||
| 560 | } | ||
| 561 | |||
| 562 | # add the rest, store. | ||
| 563 | push @after, @before; | ||
| 564 | $mod->{store}{$key} = \@after; | ||
| 565 | |||
| 566 | return $removed; | ||
| 567 | } | ||
| 568 | |||
| 569 | =head2 $mod->list_store_items($key) | ||
| 570 | |||
| 571 | Fetches all values in a list associated with module. | ||
| 572 | |||
| 573 | =over | ||
| 574 | |||
| 575 | =item * | ||
| 576 | |||
| 577 | B<$key> - name of the list to retrieve. | ||
| 578 | |||
| 579 | =back | ||
| 580 | |||
| 581 | B<Returns> | ||
| 582 | |||
| 583 | List of fetch values, or empty list if none were found. | ||
| 584 | |||
| 585 | =cut | ||
| 586 | |||
| 587 | # returns all the items in a list store. | ||
| 588 | # if the store doesn't exist, this is | ||
| 589 | # still safe and returns an empty list. | ||
| 590 | sub list_store_items { | ||
| 591 | my ($mod, $key) = @_; | ||
| 592 | return @{ $mod->{store}{$key} || [] }; | ||
| 593 | } | ||
| 594 | |||
| 595 | #################### | ||
| 596 | ### DEPENDENCIES ### | ||
| 597 | #################### | ||
| 598 | |||
| 599 | =head2 $mod->dependencies | ||
| 600 | |||
| 601 | B<Returns> top-level module dependencies. | ||
| 602 | |||
| 603 | =cut | ||
| 604 | |||
| 605 | # returns the modules that this depends on. | ||
| 606 | sub dependencies { | ||
| 607 | return @{ shift->{dependencies} || [] }; | ||
| 608 | } | ||
| 609 | |||
| 610 | =head2 $mod->companions | ||
| 611 | |||
| 612 | For companion submodules, | ||
| 613 | B<Returns> modules that this submodule depends on. | ||
| 614 | |||
| 615 | =cut | ||
| 616 | |||
| 617 | # returns the modules that this companion submodule depends on. | ||
| 618 | sub companions { | ||
| 619 | return @{ shift->{companions} || [] }; | ||
| 620 | } | ||
| 621 | |||
| 622 | =head2 $mod->dependents | ||
| 623 | |||
| 624 | B<Returns> top-level modules that depend on this module. | ||
| 625 | |||
| 626 | =cut | ||
| 627 | |||
| 628 | # returns the top-level modules that depend on this. | ||
| 629 | sub dependents { | ||
| 630 | my $mod = shift; | ||
| 631 | my @mods; | ||
| 632 | foreach my $m (@{ $mod->api->{loaded} }) { | ||
| 633 | next unless first { $_ == $mod } $m->dependencies; | ||
| 634 | push @mods, $m; | ||
| 635 | } | ||
| 636 | return @mods; | ||
| 637 | } | ||
| 638 | |||
| 639 | =head2 $mod->dependent_companions | ||
| 640 | |||
| 641 | B<Returns> companion submodules that depend on this module. | ||
| 642 | |||
| 643 | =cut | ||
| 644 | |||
| 645 | # returns the companion submodules that depend on this. | ||
| 646 | sub dependent_companions { | ||
| 647 | my $mod = shift; | ||
| 648 | my @mods; | ||
| 649 | foreach my $m (@{ $mod->api->{loaded} }) { | ||
| 650 | next unless first { $_ == $mod } $m->companions; | ||
| 651 | push @mods, $m; | ||
| 652 | } | ||
| 653 | return @mods; | ||
| 654 | } | ||
| 655 | |||
| 656 | 1; |