File Coverage

blib/lib/Rex/Require.pm
Criterion Covered Total %
statement 33 34 97.0
branch 7 8 87.5
condition 2 2 100.0
subroutine 7 7 100.0
pod 0 3 0.0
total 49 54 90.7


line stmt bran cond sub pod time code
1             #
2             # (c) Jan Gehring
3             #
4              
5             package Rex::Require;
6              
7 105     105   71561 use v5.12.5;
  105         396  
8 105     105   609 use warnings;
  105         206  
  105         5036  
9              
10             our $VERSION = '1.14.2.3'; # TRIAL VERSION
11              
12 105     105   647 use Carp;
  105         227  
  105         7471  
13             require Rex::Logger;
14              
15             # some code borrowed from: UNIVERSAL::require (neilb)
16              
17 105     105   58405 BEGIN { require UNIVERSAL; }
18              
19             my $module_name_rx = qr/[A-Z_a-z][0-9A-Z_a-z]*(?:::[0-9A-Z_a-z]+)*/;
20              
21             sub UNIVERSAL::require {
22 399     399 0 3549 my ( $module, %option ) = @_;
23              
24 399   100     3867 $option{level} ||= 0;
25              
26 399         7917 my ( $caller_package, $caller_file, $caller_line ) = caller( $option{level} );
27              
28 399         2718 my $file = $module . ".pm";
29 399         3768 $file =~ s/::/\//g;
30              
31             # check if module is already loaded.
32 399 100       2228 return eval { 1 } if $INC{$file};
  140         561  
33              
34 259         28048 my $ret = eval "CORE::require(\$file)";
35              
36 259 100       1404455 if ( !$ret ) {
37 105         111947 confess $@;
38             }
39              
40 154         1208 return $ret;
41             }
42              
43             sub UNIVERSAL::use {
44 67     67 0 904 my ( $module, @imports ) = @_;
45              
46 67         1961 $module->require( level => 1 );
47              
48 66         791 my ( $caller_package, $caller_file, $caller_line ) = caller(0);
49              
50 66         7240 eval "package $caller_package;\n\$module->import(\@imports);\n1;";
51              
52 66 50       692 if ($@) {
53 0         0 confess $@;
54             }
55              
56 66         1309 return 1;
57             }
58              
59             sub UNIVERSAL::is_loadable {
60 118     118 0 420 my ($module) = @_;
61 118         254 eval {
62 118         829 $module->require;
63 117         210 1;
64             };
65              
66 118 100       633 if ($@) { return 0; }
  1         1994  
67 117         946 return 1;
68             }
69              
70             1;