]> Dogcows Code - chaz/p5-CGI-Ex/blob - README
add PSGI handler
[chaz/p5-CGI-Ex] / README
1 NAME
2 CGI::Ex - CGI utility suite - makes powerful application writing fun and
3 easy
4
5 CGI::Ex SYNOPSIS
6 ### You probably don't want to use CGI::Ex directly
7 ### You probably should use CGI::Ex::App instead.
8
9 my $cgix = CGI::Ex->new;
10
11 $cgix->print_content_type;
12
13 my $hash = $cgix->form;
14
15 if ($hash->{'bounce'}) {
16
17 $cgix->set_cookie({
18 name => ...,
19 value => ...,
20 });
21
22 $cgix->location_bounce($new_url_location);
23 exit;
24 }
25
26 if (scalar keys %$form) {
27 my $val_hash = $cgix->conf_read($pathtovalidation);
28 my $err_obj = $cgix->validate($hash, $val_hash);
29 if ($err_obj) {
30 my $errors = $err_obj->as_hash;
31 my $input = "Some content";
32 my $content = "";
33 $cgix->swap_template(\$input, $errors, $content);
34 $cgix->fill({text => \$content, form => $hashref});
35 print $content;
36 exit;
37 } else {
38 print "Success";
39 }
40 } else {
41 print "Main page";
42 }
43
44 DESCRIPTION
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.
53
54 The main functionality is provided by several other modules that may be
55 used separately, or together through the CGI::Ex interface.
56
57 "CGI::Ex::Template"
58 A Template::Toolkit compatible processing engine. With a few
59 limitations, CGI::Ex::Template can be a drop in replacement for
60 Template::Toolkit.
61
62 "CGI::Ex::Fill"
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
66 information.
67
68 "CGI::Ex::Validate"
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.
76
77 "CGI::Ex::Conf"
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.
83
84 "CGI::Ex::Auth"
85 A highly configurable web based authentication system. See
86 CGI::Ex::Auth for more information.
87
88 CGI::Ex METHODS
89 "->fill"
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
98 of position):
99
100 "text"
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.
106
107 "form"
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.
120
121 NOTE: Only one of the form, fdat, and fobject arguments are
122 allowed at a time.
123
124 "target"
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.
127
128 "fill_passwords"
129 Boolean value defaults to 1. If set to zero - password fields
130 will not be filled.
131
132 "ignore_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.
136
137 Other named arguments are available for compatibility with
138 HTML::FillInForm. They may only be used as named arguments.
139
140 "scalarref"
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
143 filled in place.
144
145 "arrayref"
146 An array ref of lines of the document. Forces a returned filled
147 html document.
148
149 "file"
150 An filename that will be opened, filled, and returned.
151
152 "fdat"
153 A hashref of key value pairs.
154
155 "fobject"
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.
159
160 See CGI::Ex::Fill for more information about the filling process.
161
162 "->object"
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.
166
167 "->validate"
168 Validate has a wide range of options available. (See
169 CGI::Ex::Validate for a full explanation of functionality). Validate
170 has two arguments:
171
172 "form"
173 Can be either a hashref to be validated, or a CGI style object
174 (which has the param method).
175
176 "val_hash"
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.
182
183 "->get_form"
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).
187
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);
192
193 "->set_form"
194 Allow for setting a custom form hash. Useful for testing, or other
195 purposes.
196
197 $cgix->set_form(\%new_form);
198
199 "->get_cookies"
200 Returns a hash of all cookies.
201
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);
206
207 "->set_cookies"
208 Allow for setting a custom cookies hash. Useful for testing, or
209 other purposes.
210
211 $cgix->set_cookies(\%new_cookies);
212
213 "->make_form"
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.
218
219 "->content_type"
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.
225
226 $cgix->print_content_type;
227
228 # OR
229 $cgix->print_content_type('text/html');
230
231 # OR
232 $cgix->print_content_type('text/html', 'utf-8');
233
234 "->set_cookie"
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.
241
242 "->location_bounce"
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.
248
249 "->last_modified"
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.
256
257 "->expires"
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).
264
265 "->send_status"
266 Send a custom status. Works in both CGI and mod_perl. Arguments are
267 a status code and the content (optional).
268
269 "->send_header"
270 Send a http header. Works in both CGI and mod_perl. Arguments are a
271 header name and the value for that header.
272
273 "->print_js"
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)
278
279 #!/usr/bin/perl
280 use CGI::Ex;
281 CGI::Ex->print_js($ENV{'PATH_INFO'});
282
283 "->swap_template"
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
296 proper value.
297
298 my $cgix = CGI::Ex->new;
299 my $form = {foo => 'bar',
300 this => {is => {nested => ['wow', 'wee']}}
301 };
302
303 my $str = $cgix->swap_template("<html>[% foo %]<br>[% foo %]</html>", $form));
304 # $str eq '<html>bar<br>bar</html>'
305
306 $str = $cgix->swap_template("[% this.is.nested.1 %]", $form));
307 # $str eq 'wee'
308
309 $str = "[% this.is.nested.0 %]";
310 $cgix->swap_template(\$str, $form);
311 # $str eq 'wow'
312
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>
316 ([% baz %]) <br>
317 ([% this %]) </html>";
318 $cgix->swap_template(\$str);
319 #$str eq "<html>(bar) <br>
320 # (wow) <br>
321 # (wee) </html>";
322
323 For further examples, please see the code contained in
324 t/samples/cgi_ex_* of this distribution.
325
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.
329
330 MODULES
331 See also CGI::Ex::App.
332
333 See also CGI::Ex::Auth.
334
335 See also CGI::Ex::Conf.
336
337 See also CGI::Ex::Die.
338
339 See also CGI::Ex::Dump.
340
341 See also CGI::Ex::Fill.
342
343 See also CGI::Ex::Template.
344
345 See also CGI::Ex::Validate.
346
347 LICENSE
348 This module may be distributed under the same terms as Perl itself.
349
350 AUTHOR
351 Paul Seamons <perl at seamons dot com>
352
This page took 0.045564 seconds and 4 git commands to generate.