| 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.09'; | ||
| 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->packages | ||
| 172 | |||
| 173 | B<Returns> all packages associate with module. | ||
| 174 | |||
| 175 | =head2 $mod->api | ||
| 176 | |||
| 177 | B<Returns> associated L<API Engine | Evented::API::Engine>. | ||
| 178 | |||
| 179 | =head2 $mod->parent | ||
| 180 | |||
| 181 | B<Returns> parent module, if this is a submodule. | ||
| 182 | |||
| 183 | =head2 $mod->submodules | ||
| 184 | |||
| 185 | B<Returns> list of loaded submodules objects. | ||
| 186 | |||
| 187 | =cut | ||
| 188 | |||
| 189 | sub name { shift->{name}{full} } # full name | ||
| 190 | sub package { shift->{package}[0] } # main package | ||
| 191 | sub packages { @{ shift->{package} } } # all packages | ||
| 192 | sub api { shift->{api} } | ||
| 193 | sub parent { shift->{parent} } | ||
| 194 | sub submodules { @{ shift->{submodules} || [] } } | ||
| 195 | |||
| 196 | =head2 $mod->Log($msg) | ||
| 197 | |||
| 198 | Used for logging associated with module. Use L<API Engine | Evented:API::Engine> | ||
| 199 | C<< ->Log() >> for messages not associated with a specific module. | ||
| 200 | |||
| 201 | B<Parameters> | ||
| 202 | |||
| 203 | =over | ||
| 204 | |||
| 205 | =item * | ||
| 206 | |||
| 207 | B<$msg> - text to log. | ||
| 208 | |||
| 209 | =back | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | sub Log { | ||
| 214 | my $mod = shift; | ||
| 215 | $mod->api->Log($mod->name, "@_"); | ||
| 216 | } | ||
| 217 | |||
| 218 | sub _log; | ||
| 219 | *_log = *Log; | ||
| 220 | |||
| 221 | =head2 $mod->get_symbol($sym) | ||
| 222 | |||
| 223 | Fetches the value of a symbol in module's main package. | ||
| 224 | |||
| 225 | B<Parameters> | ||
| 226 | |||
| 227 | =over | ||
| 228 | |||
| 229 | =item * | ||
| 230 | |||
| 231 | B<$sym> - string symbol; such as C<@list>, C<%hash>, or C<$scalar>. | ||
| 232 | |||
| 233 | =back | ||
| 234 | |||
| 235 | B<Returns> | ||
| 236 | |||
| 237 | The data in its native type (NOT a reference), or the zero value for that type | ||
| 238 | if no symbol table entry exists. | ||
| 239 | |||
| 240 | =cut | ||
| 241 | |||
| 242 | sub get_symbol { | ||
| 243 | my ($mod, $symbol) = @_; | ||
| 244 | return Evented::Object::Hax::get_symbol($mod->package, $symbol); | ||
| 245 | } | ||
| 246 | |||
| 247 | sub _do_init { | ||
| 248 | my $mod = shift; | ||
| 249 | my $api = $mod->api; | ||
| 250 | |||
| 251 | # fire module initialize. | ||
| 252 | $api->Log($mod->name, 'Initializing'); | ||
| 253 | $api->{indent}++; | ||
| 254 | my $init_fire = $mod->prepare('init')->fire('return_check'); | ||
| 255 | $api->{indent}--; | ||
| 256 | |||
| 257 | # init was stopped. cancel the load. | ||
| 258 | if (my $stopper = $init_fire->stopper) { | ||
| 259 | $mod->Log('init stopped: '.$init_fire->stop); | ||
| 260 | $mod->Log("Load FAILED: Initialization canceled by '$stopper'"); | ||
| 261 | |||
| 262 | $api->_abort_module_load($mod); | ||
| 263 | |||
| 264 | # fire unload so that bases can undo whatever was done up | ||
| 265 | # to the fail point of init. | ||
| 266 | bless $mod, 'Evented::API::Module'; | ||
| 267 | $mod->fire('unload'); | ||
| 268 | |||
| 269 | return; | ||
| 270 | } | ||
| 271 | |||
| 272 | # init was successful | ||
| 273 | return 1; | ||
| 274 | } | ||
| 275 | |||
| 276 | sub _do_void { | ||
| 277 | my ($mod, $unloading_submodule) = @_; | ||
| 278 | my $api = $mod->api; | ||
| 279 | |||
| 280 | # fire module void. | ||
| 281 | # consider: should this have return_check like init? | ||
| 282 | $mod->Log('Voiding'); | ||
| 283 | my $void_fire = $mod->fire('void'); | ||
| 284 | |||
| 285 | # init was stopped. cancel the unload. | ||
| 286 | my $stopper = $void_fire->stopper; | ||
| 287 | if (!$unloading_submodule && $stopper) { | ||
| 288 | $mod->Log("void stopped: ".$void_fire->stop); | ||
| 289 | $mod->Log("Can't unload: canceled by '$stopper'"); | ||
| 290 | return; | ||
| 291 | } | ||
| 292 | |||
| 293 | # if this is a submodule, it isn't allowed to refuse to unload. | ||
| 294 | elsif ($stopper) { | ||
| 295 | $mod->Log( | ||
| 296 | "Warning! This submodule has requested to remain ". | ||
| 297 | 'loaded, but submodules MUST be unloaded with their parent' | ||
| 298 | ); | ||
| 299 | } | ||
| 300 | |||
| 301 | # void was successful | ||
| 302 | return 1; | ||
| 303 | |||
| 304 | } | ||
| 305 | |||
| 306 | ################## | ||
| 307 | ### SUBMODULES ### | ||
| 308 | ################## | ||
| 309 | |||
| 310 | =head2 $mod->load_submodule($submod_name) | ||
| 311 | |||
| 312 | Loads a submodule. | ||
| 313 | |||
| 314 | This is generally used within the module initializer. | ||
| 315 | |||
| 316 | B<Parameters> | ||
| 317 | |||
| 318 | =over | ||
| 319 | |||
| 320 | =item * | ||
| 321 | |||
| 322 | B<$submod_name> - name of the submodule, without the parent module name prefix. | ||
| 323 | for instance if the full submodule name is C<My::Module::Sub>, this is simply | ||
| 324 | C<Sub>. | ||
| 325 | |||
| 326 | =back | ||
| 327 | |||
| 328 | B<Returns> | ||
| 329 | |||
| 330 | Submodule object on success, false otherwise. | ||
| 331 | |||
| 332 | =cut | ||
| 333 | |||
| 334 | sub load_submodule { | ||
| 335 | my ($mod, $mod_name) = @_; | ||
| 336 | $mod->Log("Loading submodule $mod_name"); | ||
| 337 | |||
| 338 | # call ->load_module with the search dir set to the | ||
| 339 | # parent module's main directory. | ||
| 340 | $mod->api->{indent}++; | ||
| 341 | my $ret = $mod->api->load_module($mod_name, [ $mod->{dir} ], 1); | ||
| 342 | $mod->api->{indent}--; | ||
| 343 | |||
| 344 | # add weakly to submodules list. hold weak reference to parent module. | ||
| 345 | if ($ret) { | ||
| 346 | my $a = $mod->{submodules} ||= []; | ||
| 347 | push @$a, $ret; | ||
| 348 | weaken($a->[$#$a]); | ||
| 349 | weaken($ret->{parent} = $mod); | ||
| 350 | } | ||
| 351 | |||
| 352 | return $ret; | ||
| 353 | } | ||
| 354 | |||
| 355 | =head2 $mod->unload_submodule($submod) | ||
| 356 | |||
| 357 | Unloads a submodule. | ||
| 358 | |||
| 359 | You do not have to call this in the parent module deinitializer. Only use this | ||
| 360 | method if you want to dynamically unload a submodule for some reason without | ||
| 361 | unloading the parent module. | ||
| 362 | |||
| 363 | B<Parameters> | ||
| 364 | |||
| 365 | =over | ||
| 366 | |||
| 367 | =item * | ||
| 368 | |||
| 369 | B<$submod> - submodule object or name, without the parent module name prefix. | ||
| 370 | for instance if the full submodule name is C<My::Module::Sub>, this is simply | ||
| 371 | C<Sub>. | ||
| 372 | |||
| 373 | =back | ||
| 374 | |||
| 375 | =cut | ||
| 376 | |||
| 377 | sub unload_submodule { | ||
| 378 | my ($mod, $submod, $reloading) = @_; | ||
| 379 | my $submod_name = $submod->name; | ||
| 380 | $mod->Log("Unloading submodule $submod_name"); | ||
| 381 | |||
| 382 | # unload | ||
| 383 | $mod->api->{indent}++; | ||
| 384 | |||
| 385 | # ($mod, $unload_dependents, $force, $unloading_submodule, $reloading) | ||
| 386 | # | ||
| 387 | # do not force, as that might unload the parent | ||
| 388 | # but do say we are unloading a submodule so it can be unloaded | ||
| 389 | # independently (which usually wouldn't be allowed) | ||
| 390 | # | ||
| 391 | $mod->api->unload_module($submod, undef, undef, 1, $reloading); | ||
| 392 | |||
| 393 | $mod->api->{indent}--; | ||
| 394 | |||
| 395 | # remove from submodules | ||
| 396 | if (my $submods = $mod->{submodules}) { | ||
| 397 | @$submods = grep { $_ != $submod } @$submods; | ||
| 398 | } | ||
| 399 | delete $submod->{parent}; | ||
| 400 | |||
| 401 | return 1; | ||
| 402 | } | ||
| 403 | |||
| 404 | =head2 $mod->add_companion_submodule($mod_name, $submod_name) | ||
| 405 | |||
| 406 | Registers a submodule (provided by this module) as a companion of another | ||
| 407 | top-level module. | ||
| 408 | |||
| 409 | Companion submodules are submodules which are automatically loaded and unloaded | ||
| 410 | as needed in conjunction with other top-level modules. | ||
| 411 | |||
| 412 | This is generally called from within the parent module initializer. | ||
| 413 | |||
| 414 | B<Parameters> | ||
| 415 | |||
| 416 | =over | ||
| 417 | |||
| 418 | =item * | ||
| 419 | |||
| 420 | B<$mod_name> - name of another top-level module to register the submodule as | ||
| 421 | a companion. | ||
| 422 | |||
| 423 | =item * | ||
| 424 | |||
| 425 | B<$submod_name> - name of the submodule to register a companion, without the | ||
| 426 | parent module name prefix. for instance if the full submodule name is | ||
| 427 | C<My::Module::Sub>, this is simply C<Sub>. | ||
| 428 | |||
| 429 | =back | ||
| 430 | |||
| 431 | =cut | ||
| 432 | |||
| 433 | sub add_companion_submodule { | ||
| 434 | my ($mod, $mod_name, $submod_name) = @_; | ||
| 435 | $mod->api->_add_companion_submodule_wait($mod, $mod_name, $submod_name); | ||
| 436 | } | ||
| 437 | |||
| 438 | #################### | ||
| 439 | ### DATA STORAGE ### | ||
| 440 | #################### | ||
| 441 | |||
| 442 | =head2 $mod->store($key, $value) | ||
| 443 | |||
| 444 | Stores a piece of data associated with module. | ||
| 445 | |||
| 446 | B<Parameters> | ||
| 447 | |||
| 448 | =over | ||
| 449 | |||
| 450 | =item * | ||
| 451 | |||
| 452 | B<$key> - name for fetching data later. | ||
| 453 | |||
| 454 | =item * | ||
| 455 | |||
| 456 | B<$value> - value to store. | ||
| 457 | |||
| 458 | =back | ||
| 459 | |||
| 460 | =cut | ||
| 461 | |||
| 462 | # store a piece of data specific to this module. | ||
| 463 | sub store { | ||
| 464 | my ($mod, $key, $value) = @_; | ||
| 465 | $mod->{store}{$key} = $value; | ||
| 466 | } | ||
| 467 | |||
| 468 | =head2 $mod->retrieve($key, $default_value) | ||
| 469 | |||
| 470 | Retrieves a piece of data associated with module. | ||
| 471 | |||
| 472 | B<Parameters> | ||
| 473 | |||
| 474 | =over | ||
| 475 | |||
| 476 | =item * | ||
| 477 | |||
| 478 | B<$key> - name associated with data to fetch. | ||
| 479 | |||
| 480 | =back | ||
| 481 | |||
| 482 | B<Returns> | ||
| 483 | |||
| 484 | Fetched data, undef if not found. | ||
| 485 | |||
| 486 | =cut | ||
| 487 | |||
| 488 | # fetch a piece of data specific to this module. | ||
| 489 | sub retrieve { | ||
| 490 | my ($mod, $key, $default_value) = @_; | ||
| 491 | return $mod->{store}{$key} //= $default_value; | ||
| 492 | } | ||
| 493 | |||
| 494 | =head2 $mod->list_store_add($key, $value) | ||
| 495 | |||
| 496 | Adds an entry to a list of data associated with module. | ||
| 497 | |||
| 498 | =over | ||
| 499 | |||
| 500 | =item * | ||
| 501 | |||
| 502 | B<$key> - name for fetching data later. | ||
| 503 | |||
| 504 | =item * | ||
| 505 | |||
| 506 | B<$value> - value to add. | ||
| 507 | |||
| 508 | =back | ||
| 509 | |||
| 510 | =cut | ||
| 511 | |||
| 512 | # adds the item to a list store. | ||
| 513 | # if the store doesn't exist, creates it. | ||
| 514 | sub list_store_add { | ||
| 515 | my ($mod, $key, $value) = @_; | ||
| 516 | push @{ $mod->{store}{$key} ||= [] }, $value; | ||
| 517 | } | ||
| 518 | |||
| 519 | =head2 $mod->list_store_remove_matches($key, $code, $max) | ||
| 520 | |||
| 521 | Removes entries satisfying a code from a list of data associated with module. | ||
| 522 | |||
| 523 | B<Parameters> | ||
| 524 | |||
| 525 | =over | ||
| 526 | |||
| 527 | =item * | ||
| 528 | |||
| 529 | B<$key> - name of list. | ||
| 530 | |||
| 531 | =item * | ||
| 532 | |||
| 533 | B<$code> - code reference passed each entry which should return true for | ||
| 534 | matches. | ||
| 535 | |||
| 536 | =item * | ||
| 537 | |||
| 538 | B<$max> - I<optional>, maximum number of entries to remove. | ||
| 539 | |||
| 540 | =back | ||
| 541 | |||
| 542 | B<Returns> | ||
| 543 | |||
| 544 | Number of items removed, false if none matched. | ||
| 545 | |||
| 546 | =cut | ||
| 547 | |||
| 548 | # remove a single item matching. | ||
| 549 | # $max = stop searching when removed this many (optional) | ||
| 550 | sub list_store_remove_matches { | ||
| 551 | my ($mod, $key, $sub, $max) = @_; | ||
| 552 | my @before = @{ $mod->{store}{$key} or return }; | ||
| 553 | my ($removed, @after) = 0; | ||
| 554 | while (my $item = shift @before) { | ||
| 555 | |||
| 556 | # it matches. add the remaining. | ||
| 557 | if ($sub->($item)) { | ||
| 558 | last if $max && $removed == $max; | ||
| 559 | next; | ||
| 560 | } | ||
| 561 | |||
| 562 | # no match. add and continue. | ||
| 563 | push @after, $item; | ||
| 564 | |||
| 565 | } | ||
| 566 | |||
| 567 | # add the rest, store. | ||
| 568 | push @after, @before; | ||
| 569 | $mod->{store}{$key} = \@after; | ||
| 570 | |||
| 571 | return $removed; | ||
| 572 | } | ||
| 573 | |||
| 574 | =head2 $mod->list_store_items($key) | ||
| 575 | |||
| 576 | Fetches all values in a list associated with module. | ||
| 577 | |||
| 578 | =over | ||
| 579 | |||
| 580 | =item * | ||
| 581 | |||
| 582 | B<$key> - name of the list to retrieve. | ||
| 583 | |||
| 584 | =back | ||
| 585 | |||
| 586 | B<Returns> | ||
| 587 | |||
| 588 | List of fetch values, or empty list if none were found. | ||
| 589 | |||
| 590 | =cut | ||
| 591 | |||
| 592 | # returns all the items in a list store. | ||
| 593 | # if the store doesn't exist, this is | ||
| 594 | # still safe and returns an empty list. | ||
| 595 | sub list_store_items { | ||
| 596 | my ($mod, $key) = @_; | ||
| 597 | return @{ $mod->{store}{$key} || [] }; | ||
| 598 | } | ||
| 599 | |||
| 600 | #################### | ||
| 601 | ### DEPENDENCIES ### | ||
| 602 | #################### | ||
| 603 | |||
| 604 | =head2 $mod->dependencies | ||
| 605 | |||
| 606 | B<Returns> top-level module dependencies. | ||
| 607 | |||
| 608 | =cut | ||
| 609 | |||
| 610 | # returns the modules that this depends on. | ||
| 611 | sub dependencies { | ||
| 612 | return @{ shift->{dependencies} || [] }; | ||
| 613 | } | ||
| 614 | |||
| 615 | =head2 $mod->companions | ||
| 616 | |||
| 617 | For companion submodules, | ||
| 618 | B<Returns> modules that this submodule depends on. | ||
| 619 | |||
| 620 | =cut | ||
| 621 | |||
| 622 | # returns the modules that this companion submodule depends on. | ||
| 623 | sub companions { | ||
| 624 | return @{ shift->{companions} || [] }; | ||
| 625 | } | ||
| 626 | |||
| 627 | =head2 $mod->dependents | ||
| 628 | |||
| 629 | B<Returns> top-level modules that depend on this module. | ||
| 630 | |||
| 631 | =cut | ||
| 632 | |||
| 633 | # returns the top-level modules that depend on this. | ||
| 634 | sub dependents { | ||
| 635 | my $mod = shift; | ||
| 636 | my @mods; | ||
| 637 | foreach my $m (@{ $mod->api->{loaded} }) { | ||
| 638 | next unless first { $_ == $mod } $m->dependencies; | ||
| 639 | push @mods, $m; | ||
| 640 | } | ||
| 641 | return @mods; | ||
| 642 | } | ||
| 643 | |||
| 644 | =head2 $mod->dependent_companions | ||
| 645 | |||
| 646 | B<Returns> companion submodules that depend on this module. | ||
| 647 | |||
| 648 | =cut | ||
| 649 | |||
| 650 | # returns the companion submodules that depend on this. | ||
| 651 | sub dependent_companions { | ||
| 652 | my $mod = shift; | ||
| 653 | my @mods; | ||
| 654 | foreach my $m (@{ $mod->api->{loaded} }) { | ||
| 655 | next unless first { $_ == $mod } $m->companions; | ||
| 656 | push @mods, $m; | ||
| 657 | } | ||
| 658 | return @mods; | ||
| 659 | } | ||
| 660 | |||
| 661 | 1; |