Paranoid/Module.pm

filter: Statementstmt 95.1% 59 / 62 Branchbran 80.0% 16 / 20 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 1 / 1 totaltotal 92.4% 86 / 93 CC 11 SCAR 19.0
CC
11
Cov
91%
loadModule
11.4
BEGIN
1.0
BEGIN
1.0
CRAP 6.7
« Paranoid/IO/FileMultiplexer/Block/StreamHeader.pm ↑ Summary Paranoid/Glob.pm »
1 # Paranoid::Module -- Paranoid Module Loading Routines
2 #
3 # $Id: lib/Paranoid/Module.pm, 2.10 2022/03/08 00:01:04 acorliss Exp $
4 #
5 # This software is free software. Similar to Perl, you can redistribute it
6 # and/or modify it under the terms of either:
7 #
8 # a) the GNU General Public License
9 # <https://www.gnu.org/licenses/gpl-1.0.html> as published by the
10 # Free Software Foundation <http://www.fsf.org/>; either version 1
11 # <https://www.gnu.org/licenses/gpl-1.0.html>, or any later version
12 # <https://www.gnu.org/licenses/license-list.html#GNUGPL>, or
13 # b) the Artistic License 2.0
14 # <https://opensource.org/licenses/Artistic-2.0>,
15 #
16 # subject to the following additional term: No trademark rights to
17 # "Paranoid" have been or are conveyed under any of the above licenses.
18 # However, "Paranoid" may be used fairly to describe this unmodified
19 # software, in good faith, but not as a trademark.
20 #
21 # (c) 2005 - 2020, Arthur Corliss (corliss@digitalmages.com)
22 # (tm) 2008 - 2020, Paranoid Inc. (www.paranoid.com)
23 #
24 #####################################################################
25  
26 #####################################################################
27 #
28 # Environment definitions
29 #
30 #####################################################################
31  
32 package Paranoid::Module;
33  
34 30 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 30 ▶use vars qw($VERSION @EXPORT @EXPORT_OK %EXPORT_TAGS);
Subroutine BEGIN: called CC=1 SCAR=0.0
39 30 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
40 30 ▶use Paranoid;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 30 ▶use Paranoid::Debug qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 30 ▶use Paranoid::Input;
Subroutine BEGIN: called CC=1 SCAR=0.0
43 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 30 ($VERSION) = ( q$Revision: 2.10 $ =~ /(\d+(?:\.\d+)+)/sm );
46  
47 30 @EXPORT = qw(loadModule);
48 30 @EXPORT_OK = @EXPORT;
49 30 %EXPORT_TAGS = ( all => [@EXPORT_OK], );
50  
51 #####################################################################
52 #
53 # Module code follows
54 #
55 #####################################################################
56  
57 {
58 30 my %modules; # List of modules compiled
59 # {modules} => boolean
60 30 my %imports; # List of modules/tagsets imported from callers
61 # {module*tagset*caller} => boolean
62  
63 44 ▶ sub loadModule {
Subroutine loadModule: called CC=11 SCAR=24.3
Pod documented
64  
65 # Purpose: Attempts to load a module via an eval. Caches the
66 # result
67 # Returns: True (1) if the module was successfully loaded,
68 # False (0) if there are any errors
69 # Usage: $rv = loadModule($moduleName);
70  
71 44 my $module = shift;
72 44 my @args = @_;
73 44 my $rv = 0;
74 44 my $caller = scalar caller;
75 44 ▶ my $a = @args ? 'qw(' . ( join ' ', @args ) . ')' : '';
Branch: 1 branch
BranchTrueFalseTotal
@args ? : 42 2 44
76 44 my ( $m, $cm );
77  
78 44 ▶ croak 'Mandatory first argument must be a defined module name'
Branch: 1 branch
BranchTrueFalseTotal
unless defined $module 0 44 44
79 unless defined $module;
80  
81 44 subPreamble( PDLEVEL1, '$;@', $module, @args );
82  
83 # Check to see if module has been loaded already
84 44 ▶ unless ( exists $modules{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $modules{$module}) 43 1 44
85  
86 # First attempt at loading this module, so
87 # detaint and require
88 43 ▶ if ( detaint( $module, 'filename', $m ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&detaint(\$module, 'filename', \$m)) { } 43 0 43
89 43 $module = $m;
90 } else {
91 0 Paranoid::ERROR =
92 pdebug( 'failed to detaint module name (%s)',
93 PDLEVEL1, $module );
94 0 $modules{$module} = 0;
95 }
96  
97 # Skip if the detainting failed
98 43 ▶ unless ( exists $modules{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $modules{$module}) 43 0 43
99  
100 # Try to load it
101 43 ▶ $modules{$module} = eval "require $module; 1;" ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $module; 1;" ? : 40 3 43
102 pdebug( 'attempted load of %s: %s',
103 43 PDLEVEL2, $module, $modules{$module} );
104  
105 }
106 }
107  
108 # Define the module/tagset/caller
109 44 ▶ if (@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) { } 42 2 44
110 42 ▶ $a = '()' if $a eq 'qw()';
Branch: 1 branch
BranchTrueFalseTotal
if $a eq "qw()" 16 26 42
111 } else {
112 2 $a = '';
113 }
114 44 $cm = "$module*$a*$caller";
115  
116 # Check to see if this caller has imported these symbols
117 # before
118 44 ▶ if ( $modules{$module} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($modules{$module}) 41 3 44
119 41 ▶ if ( exists $imports{$cm} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $imports{$cm}) { } 0 41 41
120  
121 0 pdebug( 'previous attempt to import to %s',
122 PDLEVEL2, $caller );
123  
124 } else {
125  
126 41 pdebug( 'importing symbols into %s', PDLEVEL2, $caller );
127 41 $imports{$cm} = eval << "EOF";
128 {
129 package $caller;
130 import $module $a;
131 1;
132 }
133 EOF
134  
135 }
136  
137 41 $rv = $imports{$cm};
138 }
139  
140 44 subPostamble( PDLEVEL1, '$', $modules{$module} );
141  
142 # Return result
143 44 return $modules{$module};
144 }
145 }
146  
147 30 1;
148  
149 __END__
« Paranoid/IO/FileMultiplexer/Block/StreamHeader.pm ↑ Summary Paranoid/Glob.pm »