1 #============================================================================
5 # Perl5 module to provide a CGI interface to AppConfig. Internal variables
6 # may be set through the CGI "arguments" appended to a URL.
8 # Written by Andy Wardley <abw@wardley.org>
10 # Copyright (C) 1997-2003 Andy Wardley. All Rights Reserved.
11 # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
13 #============================================================================
15 package AppConfig::CGI;
19 our $VERSION = '1.65';
22 #------------------------------------------------------------------------
25 # Module constructor. The first, mandatory parameter should be a
26 # reference to an AppConfig::State object to which all actions should
27 # be applied. The second parameter may be a string containing a CGI
28 # QUERY_STRING which is then passed to parse() to process. If no second
29 # parameter is specifiied then the parse() process is skipped.
31 # Returns a reference to a newly created AppConfig::CGI object.
32 #------------------------------------------------------------------------
38 STATE => $state, # AppConfig::State ref
39 DEBUG => $state->_debug(), # store local copy of debug
40 PEDANTIC => $state->_pedantic, # and pedantic flags
44 # call parse(@_) to parse any arg list passed
52 #------------------------------------------------------------------------
55 # Method used to parse a CGI QUERY_STRING and set internal variable
56 # values accordingly. If a query is not passed as the first parameter,
57 # then _get_cgi_query() is called to try to determine the query by
58 # examing the environment as per CGI protocol.
60 # Returns 0 if one or more errors or warnings were raised or 1 if the
61 # string parsed successfully.
62 #------------------------------------------------------------------------
68 my ($variable, $value, $nargs);
71 # take a local copy of the state to avoid much hash dereferencing
72 my ($state, $debug, $pedantic) = @$self{ qw( STATE DEBUG PEDANTIC ) };
74 # get the cgi query if not defined
75 $query = $ENV{ QUERY_STRING }
76 unless defined $query;
79 return 1 unless defined $query;
81 # we want to install a custom error handler into the AppConfig::State
82 # which appends filename and line info to error messages and then
83 # calls the previous handler; we start by taking a copy of the
85 my $errhandler = $state->_ehandler();
87 # install a closure as a new error handler
90 # modify the error message
92 $format =~ s/</</g;
93 $format =~ s/>/>/g;
94 $format = "<p>\n<b>[ AppConfig::CGI error: </b>$format<b> ] </b>\n<p>\n";
95 # send error to stdout for delivery to web client
101 PARAM: foreach (split('&', $query)) {
103 # extract parameter and value from query token
104 ($variable, $value) = map { _unescape($_) } split('=');
106 # check an argument was provided if one was expected
107 if ($nargs = $state->_argcount($variable)) {
108 unless (defined $value) {
109 $state->_error("$variable expects an argument");
111 last PARAM if $pedantic;
115 # default an undefined value to 1 if ARGCOUNT_NONE
117 $value = 1 unless defined $value;
120 # set the variable, noting any error
121 unless ($state->set($variable, $value)) {
123 last PARAM if $pedantic;
127 # restore original error handler
128 $state->_ehandler($errhandler);
130 # return $warnings => 0, $success => 1
131 return $warnings ? 0 : 1;
136 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
137 # The following sub-routine was lifted from Lincoln Stein's CGI.pm
138 # module, version 2.36. Name has been prefixed by a '_'.
140 # unescape URL-encoded data
143 $todecode =~ tr/+/ /; # pluses become spaces
144 $todecode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
149 # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
160 AppConfig::CGI - Perl5 module for processing CGI script parameters.
166 my $state = AppConfig::State->new(\%cfg);
167 my $cgi = AppConfig::CGI->new($state);
169 $cgi->parse($cgi_query);
170 $cgi->parse(); # looks for CGI query in environment
174 AppConfig::CGI is a Perl5 module which implements a CGI interface to
175 AppConfig. It examines the QUERY_STRING environment variable, or a string
176 passed explicitly by parameter, which represents the additional parameters
177 passed to a CGI query. This is then used to update variable values in an
178 AppConfig::State object accordingly.
180 AppConfig::CGI is distributed as part of the AppConfig bundle.
184 =head2 USING THE AppConfig::CGI MODULE
186 To import and use the AppConfig::CGI module the following line should appear
191 AppConfig::CGI is used automatically if you use the AppConfig module
192 and create an AppConfig::CGI object through the cgi() method.
193 AppConfig::CGI is implemented using object-oriented methods. A new
194 AppConfig::CGI object is created and initialised using the new()
195 method. This returns a reference to a new AppConfig::CGI object. A
196 reference to an AppConfig::State object should be passed in as the
199 my $state = AppConfig::State->new();
200 my $cgi = AppConfig::CGI->new($state);
202 This will create and return a reference to a new AppConfig::CGI object.
204 =head2 PARSING CGI QUERIES
206 The C<parse()> method is used to parse a CGI query which can be specified
207 explicitly, or is automatically extracted from the "QUERY_STRING" CGI
208 environment variable. This currently limits the module to only supporting
211 See AppConfig for information about using the AppConfig::CGI
212 module via the cgi() method.
216 Andy Wardley, C<E<lt>abw@wardley.org<gt>>
220 Copyright (C) 1997-2007 Andy Wardley. All Rights Reserved.
222 Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
224 This module is free software; you can redistribute it and/or modify it
225 under the same terms as Perl itself.
229 AppConfig, AppConfig::State