blib/lib/WebDyne/Chain.pm

filter: Statementstmt 70.9% 134 / 189 Branchbran 46.1% 24 / 52 Conditioncond 33.3% 7 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 82.3% 14 / 17 Podpod 0.0% 0 / 2 totaltotal 60.8% 179 / 294 CC 38 SCAR 32.5
CC
38
Cov
63%
AUTOLOAD
44.2
handler
34.8
import
12.0
CRAP 25.7
« blib/lib/WebDyne/Request/Apache.pm ↑ Summary blib/lib/WebDyne/CGI/Simple.pm »
1 #
2 # This file is part of WebDyne.
3 #
4 # This software is copyright (c) 2026 by Andrew Speer <andrew.speer@isolutions.com.au>.
5 #
6 # This is free software; you can redistribute it and/or modify it under
7 # the same terms as the Perl 5 programming language system itself.
8 #
9 # Full license text is available at:
10 #
11 # <http://dev.perl.org/licenses/>
12 #
13 package WebDyne::Chain;
14  
15  
16 # Compiler Pragma
17 #
18 1 ▶use strict qw(vars);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 # WebDyne Modules.
25 #
26 1 ▶use WebDyne;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 1 ▶use WebDyne::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 1 ▶use WebDyne::Chain::Constant;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use WebDyne::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31  
32 # Other modules
33 #
34 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36  
37 # Version information in a formate suitable for CPAN etc. Must be
38 # all on one line
39 #
40 1 $VERSION='3.029';
41  
42  
43 # Debug using WebDyne debug handler
44 #
45 1 0 && debug("%s loaded, version $VERSION", __PACKAGE__);
46  
47  
48 # Shortcut error handler, save using ISA;
49 #
50 1 require WebDyne::Err;
51 1 ▶*err_html=\&WebDyne::Err::err_html || *err_html;
Condition33%1/3
\&WebDyne::Err::err_html || *err_html
l!l&&r!l&&!r
100
MC/DC0%0/2
\&WebDyne::Err::err_html || *err_html
\&WebDyne::Err::err_html *err_html
Truth table33%1/3
A || B
A= \&WebDyne::Err::err_htmlB= *err_html
ABresult
1X1
011
000
52  
53  
54 # Package wide hash ref for data storage
55 #
56 1 my %Package;
57  
58  
59 # Make all errors non-fatal
60 #
61 1 errnofatal(1);
62  
63  
64 # And done
65 #
66 1 1;
67  
68  
69 #------------------------------------------------------------------------------
70  
71  
72 # Packace init, attempt to load optional Time::HiRes module
73 #
74 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 eval {require Time::HiRes; Time::HiRes->import('time')};
76 1 eval {require Devel::Confess; Devel::Confess->import(qw(no_warnings))};
77 }
78  
79  
80 0 ▶sub import {
Subroutine import: not called CC=3 SCAR=24.8
81  
82  
83 # Will only work if called from within a __PERL__ block in WebDyne
84 #
85 0 my ($class, @import)=@_;
86 0 ▶ my $self_cr=UNIVERSAL::can(scalar caller, 'self') || return;
Condition0%0/2
UNIVERSAL::can(scalar caller, 'self') || (return)
l!l
00
MC/DC0%0/1
UNIVERSAL::can(scalar caller, 'self') || (return)
UNIVERSAL::can(scalar caller, 'self')
Truth table0%0/2
A
A= UNIVERSAL::can(scalar caller, 'self')
Aresult
11
00
87 0 ▶ my $self=$self_cr->() || return;
Condition0%0/2
&$self_cr() || (return)
l!l
00
MC/DC0%0/1
&$self_cr() || (return)
&$self_cr()
Truth table0%0/2
A
A= &$self_cr()
Aresult
11
00
88 0 $self->set_handler($class);
89 0 my $meta_hr=$self->meta();
90 0 push @{$meta_hr->{'webdynechain'}}, @import;
91  
92  
93 }
94  
95  
96 2 ▶sub handler : method {
Subroutine handler: called CC=15 SCAR=35.5
Pod undocumented
97  
98  
99 # Get class, request object
100 #
101 2 my ($self, $r, $param_hr)=@_;
102 2 ▶ my $class=ref($self) || do {
Condition33%1/3
ref $self || do { my(%self) = ('_time', &WebDyne::Chain::time(), '_r', $r, %{delete $self->{'_self'};}); $self = bless(\%self, $self); ref $self }
l!l&&r!l&&!r
200
MC/DC0%0/2
ref $self || do { my(%self) = ('_time', &WebDyne::Chain::time(), '_r', $r, %{delete $self->{'_self'};}); $self = bless(\%self, $self); ref $self }
ref $self do { my(%self) = ('_time', &WebDyne::Chain::time(), '_r', $r, %{delete $self->{'_self'};}); $self = bless(\%self, $self); ref $self }
Truth table33%1/3
A || B
A= ref $selfB= do { my(%self) = ('_time', &WebDyne::Chain::time(), '_r', $r, %{delete $self->{'_self'};}); $self = bless(\%self, $self); ref $self }
ABresult
1X1
011
000
103  
104  
105 # Need new self ref
106 #
107 my %self=(
108  
109 _time => time(),
110 _r => $r,
111 0 %{delete $self->{'_self'}},
112  
113 );
114 0 $self=bless \%self, $self;
115 0 ref($self);
116  
117  
118 };
119  
120  
121 # Setup error handlers
122 #
123 0 ▶ local $SIG{__DIE__}=sub {return $self->err_html(@_) unless $^S};
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
unless $^S 0 0 0
124 0 ▶ local $SIG{__WARN__}=sub {return $self->err_html(@_)}
Subroutine __ANON__: not called CC=1 SCAR=6.9
125 2 ▶ if $WEBDYNE_WARNINGS_FATAL;
Branch: 1 branch
BranchTrueFalseTotal
if $WebDyne::Chain::WEBDYNE_WARNINGS_FATAL 0 2 2
126  
127  
128 # Debug
129 #
130 2 0 && debug(
131 "in WebDyne::Chain::handler, class $class, r $r, self $self, param_hr %s",
132 Dumper($param_hr));
133  
134  
135 # Log URI
136 #
137 2 0 && debug("URI %s", $r->uri());
138  
139  
140 # Get string of modules to chain
141 #
142 2 my @module;
143 2 ▶ if (my $module_ar=$param_hr->{'meta'}{'webdynechain'}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $module_ar = $param_hr->{'meta'}{'webdynechain'}) { } 2 0 2
elsif (my $module = $r->dir_config('WebDyneChain')) { } 0 0 0
144 2 0 && debug("using module_ar $module_ar %s from meta", Dumper($module_ar));
145 2 @module=@{$module_ar};
146 }
147 elsif (my $module=$r->dir_config('WebDyneChain')) {
148 0 0 && debug("using module $module dir_config");
149 0 @module=split(/\s+/, $module);
150 }
151 else {
152 0 0 && debug('could not find any module chain info');
153 }
154 2 0 && debug('module: %s', Dumper(\@module));
155  
156  
157  
158 # WebDyne::Chain must be the first handler in line, Webdyne the last
159 #
160 2 ▶ unshift @module, __PACKAGE__ unless ($module[0] eq +__PACKAGE__);
Branch: 1 branch
BranchTrueFalseTotal
unless $module[0] eq __PACKAGE__ 2 0 2
161 2 ▶ push @module, 'WebDyne' unless ($module[$#module] eq 'WebDyne');
Branch: 1 branch
BranchTrueFalseTotal
unless $module[$#module] eq "WebDyne" 2 0 2
162 2 0 && debug('final module chain %s', join('*', @module));
163  
164  
165 # Store current chain
166 #
167 2 $Package{'_chain_ar'}=\@module;
168  
169  
170 # If only two modules (WebDyne::Chain, WebDyne) something is wrong
171 #
172 2 ▶ if (@module == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@module == 2) 0 2 2
173 return
174 0 $self->err_html('unable to determine module chain - have you set WebDyneChain var ?');
175 }
176  
177  
178 # Get location. Used to use r->location, now use module array to generate pseudo
179 # location data;
180 #
181 2 my $location=join(undef, @module);
182 2 0 && debug("location $location");
183 2 ▶ unless ($Package{'_chain_loaded_hr'}{$location}++) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Package{"_chain_loaded_hr"}{$location}++) 1 1 2
184 1 0 && debug("modules not loaded, doing now");
185 1 local $SIG{'__DIE__'};
186 1 foreach my $package (@module) {
187 3 ▶ eval("require $package") ||
Branch: 1 branch
BranchTrueFalseTotal
eval "require $package" or return $self->err_html("unable to load package $package, " . lcfirst($@)) 3 0 3
188 return $self->err_html("unable to load package $package, " . lcfirst($@));
189 3 0 && debug("loaded $package");
190 }
191 }
192  
193  
194 # If location not same as last time we were run, then unload chain
195 #
196 2 ▶ if ((my $location_current=$Package{'_location_current'}) ne $location) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $location_current = $Package{'_location_current'}) ne $location) { } 1 1 2
197  
198  
199 # Need to unload cached code refs
200 #
201 1 0 && debug("location_current '$location_current' is ne this location ('$location'). restoring cr's");
202 1 &ISA_restore();
203  
204  
205 # Update location
206 #
207 1 $Package{'_location_current'}=$location;
208  
209  
210 # If code ref's cached, load up now
211 #
212 1 ▶ if (my $chain_hr=$Package{'_chain_hr'}{$location}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $chain_hr = $Package{"_chain_hr"}{$location}) 0 1 1
213  
214  
215 # Debug
216 #
217 0 0 && debug("found cached code ref's for location $location loading");
218  
219  
220 # Yes found, load up
221 #
222 0 ▶ while (my ($method, $cr)=each %{$chain_hr}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$chain_hr;}) 0 0 0
223  
224  
225 # Debug
226 #
227 0 0 && debug("loading cr $cr for method $method");
228  
229  
230 # Install code ref
231 #
232 0 *{$method}=$cr;
233  
234 }
235  
236  
237 # Update current pointer
238 #
239 0 $Package{'_chain_current_hr'}=$chain_hr;
240  
241  
242 }
243 }
244 else {
245  
246 1 0 && debug('location chain same as last request, caching');
247  
248 }
249  
250  
251 # Debug
252 #
253 2 0 && debug('module array %s', Dumper(\@module));
254  
255  
256 # All done, pass onto next handler in chain. NOTE no error handler (eg || $self->err_html). It is
257 # not our job to check for errors here, we should just pass back whatever the next handler does.
258 #
259 2 return $self->SUPER::handler($r, @_[2..$#_]);
260  
261  
262 # Only get here if error handler invoked
263 #
264 0 RENDER_ERROR:
265 return $self->err_html();
266  
267  
268 # Only get here if subrequest invoked.
269 0 ▶ HANDLER_COMPLETE:
Branch: 1 branch
BranchTrueFalseTotal
$WebDyne::Chain::MP2 ? : 0 0 0
270 return $MP2 ? &Apache::OK : HTTP_OK;
271  
272  
273 }
274  
275  
276 1 ▶sub ISA_restore {
Subroutine ISA_restore: called CC=2 SCAR=8.4
Pod undocumented
277  
278  
279 # Get cuurent chain hash
280 #
281 1 my $chain_hr=delete $Package{'_chain_current_hr'};
282 1 0 && debug('in ISA_restore, chain %s', Dumper($chain_hr));
283  
284  
285 # Go through each module, restoring
286 #
287 1 foreach my $method (keys %{$chain_hr}) {
288  
289  
290 # Free up
291 #
292 0 0 && debug("free $method");
293 0 undef *{$method};
294  
295  
296 }
297  
298  
299 }
300  
301  
302 2 ▶sub DESTROY {
Subroutine DESTROY: called CC=3 SCAR=11.0
303  
304  
305 # Get chain array ref
306 #
307 2 my $self=shift();
308 2 my $chain_ar=$Package{'_chain_ar'};
309 2 0 && debug("self $self, going through DESTROY chain %s", Dumper($chain_ar));
310  
311  
312 # Handle destroys specially, mini version of AUTOLOAD code below
313 #
314 2 foreach my $i (1..$#{$chain_ar}) {
315 4 my $package_chain=$chain_ar->[$i];
316 4 0 && debug("looking for DESTROY $package_chain");
317 4 ▶ if (my $cr=UNIVERSAL::can($package_chain, 'DESTROY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cr = UNIVERSAL::can($package_chain, "DESTROY")) 2 2 4
318 2 0 && debug("DESTROY hit on $package_chain");
319 2 $cr->($self);
320 }
321 }
322  
323  
324 # Destroy object
325 #
326 2 %{$self}=();
327 2 undef $self;
328  
329  
330 }
331  
332  
333 1605 ▶sub UNIVERSAL::AUTOLOAD {
Subroutine AUTOLOAD: called CC=18 SCAR=37.9
334  
335  
336 # Get self ref, calling class, autoloaded method
337 #
338 1605 my $self=$_[0];
339 1605 ▶ my $autoload=$UNIVERSAL::AUTOLOAD || return;
Condition50%1/2
$UNIVERSAL::AUTOLOAD || (return)
l!l
16050
MC/DC0%0/1
$UNIVERSAL::AUTOLOAD || (return)
$UNIVERSAL::AUTOLOAD
Truth table50%1/2
A
A= $UNIVERSAL::AUTOLOAD
Aresult
11
00
340  
341  
342 # Do not handle DESTROY's
343 #
344 1605 ▶ return if $autoload=~/::DESTROY$/;
Branch: 1 branch
BranchTrueFalseTotal
if $autoload =~ /::DESTROY$/ 1584 21 1605
345  
346  
347 # Debug
348 #
349 21 0 && debug(
350 "in UNIVERSAL::AUTOLOAD, self $self, autoload $autoload, caller %s",
351 Dumper([(caller(1))[0..3]]));
352  
353  
354 # Get apache request ref, location. If not present means called by non-WebDyne class, not supported
355 #
356 21 my $r; {
357 21 local $SIG{'__DIE__'}=undef;
358  
359 #unless (eval{ ref($self) && ($r=$self->{'_r'}) }) {
360 21 ▶ unless (eval {ref($self) && ($r=$self->{'_r'})} || UNIVERSAL::can($self, $autoload)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { ref $self and $r = $self->{"_r"} } } or UNIVERSAL::can($self, $autoload)) 0 21 21
Condition33%2/6
eval { do { ref $self and $r = $self->{"_r"} } } or UNIVERSAL::can($self, $autoload)
l!l&&r!l&&!r
2100
ref $self && ($r = $self->{'_r'})
!ll&&!rl&&r
0021
MC/DC0%0/2
eval { do { ref $self and $r = $self->{"_r"} } } or UNIVERSAL::can($self, $autoload)
eval { do { ref $self and $r = $self->{"_r"} } } UNIVERSAL::can($self, $autoload)
MC/DC0%0/2
ref $self && ($r = $self->{'_r'})
ref $self ($r = $self->{'_r'})
Truth table33%1/3
A or B
A= eval { do { ref $self and $r = $self->{"_r"} } }B= UNIVERSAL::can($self, $autoload)
ABresult
1X1
011
000
Truth table33%1/3
A && B
A= ref $selfB= ($r = $self->{'_r'})
ABresult
0X0
100
111
361 0 err ("call to run %s UNIVERSAL::AUTOLOAD for non chained method '$autoload', self ref '$self'.", +__PACKAGE__);
362 0 goto RENDER_ERROR;
363 }
364 }
365  
366  
367 # Get method user was looking for, keep full package name.
368 #
369 21 my ($package_autoload, $method_autoload)=($autoload=~/(.*)::(.*?)$/);
370 21 0 && debug("package_autoload $package_autoload, method_autoload $method_autoload");
371  
372  
373 # And chain for this location
374 #
375 21 my $chain_ar=$Package{'_chain_ar'};
376 21 my $location=join(undef, @{$chain_ar});
377 21 0 && debug('going through chain %s', Dumper($chain_ar));
378  
379  
380 # Caller information
381 #
382 21 my $subroutine_caller=(caller(1))[3];
383 21 my $subroutine_caller_cr=\&{"$subroutine_caller"};
384 21 my ($package_caller, $method_caller)=($subroutine_caller=~/(.*)::(.*?)$/);
385 21 0 && debug("package_caller $package_caller, method_caller $method_caller");
386  
387  
388 # If SUPER method trawl through chain to find the package it was called from, make sure we start
389 # from there in iteration code below
390 #
391 21 my $i=0;
392 21 ▶ if ($autoload=~/\QSUPER::$method_autoload\E$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($autoload =~ /\QSUPER::$method_autoload\E$/) 4 17 21
393 4 0 && debug("SUPER method");
394 4 ▶ for (1; $i < @{$chain_ar}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$chain_ar;}) 6 0 6
395 6 ▶ if (UNIVERSAL::can($chain_ar->[$i], $method_caller) eq $subroutine_caller_cr) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::can($chain_ar->[$i], $method_caller) eq $subroutine_caller_cr) { } 4 2 6
396 4 $i++;
397 4 last;
398 }
399 else {
400 2 0 && debug("miss on package $chain_ar->[$i], $_ ne $subroutine_caller_cr");
401 }
402 }
403 4 0 && debug("loop finished, i $i, chain_ar %s", $#{$chain_ar});
404 }
405  
406  
407 # Iterate through the chain (in order) looking for the method
408 #
409 21 foreach $i ($i..$#{$chain_ar}) {
410  
411  
412 # Can this package in the chain support the calling method ?
413 #
414 53 0 && debug("look for $method_autoload in package $chain_ar->[$i]");
415 53 ▶ if (my $cr=UNIVERSAL::can($chain_ar->[$i], $method_autoload)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cr = UNIVERSAL::can($chain_ar->[$i], $method_autoload)) { } 21 32 53
416  
417  
418 # Yes. Check for loops
419 #
420 21 ▶ if ($cr eq $subroutine_caller_cr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cr eq $subroutine_caller_cr) 0 21 21
421 err (
422 "detected AUTOLOAD loop for method '$method_autoload' " .
423 0 "package $package_caller. Current chain: %s", join(', ', @{$chain_ar}));
424 0 goto RENDER_ERROR;
425 }
426  
427  
428 # Update
429 #
430 21 0 && debug('hit');
431 21 *{$autoload}=$cr;
432  
433  
434 # And keep a record
435 #
436 21 $Package{'_chain_hr'}{$location}{$autoload}=$cr;
437 21 ▶ $Package{'_chain_current_hr'} ||= $Package{'_chain_hr'}{$location};
Condition66%2/3
$Package{"_chain_current_hr"} ||= $Package{"_chain_hr"}{$location}
l!l&&r!l&&!r
2010
MC/DC0%0/2
$Package{"_chain_current_hr"} ||= $Package{"_chain_hr"}{$location}
$Package{"_chain_current_hr"} $Package{"_chain_hr"}{$location}
Truth table66%2/3
A ||= B
A= $Package{"_chain_current_hr"}B= $Package{"_chain_hr"}{$location}
ABresult
1X1
011
000
438  
439  
440 # And dispatch. The commented out code is good for debugging internal
441 # server errors, esp if comment out *{$autoload} above and turn on
442 # debugging
443 #
444 21 goto &{$cr};
445  
446 }
447 else {
448  
449  
450 # Debug
451 #
452 32 0 && debug("unable to find method $method_autoload in package $chain_ar->[$i]");
453  
454 }
455  
456 }
457  
458  
459 # Last resort - look back through call chain
460 #
461 0 0 && debug("checking back through callstack for method $method_autoload");
462 0 my %chain=map {$_ => 1} @{$chain_ar};
463 0 my @caller;
464 0 ▶ for ($i=0; my $caller=(caller($i))[0]; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $caller = (caller $i)[0]) 0 0 0
465 0 ▶ next if $chain{$caller}++; #already looked there
Branch: 1 branch
BranchTrueFalseTotal
if $chain{$caller}++ 0 0 0
466 0 push @caller, $caller;
467 0 ▶ if (my $cr=UNIVERSAL::can($caller, $method_autoload)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cr = UNIVERSAL::can($caller, $method_autoload)) 0 0 0
468 0 ▶ if ($cr eq $subroutine_caller_cr) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cr eq $subroutine_caller_cr) 0 0 0
469 err (
470 "detected AUTOLOAD loop for method '$method_autoload' " .
471 0 "package $package_caller. Current chain: %s", join(', ', @{$chain_ar}));
472 0 goto RENDER_ERROR;
473 }
474 0 ▶ if ($WEBDYNE_AUTOLOAD_POLLUTE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($WebDyne::Chain::WEBDYNE_AUTOLOAD_POLLUTE) 0 0 0
475 0 *{$autoload}=$cr;
476 0 $Package{'_chain_hr'}{$location}{$autoload}=$cr;
477 }
478 0 goto &{$cr}
479 }
480 }
481  
482  
483 # Return err
484 #
485 0 err ("method '$method_autoload' not found in call chain: %s", join(',', @caller));
486 0 goto RENDER_ERROR;
487  
488 }
489 1 1;
490  
491 __END__
« blib/lib/WebDyne/Request/Apache.pm ↑ Summary blib/lib/WebDyne/CGI/Simple.pm »