File Coverage

blib/lib/Dancer/ModuleLoader.pm
Criterion Covered Total %
statement 41 41 100.0
branch 11 22 50.0
condition 1 2 50.0
subroutine 9 9 100.0
pod 5 5 100.0
total 67 79 84.8


line stmt bran cond sub pod time code
1             package Dancer::ModuleLoader;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             #ABSTRACT: dynamic module loading helpers for Dancer core components
4             $Dancer::ModuleLoader::VERSION = '1.3514_04'; # TRIAL
5             $Dancer::ModuleLoader::VERSION = '1.351404';
6             # Abstraction layer for dynamic module loading
7              
8 199     199   1232927 use strict;
  199         538  
  199         4646  
9 199     199   814 use warnings;
  199         317  
  199         4530  
10 199     199   83030 use Module::Runtime qw/ use_module /;
  199         296167  
  199         1046  
11              
12             sub load {
13 915     915 1 179505 my ($class, $module, $version) = @_;
14              
15 915         2602 my ($res, $error) = $class->require($module, $version);
16 915 100       12330 return wantarray ? ($res, $error) : $res;
17             }
18              
19             sub require {
20 955     955 1 3139 my ($class, $module, $version) = @_;
21 955 50       1506 eval { defined $version ? use_module( $module, $version )
  955 100       3760  
    100          
22             : use_module( $module ) }
23             or return wantarray ? (0, $@) : 0;
24 934         1391619 return 1; #success
25             }
26              
27             sub load_with_params {
28 39     39 1 715 my ($class, $module, @args) = @_;
29 39         113 my ($res, $error) = $class->require($module);
30 39 0       138 $res or return wantarray ? (0, $error) : 0;
    50          
31              
32             # From perlfunc : If no "import" method can be found then the call is
33             # skipped, even if there is an AUTOLOAD method.
34 39 50       349 if ($module->can('import')) {
35              
36             # bump Exporter Level to import symbols in the caller
37 39   50     201 local $Exporter::ExportLevel = ($Exporter::ExportLevel || 0) + 1;
38 39         75 local $@;
39 39         76 eval { $module->import(@args) };
  39         142  
40 39         189322 my $error = $@;
41 39 0       144 $error and return wantarray ? (0, $error) : 0;
    50          
42             }
43 39         202 return 1;
44             }
45              
46             sub use_lib {
47 196     196 1 668 my ($class, @args) = @_;
48 199     199   114984 use lib;
  199         104857  
  199         998  
49 196         460 local $@;
50 196         1500 lib->import(@args);
51 196         21189 my $error = $@;
52 196 0       699 $error and return wantarray ? (0, $error) : 0;
    50          
53 196         709 return 1;
54             }
55              
56             sub class_from_setting {
57 441     441 1 1290 my ($self, $namespace, $setting) = @_;
58              
59 441         909 my $class = '';
60 441         1594 for my $token (split /_/, $setting) {
61 442         1216 $class .= ucfirst($token);
62             }
63 441         1781 return "${namespace}::${class}";
64             }
65              
66             1;
67              
68             __END__