File Coverage

blib/lib/CGI/Application/Plugin/AnyCGI.pm
Criterion Covered Total %
statement 12 57 21.0
branch 0 20 0.0
condition 0 5 0.0
subroutine 4 7 57.1
pod 1 1 100.0
total 17 90 18.8


line stmt bran cond sub pod time code
1             ## -------------------------------------------------------------------
2             ## C::A::Plugin
3             ##--------------------------------------------------------------------
4            
5             package CGI::Application::Plugin::AnyCGI;
6 1     1   54839 use strict;
  1         2  
  1         43  
7 1     1   6 use warnings;
  1         2  
  1         362  
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             $CGI::Application::Plugin::AnyCGI::VERSION = '0.02';
23            
24             ## to enable debugging, set this to "1" or any other "true" value
25             $CGI::Application::Plugin::AnyCGI::DEBUG = 0;
26            
27             our ( @ISA, $AUTOLOAD );
28            
29             =pod
30            
31             =head1 SYNOPSIS
32            
33             In your L-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. Just give a list of your preferred modules by
52             using the C option with L(). 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 list can be loaded, the
56             Plugin silently loads L 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. 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 provides. The one
70             and only parameter C uses is C.
71            
72             =head3 Calling new() without any further options
73            
74             If no additional options are passed, C 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 method to
77             it's parent.)
78            
79             B
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 or C (or, as a
87             final fallback, of C).
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) to the
93             constructor. C then imports it's C 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, passing some params for
104             initializing, and returns this instance to the caller.
105            
106             B As the different CGI::* modules don't take the same
107             arguments to C, 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             sub new {
119 0     0 1   my $caller = shift;
120 0   0       my $class = ref($caller) || $caller;
121            
122 0           my %args = (
123             cgi_modules => [ 'CGI::Minimal' ],
124             @_
125             );
126            
127 0           my $module;
128             my $loaded;
129            
130 0           TRY:
131             {
132 0           foreach $module ( @{$args{'cgi_modules'}} ) {
  0            
133            
134 0 0         $CGI::Application::Plugin::AnyCGI::DEBUG and
135             __PACKAGE__->_debug( "Trying module $module" );
136            
137 0           eval "use $module";
138            
139 0 0         if ( ! $@ ) {
140 0           push @ISA, $module;
141 0           $loaded = $module;
142 0 0         $CGI::Application::Plugin::AnyCGI::DEBUG and
143             __PACKAGE__->_debug( "Loaded module $module" );
144 0           last TRY;
145             }
146            
147             }
148             } # TRY:
149            
150 0 0         unless ( $loaded ) {
151             ## Fallback to CGI.pm (included in Perl Core)
152 0 0         $CGI::Application::Plugin::AnyCGI::DEBUG and
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 0         __PACKAGE__->_debug( "CGI module loaded: " . $loaded );
161            
162 0           delete $args{'cgi_modules'};
163            
164 0 0         if ( %args ) {
165 0           my $self = $loaded->new( %args );
166 1     1   7 no strict 'refs';
  1         12  
  1         401  
167 0           *{ $loaded . '::AUTOLOAD' } = *CGI::Application::Plugin::AnyCGI::AUTOLOAD;
  0            
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 calling C:
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             sub _debug {
219 0     0     my $self = shift;
220 0           my $msg = shift;
221            
222 0           my $dump;
223 0 0         if ( @_ ) {
224 0 0         if ( scalar ( @_ ) % 2 == 2 ) {
225 0           %{ $dump } = ( @_ );
  0            
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   0       $sub ||= '-';
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             sub AUTOLOAD {
260 0     0     my $self = shift;
261 0           my ($method) = $AUTOLOAD =~ /^.*::(.*)$/;
262 0 0         return if ( $method =~ /^DESTROY$/ );
263 1     1   6 no strict 'refs';
  1         2  
  1         125  
264 0           eval "use CGI qw/$method/";
265 0           &$method(@_);
266             } # --- end sub AUTOLOAD ---
267            
268             1;
269            
270             __END__