Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / perl5 / lib / CGI / Switch.pm
1 package CGI::Switch;
2 use Carp;
3 use strict;
4 use vars qw($VERSION @Pref);
5 $VERSION = '0.06';
6 @Pref = qw(CGI::Apache CGI); #default
7
8 sub import {
9     my($self,@arg) = @_;
10     @Pref = @arg if @arg;
11 }
12
13 sub new {
14     shift;
15     my($file,$pack);
16     for $pack (@Pref) {
17         ($file = $pack) =~ s|::|/|g;
18         eval { require "$file.pm"; };
19         if ($@) {
20 #XXX        warn $@;
21             next;
22         } else {
23 #XXX        warn "Going to try $pack\->new\n";
24             my $obj;
25             eval {$obj = $pack->new(@_)};
26             if ($@) {
27 #XXX            warn $@;
28             } else {
29                 return $obj;
30             }
31         }
32     }
33     Carp::croak "Couldn't load+construct any of @Pref\n";
34 }
35
36 1;
37 __END__
38
39 =head1 NAME
40
41 CGI::Switch - Try more than one constructors and return the first object available
42
43 =head1 SYNOPSIS
44
45  
46  use CGISwitch;
47
48   -or-
49
50  use CGI::Switch This, That, CGI::XA, Foo, Bar, CGI;
51
52  my $q = new CGI::Switch;
53
54 =head1 DESCRIPTION
55
56 Per default the new() method tries to call new() in the three packages
57 Apache::CGI, CGI::XA, and CGI. It returns the first CGI object it
58 succeeds with.
59
60 The import method allows you to set up the default order of the
61 modules to be tested.
62
63 =head1 SEE ALSO
64
65 perl(1), Apache(3), CGI(3), CGI::XA(3)
66
67 =head1 AUTHOR
68
69 Andreas KE<ouml>nig E<lt>a.koenig@mind.deE<gt>
70
71 =cut