blib/lib/CGI/Application/Plugin/AnyCGI.pm

filter: Statementstmt 26.2% 16 / 61 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 57.1% 4 / 7 Podpod 100% 1 / 1 totaltotal 21.6% 21 / 97 CC 14 SCAR 39.2
CC
14
Cov
19%
new
92.0
_debug
20.0
AUTOLOAD
3.4
CRAP 50.6
↑ Summary
1 ## -------------------------------------------------------------------
2 ## C::A::Plugin
3 ##--------------------------------------------------------------------
4
5 package CGI::Application::Plugin::AnyCGI;
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 =pod
10
11 =head1 NAME
12
13 CGI::Application::Plugin::AnyCGI - Use your favourite CGI::* module
14 with CGI::Application (instead of CGI.pm)
15
16 =head1 VERSION
17
18 Version 0.02
19
20 =cut
21
22 2 $CGI::Application::Plugin::AnyCGI::VERSION = '0.02';
23
24 ## to enable debugging, set this to "1" or any other "true" value
25 2 $CGI::Application::Plugin::AnyCGI::DEBUG = 0;
26
27 2 our ( @ISA, $AUTOLOAD );
28
29 =pod
30
31 =head1 SYNOPSIS
32
33 In your L<CGI::Application|CGI::Application>-based module:
34
35 use base 'CGI::Application';
36 use CGI::Application::Plugin::AnyCGI;
37
38 sub cgiapp_get_query() {
39 my $self = shift;
40 return CGI::Application::Plugin::AnyCGI->new(
41 cgi_modules => [ qw/ CGI::Minimal CGI::Simple / ],
42 ## any other options given here are passed to the
43 ## loaded CGI::* module
44 );
45 }
46
47
48 =head1 DESCRIPTION
49
50 This module allows to use (nearly) any CGI.pm compatible CGI::* module
51 with L<CGI::Application|CGI::Application>. Just give a list of your preferred modules by
52 using the C<cgi_modules> option with L<new|new>(). The modules are checked
53 in the same order they appear, so see it as a list of fallbacks.
54
55 If none of the modules in the C<cgi_modules> list can be loaded, the
56 Plugin silently loads L<CGI.pm|CGI> as a final fallback.
57
58 If a method is called that is not provided by the module currently in
59 use, it will be silently loaded from L<CGI.pm|CGI>. This may eat up
60 the "performance boost" you could have expected by using any other
61 CGI::* module for your application, but on the other hand you don't
62 have to worry about incompatibilities. ;)
63
64
65 =head1 METHODS
66
67 =head2 new
68
69 This is the only (public) method C<C::A::P::AnyCGI> provides. The one
70 and only parameter C<C::A::P::AnyCGI> uses is C<cgi_modules>.
71
72 =head3 Calling new() without any further options
73
74 If no additional options are passed, C<C::A::P::AnyCGI> returns an
75 instance of itself, with the loaded module pushed at it's @ISA. (So,
76 it acts as an empty subclass, just adding it's C<AUTOLOAD> method to
77 it's parent.)
78
79 B<Example:>
80
81 CGI::Application::Plugin::AnyCGI->new(
82 cgi_modules => [ qw/ CGI::Minimal CGI::Simple / ]
83 );
84
85 ...returns an instance of CGI::Application::Plugin::AnyCGI, which
86 inherits all methods of C<CGI::Minimal> or C<CGI::Simple> (or, as a
87 final fallback, of C<CGI>).
88
89 =head3 Calling new() with further options
90
91 If you pass any options, an instance of the loaded CGI::* module is
92 created, passing all options (except C<cgi_modules>) to the
93 constructor. C<C::A::P::AnyCGI> then imports it's C<AUTOLOAD> method
94 to the loaded module, returning the instance it created.
95
96 Example:
97
98 CGI::Application::Plugin::AnyCGI->new(
99 cgi_modules => [ qw/ CGI::Simple / ],
100 { 'foo'=>'1', 'bar'=>[2,3,4] }
101 );
102
103 ...creates an instance of C<CGI::Simple>, passing some params for
104 initializing, and returns this instance to the caller.
105
106 B<Warning:> As the different CGI::* modules don't take the same
107 arguments to C<new>, this may not work as expected, so it may be better
108 not to use this option.
109
110 =cut
111
112 #-------------------------------------------------------------------
113 # METHOD: new
114 # + author: Bianka Martinovic
115 # + reviewed: Bianka Martinovic
116 # + purpose:
117 #-------------------------------------------------------------------
118 0 ▶sub new {
Subroutine new: not called CC=10 SCAR=45.2
Pod documented
119 0 my $caller = shift;
120 0 ▶ my $class = ref($caller) || $caller;
Condition0%0/3
ref $caller || $caller
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table0%0/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
121
122 0 my %args = (
123 cgi_modules => [ 'CGI::Minimal' ],
124 @_
125 );
126
127 0 my $module;
128 my $loaded;
129
130 TRY:
131 {
132 0 foreach $module ( @{$args{'cgi_modules'}} ) {
133
134 0 ▶ $CGI::Application::Plugin::AnyCGI::DEBUG and
Branch: 1 branch
BranchTrueFalseTotal
$CGI::Application::Plugin::AnyCGI::DEBUG and __PACKAGE__->_debug("Trying module $module") 0 0 0
135 __PACKAGE__->_debug( "Trying module $module" );
136
137 0 eval "use $module";
138
139 0 ▶ if ( ! $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
140 0 push @ISA, $module;
141 0 $loaded = $module;
142 0 ▶ $CGI::Application::Plugin::AnyCGI::DEBUG and
Branch: 1 branch
BranchTrueFalseTotal
$CGI::Application::Plugin::AnyCGI::DEBUG and __PACKAGE__->_debug("Loaded module $module") 0 0 0
143 __PACKAGE__->_debug( "Loaded module $module" );
144 0 last TRY;
145 }
146
147 }
148 } # TRY:
149
150 0 ▶ unless ( $loaded ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($loaded) 0 0 0
151 ## Fallback to CGI.pm (included in Perl Core)
152 0 ▶ $CGI::Application::Plugin::AnyCGI::DEBUG and
Branch: 1 branch
BranchTrueFalseTotal
$CGI::Application::Plugin::AnyCGI::DEBUG and __PACKAGE__->_debug("Fallback to CGI.pm") 0 0 0
153 __PACKAGE__->_debug( "Fallback to CGI.pm" );
154 0 eval "use CGI qw/:standard/";
155 0 push @ISA, 'CGI';
156 0 $loaded = 'CGI';
157 }
158
159 $CGI::Application::Plugin::AnyCGI::DEBUG and
160 0 ▶ __PACKAGE__->_debug( "CGI module loaded: " . $loaded );
Branch: 1 branch
BranchTrueFalseTotal
$CGI::Application::Plugin::AnyCGI::DEBUG and __PACKAGE__->_debug('CGI module loaded: ' . $loaded) 0 0 0
161
162 0 delete $args{'cgi_modules'};
163
164 0 ▶ if ( %args ) {
Branch: 1 branch
BranchTrueFalseTotal
if (%args) { } 0 0 0
165 0 my $self = $loaded->new( %args );
166 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
167 0 *{ $loaded . '::AUTOLOAD' } = *CGI::Application::Plugin::AnyCGI::AUTOLOAD;
168 0 return $self;
169 }
170 else {
171 0 return bless {}, $class;
172 }
173
174 } # --- end sub new ---
175
176
177 #-------------------------------------------------------------------
178 # + + + + + PRIVATE + + + + +
179 #-------------------------------------------------------------------
180
181 =pod
182
183 =head1 DEBUGGING
184
185 This module provides some internal debugging. Any debug messages go to
186 STDOUT, so beware of enabling debugging when running in a web
187 environment. (This will end up with "Internal Server Error"s in most
188 cases.)
189
190 There are two ways to enable the debug mode:
191
192 =over 4
193
194 =item In the module
195
196 Find line
197
198 $CGI::Application::Plugin::AnyCGI::DEBUG = 0;
199
200 and set it to any "true" value. ("1", "TRUE", ... )
201
202 =item From outside the module
203
204 Add this line B<before> calling C<new>:
205
206 $CGI::Application::Plugin::AnyCGI::DEBUG = 1;
207
208 =back
209
210 =cut
211
212 #-------------------------------------------------------------------
213 # METHOD: _debug
214 # + author: Bianka Martinovic
215 # + reviewed: 07-11-14 Bianka Martinovic
216 # + purpose: print out formatted _debug messages
217 #-------------------------------------------------------------------
218 0 ▶sub _debug {
Subroutine _debug: not called CC=4 SCAR=30.0
219 0 my $self = shift;
220 0 my $msg = shift;
221
222 0 my $dump;
223 0 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
224 0 ▶ if ( scalar ( @_ ) % 2 == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@_) % 2 == 2) { } 0 0 0
225 0 %{ $dump } = ( @_ );
226 }
227 else {
228 0 $dump = \@_;
229 }
230 }
231
232 0 my ( $package, $line, $sub ) = (caller())[0,2,3];
233 0 my ( $callerpackage, $callerline, $callersub )
234 = (caller(1))[0,2,3];
235
236 0 ▶ $sub ||= '-';
Condition0%0/2
$sub ||= "-"
l!l
00
MC/DC0%0/1
$sub ||= "-"
$sub
Truth table0%0/2
A
A= $sub
Aresult
11
00
237
238 0 print "\n",
239 join( ' | ', $package, $line, $sub ),
240 "\n\tcaller: ",
241 join( ' | ', $callerpackage, $callerline, $callersub ),
242 "\n\t$msg",
243 "\n\n";
244
245 #if ( $dump ) {
246 # print $self->_dump( $dump );
247 #}
248
249 0 return;
250 } # --- end sub _debug ---
251
252 #-------------------------------------------------------------------
253 # METHOD: AUTOLOAD
254 # + author: Bianka Martinovic
255 # + reviewed: Bianka Martinovic
256 # + purpose: autoloading methods missing in the current CGI module
257 # by using CGI.pm
258 #-------------------------------------------------------------------
259 0 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: not called CC=2 SCAR=12.2
260 0 my $self = shift;
261 0 my ($method) = $AUTOLOAD =~ /^.*::(.*)$/;
262 0 ▶ return if ( $method =~ /^DESTROY$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /^DESTROY$/ 0 0 0
263 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
264 0 eval "use CGI qw/$method/";
265 0 &$method(@_);
266 } # --- end sub AUTOLOAD ---
267
268 2 1;
269
270 __END__
↑ Summary