| 1 | package FindApp::Object::Behavior::Loader; | ||
| 2 | |||
| 3 | use v5.10; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | use mro "c3"; | ||
| 7 | |||
| 8 | use FindApp::Utils qw(:all); | ||
| 9 | |||
| 10 | my %Internal = map { $_ => 1 } @CARP_NOT; | ||
| 11 | |||
| 12 | sub foreign_caller { | ||
| 13 | my $frame = 0; | ||
| 14 | my($pkg, $file, $line, $sub); | ||
| 15 | while (1) { | ||
| 16 | ($pkg, $file, $line, $sub) = caller($frame); | ||
| 17 | last unless $pkg; | ||
| 18 | last if $sub eq "FindApp::Exporter::import"; | ||
| 19 | last unless $pkg =~ /^FindApp\b/; | ||
| 20 | last unless $Internal{$pkg}; | ||
| 21 | $frame++; | ||
| 22 | panic("runaway frame search") if $frame > 100; | ||
| 23 | } | ||
| 24 | croak "can't find importing package" unless $pkg; | ||
| 25 | return $pkg; | ||
| 26 | } | ||
| 27 | |||
| 28 | use namespace::clean; | ||
| 29 | |||
| 30 | use pluskeys qw{ | ||
| 31 | MODULES_USED | ||
| 32 | FILES_REQUIRED | ||
| 33 | }; | ||
| 34 | |||
| 35 | sub init { &ENTER_TRACE; | ||
| 36 | my($self) = @_; | ||
| 37 | $self->maybe::next::method; | ||
| 38 | $$self{+MODULES_USED} = []; | ||
| 39 | $$self{+FILES_REQUIRED} = []; | ||
| 40 | } | ||
| 41 | |||
| 42 | # overrides the f:o:Behavior method, not the f:o:s:Group Method. | ||
| 43 | sub export_lib_to_env { &ENTER_TRACE; | ||
| 44 | my $self = &myself; | ||
| 45 | $self->next::method(@_); # first get @INC setup | ||
| 46 | $self->load_wanted; # so we can load whatever they asked for | ||
| 47 | } | ||
| 48 | |||
| 49 | sub load_wanted { &ENTER_TRACE; | ||
| 50 | my $self = &myself; | ||
| 51 | return unless my @wanted = map { $self->$_->wanted } <rootdir libdirs>; | ||
| 52 | $self->load_libraries(@wanted); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub load_libraries { &ENTER_TRACE; | ||
| 56 | my $self = &myself; | ||
| 57 | return unless @_; | ||
| 58 | $self->load_modules(grep /^\w+(?:::\w+)+$/ => @_); | ||
| 59 | $self->load_files (grep /\.p[lm]$/ => @_); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub load_modules { &ENTER_TRACE; | ||
| 63 | my($self, $caller) = (&myself, &foreign_caller); | ||
| 64 | for my $mod (@_) { | ||
| 65 | debug "load $mod into $caller"; | ||
| 66 | eval qq{package $caller; use $mod; 1} || die; | ||
| 67 | $self->another_module_used($mod); | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | sub another_module_used { | ||
| 72 | my $self = &myself; | ||
| 73 | push @{ $$self{+MODULES_USED} }, @_; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub used_modules { | ||
| 77 | my $self = &myself; | ||
| 78 | @{ $$self{+MODULES_USED} }; | ||
| 79 | } | ||
| 80 | |||
| 81 | sub load_files { &ENTER_TRACE; | ||
| 82 | my $self = &myself; | ||
| 83 | for my $file (@_) { | ||
| 84 | require "$file"; # the "new" version stuff has messed this all up! | ||
| 85 | $self->another_file_required($file); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | |||
| 89 | sub another_file_required { | ||
| 90 | my $self = &myself; | ||
| 91 | push @{ $$self{+FILES_REQUIRED} }, @_; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub required_files { | ||
| 95 | my $self = &myself; | ||
| 96 | @{ $$self{+FILES_REQUIRED} }; | ||
| 97 | } | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | =encoding utf8 | ||
| 102 | |||
| 103 | =head1 NAME | ||
| 104 | |||
| 105 | FindApp::Loader - FIXME | ||
| 106 | |||
| 107 | =head1 SYNOPSIS | ||
| 108 | |||
| 109 | use FindApp::Loader; | ||
| 110 | |||
| 111 | =head1 DESCRIPTION | ||
| 112 | |||
| 113 | =head2 Pluskey Attributes | ||
| 114 | |||
| 115 | These are all private; use the methods instead. | ||
| 116 | |||
| 117 | =over | ||
| 118 | |||
| 119 | =item MODULES_USED | ||
| 120 | |||
| 121 | Reference to array of modules actually used. | ||
| 122 | |||
| 123 | =item FILES_REQUIRED | ||
| 124 | |||
| 125 | Reference to array of files actually required. | ||
| 126 | |||
| 127 | =back | ||
| 128 | |||
| 129 | =head2 Methods | ||
| 130 | |||
| 131 | =over | ||
| 132 | |||
| 133 | =item another_file_required | ||
| 134 | |||
| 135 | =item another_module_used | ||
| 136 | |||
| 137 | =item export_lib_to_env | ||
| 138 | |||
| 139 | =item load_files | ||
| 140 | |||
| 141 | =item load_libraries | ||
| 142 | |||
| 143 | =item load_modules | ||
| 144 | |||
| 145 | =item load_wanted | ||
| 146 | |||
| 147 | =item required_files | ||
| 148 | |||
| 149 | =item used_modules | ||
| 150 | |||
| 151 | =back | ||
| 152 | |||
| 153 | =head1 EXAMPLES | ||
| 154 | |||
| 155 | =head1 ENVIRONMENT | ||
| 156 | |||
| 157 | =head1 SEE ALSO | ||
| 158 | |||
| 159 | =over | ||
| 160 | |||
| 161 | =item L<FindApp> | ||
| 162 | |||
| 163 | =back | ||
| 164 | |||
| 165 | =head1 CAVEATS AND PROVISOS | ||
| 166 | |||
| 167 | =head1 BUGS AND LIMITATIONS | ||
| 168 | |||
| 169 | =head1 HISTORY | ||
| 170 | |||
| 171 | =head1 AUTHOR | ||
| 172 | |||
| 173 | Tom Christiansen C<< <tchrist@perl.com> >> | ||
| 174 | |||
| 175 | =head1 LICENCE AND COPYRIGHT | ||
| 176 | |||
| 177 | Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>. | ||
| 178 | All Rights Reserved. | ||
| 179 | |||
| 180 | This module is free software; you can redistribute it and/or | ||
| 181 | modify it under the same terms as Perl itself. See L<perlartistic>. | ||
| 182 | |||
| 183 | =cut | ||
| 184 |