File Coverage

blib/lib/Bot/Cobalt/Core/Loader.pm
Criterion Covered Total %
statement 29 51 56.8
branch 4 16 25.0
condition 1 6 16.6
subroutine 9 13 69.2
pod 3 5 60.0
total 46 91 50.5


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