| 1 | package Drogo::LoadModules; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use File::Find; | ||
| 7 | use Cwd; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | Drogo::LoadModules - Lightweight module loader | ||
| 12 | |||
| 13 | =head1 SYNOPSIS | ||
| 14 | |||
| 15 | use Drogo::LoadModules( | ||
| 16 | path => [ '/var/foo/weasel', './lib', '../dragons/time/lib' ], | ||
| 17 | skip => [ 'BadVoodoo.pm' ], | ||
| 18 | ); | ||
| 19 | |||
| 20 | =cut | ||
| 21 | |||
| 22 | sub import | ||
| 23 | { | ||
| 24 | my ($self, %params) = @_; | ||
| 25 | |||
| 26 | my @paths = @{ $params{path} || [] }; | ||
| 27 | my @skips = @{ $params{skip} || [] }; | ||
| 28 | |||
| 29 | for my $path (@paths) | ||
| 30 | { | ||
| 31 | my $caller_dir = ''; | ||
| 32 | if ($path =~ /^\//) | ||
| 33 | { | ||
| 34 | $caller_dir = $path; | ||
| 35 | } | ||
| 36 | else | ||
| 37 | { | ||
| 38 | my @caller_dir = split('/', (caller)[1]); | ||
| 39 | pop @caller_dir; # trash file name | ||
| 40 | |||
| 41 | $caller_dir = join('/', @caller_dir) . '/' . $path; | ||
| 42 | } | ||
| 43 | |||
| 44 | $caller_dir = Cwd::abs_path($caller_dir); | ||
| 45 | |||
| 46 | die "$caller_dir does not exist" unless -d $caller_dir; | ||
| 47 | |||
| 48 | my @files; | ||
| 49 | File::Find::find({ | ||
| 50 | wanted => sub { push @files, $_ }, | ||
| 51 | no_chdir => 1, | ||
| 52 | }, $caller_dir); | ||
| 53 | |||
| 54 | FILE: for my $rel_file (@files) | ||
| 55 | { | ||
| 56 | # do nothing with directories | ||
| 57 | next if -d $rel_file; | ||
| 58 | |||
| 59 | my $file = Cwd::abs_path($rel_file); | ||
| 60 | |||
| 61 | # only include perl files | ||
| 62 | next unless $file =~ /\.pm$/; | ||
| 63 | next unless -e $file; | ||
| 64 | |||
| 65 | my @path = split('/', $file); | ||
| 66 | my $filename = $path[-1]; | ||
| 67 | |||
| 68 | # skip hidden files | ||
| 69 | next if $filename =~ /^\./; | ||
| 70 | next if $filename =~ /^\#/; | ||
| 71 | |||
| 72 | # should the file be slipped | ||
| 73 | for my $skip (@skips) | ||
| 74 | { | ||
| 75 | next FILE if $file =~ /$skip/; | ||
| 76 | } | ||
| 77 | |||
| 78 | (my $local_file = $file) =~ s/$caller_dir\///; | ||
| 79 | my (undef, @rest_path) = split('/', $local_file); | ||
| 80 | my $soft_path = join('/', @rest_path); | ||
| 81 | |||
| 82 | # don't reload this if the file is already in %INC | ||
| 83 | next if $INC{$local_file}; | ||
| 84 | next if $INC{$soft_path}; | ||
| 85 | next if $INC{$file}; | ||
| 86 | next if $INC{$rel_file}; | ||
| 87 | |||
| 88 | # actually require it | ||
| 89 | require $file; | ||
| 90 | |||
| 91 | # build local paths to avoid reloading files | ||
| 92 | $INC{$soft_path} = $file; | ||
| 93 | $INC{$local_file} = $file; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | } | ||
| 97 | |||
| 98 | =head1 AUTHORS | ||
| 99 | |||
| 100 | Bizowie <http://bizowie.com> | ||
| 101 | |||
| 102 | =head1 COPYRIGHT AND LICENSE | ||
| 103 | |||
| 104 | Copyright (C) 2013 Bizowie | ||
| 105 | |||
| 106 | This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | 1; |