2 CGI::Ex - CGI utility suite - makes powerful application writing fun and
6 ### You probably don't want to use CGI::Ex directly
7 ### You probably should use CGI::Ex::App instead.
9 my $cgix = CGI::Ex->new;
11 $cgix->print_content_type;
13 my $hash = $cgix->form;
15 if ($hash->{'bounce'}) {
22 $cgix->location_bounce($new_url_location);
26 if (scalar keys %$form) {
27 my $val_hash = $cgix->conf_read($pathtovalidation);
28 my $err_obj = $cgix->validate($hash, $val_hash);
30 my $errors = $err_obj->as_hash;
31 my $input = "Some content";
33 $cgix->swap_template(\$input, $errors, $content);
34 $cgix->fill({text => \$content, form => $hashref});
45 CGI::Ex provides a suite of utilities to make writing CGI scripts more
46 enjoyable. Although they can all be used separately, the main
47 functionality of each of the modules is best represented in the
48 CGI::Ex::App module. CGI::Ex::App takes CGI application building to the
49 next step. CGI::Ex::App is not quite a framework (which normally
50 includes pre-built html) instead CGI::Ex::App is an extended application
51 flow that dramatically reduces CGI build time in most cases. It does so
52 using as little magic as possible. See CGI::Ex::App.
54 The main functionality is provided by several other modules that may be
55 used separately, or together through the CGI::Ex interface.
58 A Template::Toolkit compatible processing engine. With a few
59 limitations, CGI::Ex::Template can be a drop in replacement for
63 A regular expression based form filler inner (accessed through
64 ->fill or directly via its own functions). Can be a drop in
65 replacement for HTML::FillInForm. See CGI::Ex::Fill for more
69 A form field / cgi parameter / any parameter validator (accessed
70 through ->validate or directly via its own methods). Not quite a
71 drop in for most validators, although it has most of the
72 functionality of most of the validators but with the key additions
73 of conditional validation. Has a tightly integrated JavaScript
74 portion that allows for duplicate client side validation. See
75 CGI::Ex::Validate for more information.
78 A general use configuration, or settings, or key / value file
79 reader. Has ability for providing key fallback as well as immutable
80 key definitions. Has default support for yaml, storable, perl, ini,
81 and xml and open architecture for definition of others. See
82 CGI::Ex::Conf for more information.
85 A highly configurable web based authentication system. See
86 CGI::Ex::Auth for more information.
90 fill is used for filling hash or cgi object values into an existing
91 html document (it doesn't deal at all with how you got the
92 document). Arguments may be given as a hash, or a hashref or
93 positional. Some of the following arguments will only work using
94 CGI::Ex::Fill - most will work with either CGI::Ex::Fill or
95 HTML::FillInForm (assume they are available unless specified
96 otherwise). (See CGI::Ex::Fill for a full explanation of
97 functionality). The arguments to fill are as follows (and in order
101 Text should be a reference to a scalar string containing the
102 html to be modified (actually it could be any reference or
103 object reference that can be modified as a string). It will be
104 modified in place. Another named argument scalarref is available
105 if you would like to copy rather than modify.
108 Form may be a hashref, a cgi style object, a coderef, or an
109 array of multiple hashrefs, cgi objects, and coderefs. Hashes
110 should be key value pairs. CGI objects should be able to call
111 the method param (This can be overrided). Coderefs should expect
112 the field name as an argument and should return a value. Values
113 returned by form may be undef, scalar, arrayref, or coderef
114 (coderef values should expect an argument of field name and
115 should return a value). The code ref options are available to
116 delay or add options to the bringing in of form information -
117 without having to tie the hash. Coderefs are not available in
118 HTML::FillInForm. Also HTML::FillInForm only allows CGI objects
119 if an arrayref is used.
121 NOTE: Only one of the form, fdat, and fobject arguments are
125 The name of the form that the fields should be filled to. The
126 default value of undef, means to fill in all forms in the html.
129 Boolean value defaults to 1. If set to zero - password fields
133 Specify which fields to not fill in. It takes either array ref
134 of names, or a hashref with the names as keys. The hashref
135 option is not available in CGI::Ex::Fill.
137 Other named arguments are available for compatibility with
138 HTML::FillInForm. They may only be used as named arguments.
141 Almost the same as the argument text. If scalarref is used, the
142 filled html will be returned. If text is used the html passed is
146 An array ref of lines of the document. Forces a returned filled
150 An filename that will be opened, filled, and returned.
153 A hashref of key value pairs.
156 A cgi style object or arrayref of cgi style objects used for
157 getting the key value pairs. Should be capable of the ->param
158 method and ->cookie method as document in CGI.
160 See CGI::Ex::Fill for more information about the filling process.
163 Returns the CGI object that is currently being used by CGI::Ex. If
164 none has been set it will automatically generate an object of type
165 $PREFERRED_CGI_MODULE which defaults to CGI.
168 Validate has a wide range of options available. (See
169 CGI::Ex::Validate for a full explanation of functionality). Validate
173 Can be either a hashref to be validated, or a CGI style object
174 (which has the param method).
177 The val_hash can be one of three items. First, it can be a
178 straight perl hashref containing the validation to be done.
179 Second, it can be a YAML document string. Third, it can be the
180 path to a file containing the validation. The validation in a
181 validation file will be read in depending upon file extension.
184 Very similar to CGI->new->Vars except that arrays are returned as
185 arrays. Not sure why CGI didn't do this anyway (well - yes - legacy
186 Perl 4 - but at some point things need to be updated).
188 my $hash = $cgix->get_form;
189 my $hash = $cgix->get_form(CGI->new);
190 my $hash = get_form();
191 my $hash = get_form(CGI->new);
194 Allow for setting a custom form hash. Useful for testing, or other
197 $cgix->set_form(\%new_form);
200 Returns a hash of all cookies.
202 my $hash = $cgix->get_cookies;
203 my $hash = $cgix->get_cookies(CGI->new);
204 my $hash = get_cookies();
205 my $hash = get_cookies(CGI->new);
208 Allow for setting a custom cookies hash. Useful for testing, or
211 $cgix->set_cookies(\%new_cookies);
214 Takes a hash and returns a query_string. A second optional argument
215 may contain an arrayref of keys to use from the hash in building the
216 query_string. First argument is undef, it will use the form stored
217 in itself as the hash.
220 Can be called multiple times during the same session. Will only
221 print content-type once. (Useful if you don't know if something else
222 already printed content-type). Calling this sends the Content-type
223 header. Trying to print ->content_type is an error. For clarity, the
224 method ->print_content_type is available.
226 $cgix->print_content_type;
229 $cgix->print_content_type('text/html');
232 $cgix->print_content_type('text/html', 'utf-8');
235 Arguments are the same as those to CGI->new->cookie({}). Uses CGI's
236 cookie method to create a cookie, but then, depending on if content
237 has already been sent to the browser will either print a Set-cookie
238 header, or will add a <meta http-equiv='set-cookie'> tag (this is
239 supported on most major browsers). This is useful if you don't know
240 if something else already printed content-type.
243 Depending on if content has already been sent to the browser will
244 either print a Location header, or will add a <meta
245 http-equiv='refresh'> tag (this is supported on all major browsers).
246 This is useful if you don't know if something else already printed
247 content-type. Takes single argument of a url.
250 Depending on if content has already been sent to the browser will
251 either print a Last-Modified header, or will add a <meta
252 http-equiv='Last-Modified'> tag (this is supported on most major
253 browsers). This is useful if you don't know if something else
254 already printed content-type. Takes an argument of either a time
255 (may be a CGI -expires style time) or a filename.
258 Depending on if content has already been sent to the browser will
259 either print a Expires header, or will add a <meta
260 http-equiv='Expires'> tag (this is supported on most major
261 browsers). This is useful if you don't know if something else
262 already printed content-type. Takes an argument of a time (may be a
263 CGI -expires style time).
266 Send a custom status. Works in both CGI and mod_perl. Arguments are
267 a status code and the content (optional).
270 Send a http header. Works in both CGI and mod_perl. Arguments are a
271 header name and the value for that header.
274 Prints out a javascript file. Does everything it can to make sure
275 that the javascript will cache. Takes either a full filename, or a
276 shortened name which will be looked for in @INC. (ie
277 /full/path/to/my.js or CGI/Ex/validate.js or CGI::Ex::validate)
281 CGI::Ex->print_js($ENV{'PATH_INFO'});
284 This is intended as a simple yet strong subroutine to swap in tags
285 to a document. It is intended to be very basic for those who may not
286 want the full features of a Templating system such as
287 Template::Toolkit (even though they should investigate them because
288 they are pretty nice). The default allows for basic template toolkit
289 variable swapping. There are two arguments. First is a string or a
290 reference to a string. If a string is passed, a copy of that string
291 is swapped and returned. If a reference to a string is passed, it is
292 modified in place. The second argument is a form, or a CGI object,
293 or a cgiex object, or a coderef (if the second argument is missing,
294 the cgiex object which called the method will be used). If it is a
295 coderef, it should accept key as its only argument and return the
298 my $cgix = CGI::Ex->new;
299 my $form = {foo => 'bar',
300 this => {is => {nested => ['wow', 'wee']}}
303 my $str = $cgix->swap_template("<html>[% foo %]<br>[% foo %]</html>", $form));
304 # $str eq '<html>bar<br>bar</html>'
306 $str = $cgix->swap_template("[% this.is.nested.1 %]", $form));
309 $str = "[% this.is.nested.0 %]";
310 $cgix->swap_template(\$str, $form);
313 # may also be called with only one argument as follows:
314 # assuming $cgix had a query string of ?foo=bar&baz=wow&this=wee
315 $str = "<html>([% foo %]) <br>
317 ([% this %]) </html>";
318 $cgix->swap_template(\$str);
319 #$str eq "<html>(bar) <br>
323 For further examples, please see the code contained in
324 t/samples/cgi_ex_* of this distribution.
326 If at a later date, the developer upgrades to Template::Toolkit, the
327 templates that were being swapped by CGI::Ex::swap_template should
328 be compatible with Template::Toolkit.
331 See also CGI::Ex::App.
333 See also CGI::Ex::Auth.
335 See also CGI::Ex::Conf.
337 See also CGI::Ex::Die.
339 See also CGI::Ex::Dump.
341 See also CGI::Ex::Fill.
343 See also CGI::Ex::Template.
345 See also CGI::Ex::Validate.
348 This module may be distributed under the same terms as Perl itself.
351 Paul Seamons <perl at seamons dot com>