File Coverage

blib/lib/Bot/Cobalt/Core/Loader.pm
Criterion Covered Total %
statement 31 53 58.4
branch 5 18 27.7
condition 1 6 16.6
subroutine 9 13 69.2
pod 3 5 60.0
total 49 95 51.5


line stmt bran cond sub pod time code
1             package Bot::Cobalt::Core::Loader;
2             $Bot::Cobalt::Core::Loader::VERSION = '0.021003';
3 7     7   14346 use strictures 2;
  7         1091  
  7         247  
4 7     7   961 use Carp;
  7         12  
  7         368  
5 7     7   29 use Scalar::Util 'blessed';
  7         8  
  7         256  
6              
7 7     7   425 use Try::Tiny;
  7         1668  
  7         2620  
8              
9 0     0 0 0 sub new { bless [], shift }
10              
11             sub is_reloadable {
12 1     1 1 1333 my ($class, $obj) = @_;
13              
14 1 50 33     202 confess "is_reloadable() needs a plugin object"
15             unless $obj and blessed $obj;
16              
17 0 0 0     0 $obj->can('NON_RELOADABLE') && $obj->NON_RELOADABLE ?
18             undef : 1
19             }
20              
21             sub module_path {
22 1     1 0 1 my ($class, $module) = @_;
23              
24 1 50       3 confess "module_path() needs a module name" unless defined $module;
25              
26 1         8 join('/', split /::/, $module).".pm";
27             }
28              
29             sub load {
30 1     1 1 42 my ($class, $module, @newargs) = @_;
31              
32 1 50       4 confess "load() needs a module name" unless defined $module;
33              
34 1         5 my $modpath = $class->module_path($module);
35              
36 1         8 local @INC = @INC;
37 1 50       6 pop @INC if $INC[-1] eq '.';
38              
39 1         2 my $orig_err;
40 1 50   1   9 unless (try { require $modpath;1 } catch { $orig_err = $_;0 }) {
  1         603  
  0         0  
  1         20  
  1         8  
41             ## die informatively
42 1         175 croak "Could not load $module: $orig_err";
43             ## Okay, so we require 5.12.1+ and this only happens on <=5.8 ...
44             ## ... but it's worth noting in case this code is ported to older
45             ## perls. $INC{$modpath} is set even if we died, so long as the file
46             ## exists in our INC path.
47 0           delete $INC{$modpath};
48             }
49              
50 0           my $obj;
51             my $err; try {
52 0     0     $obj = $module->new(@newargs)
53             } catch {
54 0     0     $err = "new() failed for $module: $_";
55             undef
56 0 0         } or confess $err;
  0            
57              
58 0           $obj
59             }
60              
61             sub unload {
62 0     0 1   my ($class, $module) = @_;
63              
64 0 0         confess "unload() needs a module name" unless defined $module;
65              
66 0           my $modpath = $class->module_path($module);
67              
68 0           delete $INC{$modpath};
69              
70             {
71 7     7   32 no strict 'refs';
  7         11  
  7         779  
  0            
72 0           @{$module.'::ISA'} = ();
  0            
73              
74 0           my $s_table = $module.'::';
75 0           for my $symbol (keys %$s_table) {
76 0 0         next if $symbol =~ /^[^:]+::$/;
77 0           delete $s_table->{$symbol}
78             }
79             }
80              
81             ## Pretty much always returns success, on the theory that
82             ## we did all we could from here.
83             1
84 0           }
85              
86             1;
87             __END__