Wrapper/Module.pm

filter: Statementstmt 89.3% 118 / 132 Branchbran 50.0% 21 / 42 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 90.3% 28 / 31 Podpod 100% 2 / 2 totaltotal 80.4% 169 / 210 CC 24 SCAR 11.9
CC
24
Cov
79%
_run
7.7
_set_module_name
6.0
_module_mtime
6.0
CRAP 3.3
« Wrapper.pm ↑ Summary
1 package Inline::Wrapper::Module;
2 #
3 # Inline::* module dynamic loader and useful wrapper routines
4 #
5 # Individual module handler object
6 #
7 # $Id: Module.pm 14 2010-03-10 09:08:18Z infidel $
8 #
9 # POD documentation after __END__
10 #
11  
12 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Carp qw( carp croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use base qw( Inline::Wrapper );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Inline;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 7 ▶use vars qw( $TRUE $FALSE $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 ###
21 ### VARS
22 ###
23  
24 7 ($VERSION) = q$Revision: 14 $ =~ /(\d+)/;
25 7 *TRUE = \1;
26 7 *FALSE = \0;
27  
28 my $PARAMS = {
29 1 ▶ module_name => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
30 1 ▶ lang_ext => sub { $_[0] },
Subroutine __ANON__: called CC=1 SCAR=0.0
31 7 };
32  
33 ###
34 ### INITIALIZER
35 ###
36  
37 sub new
38 1 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod documented
39 1 my( $class, @args ) = @_;
40  
41 1 ▶ croak "Do not use this class directly; used internally by Inline::Wrapper"
Branch: 1 branch
BranchTrueFalseTotal
unless caller eq "Inline::Wrapper" 0 1 1
42 unless( caller eq 'Inline::Wrapper' );
43  
44 1 return( $class->SUPER::new( @args ) );
45 }
46  
47 sub initialize
48 1 ▶{
Subroutine initialize: called CC=5 SCAR=16.6
Pod documented
49 1 my( $self, @args ) = @_;
50  
51 # Check parameters
52 0 ▶ @args = %{ $args[0] } if( ref( $args[0] ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $args[0] eq "HASH" 0 1 1
53 1 ▶ croak "initialize(): \%args must be a hash; read the docs"
Branch: 1 branch
BranchTrueFalseTotal
if @args & 1 0 1 1
54 if( @args & 1 );
55 1 my %args = @args;
56  
57 1 for( keys %args )
58 {
59 2 ▶ next unless( exists( $PARAMS->{lc $_} ) ); # ignore unwanted args
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$PARAMS{lc $_} 0 2 2
60 2 $self->{lc $_} = $PARAMS->{lc $_}->( $args{$_} );
61 2 delete( $args{$_} );
62 }
63  
64 1 $self->_set_function_list( [] );
65 1 $self->_set_last_load_time( 0 );
66  
67 1 return;
68 }
69  
70 ###
71 ### DESTRUCTOMATIC!
72 ###
73  
74 sub DESTROY
75 1 ▶{
Subroutine DESTROY: called CC=1 SCAR=0.0
76 1 my( $self ) = @_;
77  
78 1 $self->_delete_namespace();
79  
80 1 return;
81 }
82  
83 ###
84 ### PRIVATE METHODS
85 ###
86  
87 # Load the self-corresponding sub-language code module.
88 # At this point in time, we should be a complete object.
89 sub _load
90 # "He who fights with monsters should be careful lest he thereby become a
91 # monster..."
92 1 ▶{
Subroutine _load: called CC=2 SCAR=7.1
93 1 my( $self ) = @_;
94  
95 1 my $module_src = $self->_read_module_source();
96 1 my $namespace = $self->_namespace();
97  
98 # Try to bind via Inline::$language
99 1 $self->_delete_namespace();
100 # BugFix: For some reason, the package stash changed between
101 # 5.11.1 and >= 5.11.1 commit-id 81693ff90925b7d196d1f339fa6f85555e38cab7
102 # Needed to add own module name into the grep -v list.
103 1 my $code = sprintf(q#package %s::%s;
104 use Inline;
105 Inline->bind( %s => $module_src );
106 package %s;
107 return( grep { !/^(?:BEGIN|ISA|Inline)$/ }
108 keys %%%s::%s:: )#,
109 __PACKAGE__, $namespace,
110 $self->language(),
111 __PACKAGE__,
112 __PACKAGE__, $namespace );
113  
114 # DEAR LORD, STRING EVAL! RUN AWAY!
115 # http://perlmonks.org/index.pl?node_id=732598
116 1 ▶ my @symbols = eval $code;
Subroutine __ANON__: called CC=1 SCAR=0.0
117 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
118 {
119 0 chomp $@;
120 0 carp "Error compiling " . $self->_module_path() . ": '$@'";
121 0 return();
122 }
123  
124 # Update our state
125 1 $self->_set_function_list( @symbols );
126 1 $self->_set_last_load_time( time );
127  
128 # return loaded symbol list
129 1 return( @symbols );
130 }
131  
132 # Actually run the associated function and return its @retvals
133 sub _run
134 # ".. And if thou gaze long into an abyss, the abyss will also gaze into thee."
135 1 ▶{
Subroutine _run: called CC=7 SCAR=20.4
136 1 my( $self, $funcname, @args ) = @_;
137 1 ▶ croak "run(): $funcname is a required param; read the docs"
Branch: 1 branch
BranchTrueFalseTotal
unless $funcname 0 1 1
138 unless( $funcname );
139  
140 1 ▶ $self->_load() if( $self->_issue_reload() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_issue_reload 0 1 1
141  
142 1 ▶ croak "run(): $funcname not found"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_func_exists($funcname) 0 1 1
143 unless( $self->_func_exists( $funcname ) );
144  
145 # Attempt to pull coderef out of package namespace
146 1 my $namespace = $self->_namespace();
147 1 my $sub = \&{__PACKAGE__ . "::${namespace}::${funcname}"};
148 1 ▶ carp "No such module or function: '$namespace'::'$funcname'", return
Branch: 1 branch
BranchTrueFalseTotal
unless ref $sub eq "CODE" 0 1 1
149 unless( ref( $sub ) eq 'CODE' );
150  
151 # Attempt to execute coderef
152 1 my @retvals = eval { $sub->( @args ) }; # Ahhh, block eval.
153 1 ▶ chomp $@ if( $@ );
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
154 1 ▶ carp "Error executing ${namespace}::${funcname}: $@", return
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
155 if( $@ );
156  
157 1 return( @retvals );
158 }
159  
160 # Fairly self-explanatory.
161 sub _read_module_source
162 1 ▶{
Subroutine _read_module_source: called CC=2 SCAR=7.0
163 1 my( $self ) = @_;
164  
165 1 my $path = $self->_module_path();
166  
167 1 ▶ open( my $fd, '<', $path )
Branch: 1 branch
BranchTrueFalseTotal
open my $fd, "<", $path or carp("$path is inaccessible: $!", (return undef)) 1 0 1
168 or carp "$path is inaccessible: $!", return( undef );
169 1 my $module_src = do { local $/; <$fd> };
170 1 close( $fd );
171  
172 1 return( $module_src );
173 }
174  
175 sub _delete_namespace
176 2 ▶{
Subroutine _delete_namespace: called CC=2 SCAR=6.9
177 2 my( $self ) = @_;
178  
179 2 my $namespace = $self->_namespace();
180 7 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
181 2 my $wiped = delete( ${__PACKAGE__.'::'}{$namespace.'::'} );
182  
183 2 ▶ return( $wiped ? $TRUE : $FALSE );
Branch: 1 branch
BranchTrueFalseTotal
$wiped ? : 1 1 2
184 }
185  
186 ###
187 ### ACCESSORS
188 ###
189  
190 sub _module_name
191 5 ▶{
Subroutine _module_name: called CC=1 SCAR=0.0
192 5 my( $self ) = @_;
193  
194 5 return( $self->{module_name} );
195 }
196  
197 sub _set_module_name
198 0 ▶{
Subroutine _set_module_name: not called CC=2 SCAR=17.9
199 0 my( $self, $modname ) = @_;
200  
201 # Validate
202 0 $modname = $PARAMS->{module_name}->( $modname );
203  
204 return( $modname
205 ? $self->{module_name} = $modname
206 0 ▶ : $self->{module_name} );
Branch: 1 branch
BranchTrueFalseTotal
$modname ? : 0 0 0
207 }
208  
209 sub _function_list
210 1 ▶{
Subroutine _function_list: called CC=1 SCAR=0.0
211 1 my( $self ) = @_;
212  
213 1 return( keys %{ $self->{functions} } );
214 }
215  
216 sub _set_function_list
217 2 ▶{
Subroutine _set_function_list: called CC=2 SCAR=6.9
218 2 my( $self, @funcs ) = @_;
219  
220 2 ▶ @funcs = @{ $funcs[0] } if( ref( $funcs[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $funcs[0] 1 1 2
221  
222 2 return( $self->{functions} = { map { $_ => $TRUE } @funcs } );
223 }
224  
225 sub _func_exists
226 1 ▶{
Subroutine _func_exists: called CC=1 SCAR=0.0
227 1 my( $self, $funcname ) = @_;
228  
229 1 return( exists( $self->{functions}->{$funcname} ) );
230 }
231  
232 sub _last_load_time
233 0 ▶{
Subroutine _last_load_time: not called CC=1 SCAR=6.9
234 0 my( $self ) = @_;
235  
236 0 return( $self->{last_load_time} );
237 }
238  
239 sub _set_last_load_time
240 2 ▶{
Subroutine _set_last_load_time: called CC=2 SCAR=7.2
241 2 my( $self, $load_time ) = @_;
242  
243 return( $load_time =~ /^\d+$/
244 ? $self->{last_load_time} = $load_time
245 2 ▶ : $self->{last_load_time} );
Branch: 1 branch
BranchTrueFalseTotal
$load_time =~ /^\d+$/ ? : 2 0 2
246 }
247  
248 ###
249 ### UTILITY ROUTINES
250 ###
251  
252 # Overload the parent classes' _lang_ext, as we've stored this as an attr
253 sub _lang_ext
254 1 ▶{
Subroutine _lang_ext: called CC=1 SCAR=0.0
255 1 my( $self ) = @_;
256  
257 1 return( $self->{lang_ext} );
258 }
259  
260 # Return boolean if source file has been updated
261 sub _issue_reload
262 1 ▶{
Subroutine _issue_reload: called CC=3 SCAR=16.5
263 1 my( $self ) = @_;
264 1 ▶ return( $FALSE ) unless( $self->auto_reload() );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->auto_reload 1 0 1
265  
266 0 my $file_mod_time = $self->_module_mtime();
267  
268 0 ▶ return( $self->_last_load_time < $self->_module_mtime ? $TRUE : $FALSE );
Branch: 1 branch
BranchTrueFalseTotal
$self->_last_load_time < $self->_module_mtime ? : 0 0 0
269 }
270  
271 # Return file modificiation time
272 sub _module_mtime
273 0 ▶{
Subroutine _module_mtime: not called CC=2 SCAR=17.9
274 0 my( $self ) = @_;
275  
276 0 my $path = $self->_module_path();
277  
278 0 ▶ return( (stat $path)[9] || 0 );
Condition0%0/2
(stat $path)[9] || 0
l!l
00
MC/DC0%0/1
(stat $path)[9] || 0
(stat $path)[9]
Truth table0%0/2
A
A= (stat $path)[9]
Aresult
11
00
279 }
280  
281 # What is our namespace, based on our object state?
282 # XXX: I don't think this is unique.
283 sub _namespace
284 4 ▶{
Subroutine _namespace: called CC=1 SCAR=0.0
285 4 my( $self ) = @_;
286  
287 4 my $modname = $self->_module_name();
288 4 $modname =~ s#[/\\]#_#;
289  
290 4 return( $modname );
291 }
292  
293 # What is our path, based on our object state?
294 sub _module_path
295 1 ▶{
Subroutine _module_path: called CC=2 SCAR=7.0
296 1 my( $self ) = @_;
297  
298 1 my $modname = $self->_module_name();
299 1 my $file_ext = $self->_lang_ext();
300 1 ▶ my $src_file = ( $modname =~ m/.*\Q$file_ext\E$/ )
Branch: 1 branch
BranchTrueFalseTotal
$modname =~ /.*\Q$file_ext\E$/ ? : 0 1 1
301 ? $modname
302 : $modname . $file_ext;
303 1 my $path = _path_join( $self->base_dir(), $src_file );
304  
305 1 return( $path );
306 }
307  
308 # Generate a joined path from @_
309 sub _path_join
310 1 ▶{
Subroutine _path_join: called CC=3 SCAR=11.7
311 1 ▶ ref( $_[0] ) and shift; # scrap instance ref, if passed
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] and shift() 0 1 1
312  
313 1 ▶ my $pathchar = ( $^O eq 'MSWin32' ) ? "\\" : '/';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 1 1
314 1 return( join( $pathchar, @_ ) );
315 }
316  
317 7 1;
318  
319 __END__
« Wrapper.pm ↑ Summary