1CGI::Application(3) User Contributed Perl Documentation CGI::Application(3)
2
3
4
6 CGI::Application - Framework for building reusable web-applications
7
9 # In "WebApp.pm"...
10 package WebApp;
11 use base 'CGI::Application';
12
13 # ( setup() can even be skipped for common cases. See docs below. )
14 sub setup {
15 my $self = shift;
16 $self->start_mode('mode1');
17 $self->mode_param('rm');
18 $self->run_modes(
19 'mode1' => 'do_stuff',
20 'mode2' => 'do_more_stuff',
21 'mode3' => 'do_something_else'
22 );
23 }
24 sub do_stuff { ... }
25 sub do_more_stuff { ... }
26 sub do_something_else { ... }
27 1;
28
29
30 ### In "webapp.cgi"...
31 use WebApp;
32 my $webapp = WebApp->new();
33 $webapp->run();
34
36 CGI::Application makes it easier to create sophisticated, high-
37 performance, reusable web-based applications. CGI::Application helps
38 makes your web applications easier to design, write, and evolve.
39
40 CGI::Application judiciously avoids employing technologies and
41 techniques which would bind a developer to any one set of tools,
42 operating system or web server.
43
44 It is lightweight in terms of memory usage, making it suitable for
45 common CGI environments, and a high performance choice in persistent
46 environments like FastCGI or mod_perl.
47
48 By adding PLUG-INS as your needs grow, you can add advanced and complex
49 features when you need them.
50
51 First released in 2000 and used and expanded by a number of
52 professional website developers, CGI::Application is a stable, reliable
53 choice.
54
56 Imagine you have to write an application to search through a database
57 of widgets. Your application has three screens:
58
59 1. Search form
60 2. List of results
61 3. Detail of a single record
62
63 To write this application using CGI::Application you will create two
64 files:
65
66 1. WidgetView.pm -- Your "Application Module"
67 2. widgetview.cgi -- Your "Instance Script"
68
69 The Application Module contains all the code specific to your
70 application functionality, and it exists outside of your web server's
71 document root, somewhere in the Perl library search path.
72
73 The Instance Script is what is actually called by your web server. It
74 is a very small, simple file which simply creates an instance of your
75 application and calls an inherited method, run(). Following is the
76 entirety of "widgetview.cgi":
77
78 #!/usr/bin/perl -w
79 use WidgetView;
80 my $webapp = WidgetView->new();
81 $webapp->run();
82
83 As you can see, widgetview.cgi simply "uses" your Application module
84 (which implements a Perl package called "WidgetView"). Your
85 Application Module, "WidgetView.pm", is somewhat more lengthy:
86
87 package WidgetView;
88 use base 'CGI::Application';
89 use strict;
90
91 # Needed for our database connection
92 use CGI::Application::Plugin::DBH;
93
94 sub setup {
95 my $self = shift;
96 $self->start_mode('mode1');
97 $self->run_modes(
98 'mode1' => 'showform',
99 'mode2' => 'showlist',
100 'mode3' => 'showdetail'
101 );
102
103 # Connect to DBI database, with the same args as DBI->connect();
104 $self->dbh_config();
105 }
106
107 sub teardown {
108 my $self = shift;
109
110 # Disconnect when we're done, (Although DBI usually does this automatically)
111 $self->dbh->disconnect();
112 }
113
114 sub showform {
115 my $self = shift;
116
117 # Get CGI query object
118 my $q = $self->query();
119
120 my $output = '';
121 $output .= $q->start_html(-title => 'Widget Search Form');
122 $output .= $q->start_form();
123 $output .= $q->textfield(-name => 'widgetcode');
124 $output .= $q->hidden(-name => 'rm', -value => 'mode2');
125 $output .= $q->submit();
126 $output .= $q->end_form();
127 $output .= $q->end_html();
128
129 return $output;
130 }
131
132 sub showlist {
133 my $self = shift;
134
135 # Get our database connection
136 my $dbh = $self->dbh();
137
138 # Get CGI query object
139 my $q = $self->query();
140 my $widgetcode = $q->param("widgetcode");
141
142 my $output = '';
143 $output .= $q->start_html(-title => 'List of Matching Widgets');
144
145 ## Do a bunch of stuff to select "widgets" from a DBI-connected
146 ## database which match the user-supplied value of "widgetcode"
147 ## which has been supplied from the previous HTML form via a
148 ## CGI.pm query object.
149 ##
150 ## Each row will contain a link to a "Widget Detail" which
151 ## provides an anchor tag, as follows:
152 ##
153 ## "widgetview.cgi?rm=mode3&widgetid=XXX"
154 ##
155 ## ...Where "XXX" is a unique value referencing the ID of
156 ## the particular "widget" upon which the user has clicked.
157
158 $output .= $q->end_html();
159
160 return $output;
161 }
162
163 sub showdetail {
164 my $self = shift;
165
166 # Get our database connection
167 my $dbh = $self->dbh();
168
169 # Get CGI query object
170 my $q = $self->query();
171 my $widgetid = $q->param("widgetid");
172
173 my $output = '';
174 $output .= $q->start_html(-title => 'Widget Detail');
175
176 ## Do a bunch of things to select all the properties of
177 ## the particular "widget" upon which the user has
178 ## clicked. The key id value of this widget is provided
179 ## via the "widgetid" property, accessed via the CGI.pm
180 ## query object.
181
182 $output .= $q->end_html();
183
184 return $output;
185 }
186
187 1; # Perl requires this at the end of all modules
188
189 CGI::Application takes care of implementing the new() and the run()
190 methods. Notice that at no point do you call print() to send any
191 output to STDOUT. Instead, all output is returned as a scalar.
192
193 CGI::Application's most significant contribution is in managing the
194 application state. Notice that all which is needed to push the
195 application forward is to set the value of a HTML form parameter 'rm'
196 to the value of the "run mode" you wish to handle the form submission.
197 This is the key to CGI::Application.
198
200 The guiding philosophy behind CGI::Application is that a web-based
201 application can be organized into a specific set of "Run Modes." Each
202 Run Mode is roughly analogous to a single screen (a form, some output,
203 etc.). All the Run Modes are managed by a single "Application Module"
204 which is a Perl module. In your web server's document space there is
205 an "Instance Script" which is called by the web server as a CGI (or an
206 Apache::Registry script if you're using Apache + mod_perl).
207
208 This methodology is an inversion of the "Embedded" philosophy (ASP,
209 JSP, EmbPerl, Mason, etc.) in which there are "pages" for each state of
210 the application, and the page drives functionality. In
211 CGI::Application, form follows function -- the Application Module
212 drives pages, and the code for a single application is in one place;
213 not spread out over multiple "pages". If you feel that Embedded
214 architectures are confusing, unorganized, difficult to design and
215 difficult to manage, CGI::Application is the methodology for you!
216
217 Apache is NOT a requirement for CGI::Application. Web applications
218 based on CGI::Application will run equally well on NT/IIS or any other
219 CGI-compatible environment. CGI::Application-based projects are,
220 however, ripe for use on Apache/mod_perl servers, as they naturally
221 encourage Good Programming Practices and will often work in persistent
222 environments without modification.
223
224 For more information on using CGI::Application with mod_perl, please
225 see our website at http://www.cgi-app.org/, as well as
226 CGI::Application::Plugin::Apache, which integrates with
227 Apache::Request.
228
230 It is intended that your Application Module will be implemented as a
231 sub-class of CGI::Application. This is done simply as follows:
232
233 package My::App;
234 use base 'CGI::Application';
235
236 Notation and Conventions
237
238 For the purpose of this document, we will refer to the following
239 conventions:
240
241 WebApp.pm The Perl module which implements your Application Module class.
242 WebApp Your Application Module class; a sub-class of CGI::Application.
243 webapp.cgi The Instance Script which implements your Application Module.
244 $webapp An instance (object) of your Application Module class.
245 $c Same as $webapp, used in instance methods to pass around the
246 current object. (Sometimes referred as "$self" in other code)
247
248 Instance Script Methods
249 By inheriting from CGI::Application you have access to a number of
250 built-in methods. The following are those which are expected to be
251 called from your Instance Script.
252
253 new()
254
255 The new() method is the constructor for a CGI::Application. It returns
256 a blessed reference to your Application Module package (class).
257 Optionally, new() may take a set of parameters as key => value pairs:
258
259 my $webapp = WebApp->new(
260 TMPL_PATH => 'App/',
261 PARAMS => {
262 'custom_thing_1' => 'some val',
263 'another_custom_thing' => [qw/123 456/]
264 }
265 );
266
267 This method may take some specific parameters:
268
269 TMPL_PATH - This optional parameter defines a path to a directory of
270 templates. This is used by the load_tmpl() method (specified below),
271 and may also be used for the same purpose by other template plugins.
272 This run-time parameter allows you to further encapsulate instantiating
273 templates, providing potential for more re-usability. It can be either
274 a scalar or an array reference of multiple paths.
275
276 QUERY - This optional parameter allows you to specify an already-
277 created CGI.pm query object. Under normal use, CGI::Application will
278 instantiate its own CGI.pm query object. Under certain conditions, it
279 might be useful to be able to use one which has already been created.
280
281 PARAMS - This parameter, if used, allows you to set a number of custom
282 parameters at run-time. By passing in different values in different
283 instance scripts which use the same application module you can achieve
284 a higher level of re-usability. For instance, imagine an application
285 module, "Mailform.pm". The application takes the contents of a HTML
286 form and emails it to a specified recipient. You could have multiple
287 instance scripts throughout your site which all use this "Mailform.pm"
288 module, but which set different recipients or different forms.
289
290 One common use of instance scripts is to provide a path to a config
291 file. This design allows you to define project wide configuration
292 objects used by many several instance scripts. There are several
293 plugins which simplify the syntax for this and provide lazy loading.
294 Here's an example using CGI::Application::Plugin::ConfigAuto, which
295 uses Config::Auto to support many configuration file formats.
296
297 my $app = WebApp->new(PARAMS => { cfg_file => 'config.pl' });
298
299 # Later in your app:
300 my %cfg = $self->cfg()
301 # or ... $self->cfg('HTML_ROOT_DIR');
302
303 See the list of of plugins below for more config file integration
304 solutions.
305
306 run()
307
308 The run() method is called upon your Application Module object, from
309 your Instance Script. When called, it executes the functionality in
310 your Application Module.
311
312 my $webapp = WebApp->new();
313 $webapp->run();
314
315 This method first determines the application state by looking at the
316 value of the CGI parameter specified by mode_param() (defaults to 'rm'
317 for "Run Mode"), which is expected to contain the name of the mode of
318 operation. If not specified, the state defaults to the value of
319 start_mode().
320
321 Once the mode has been determined, run() looks at the dispatch table
322 stored in run_modes() and finds the function pointer which is keyed
323 from the mode name. If found, the function is called and the data
324 returned is print()'ed to STDOUT and to the browser. If the specified
325 mode is not found in the run_modes() table, run() will croak().
326
327 Methods to possibly override
328 CGI::Application implements some methods which are expected to be
329 overridden by implementing them in your sub-class module. These
330 methods are as follows:
331
332 setup()
333
334 This method is called by the inherited new() constructor method. The
335 setup() method should be used to define the following property/methods:
336
337 mode_param() - set the name of the run mode CGI param.
338 start_mode() - text scalar containing the default run mode.
339 error_mode() - text scalar containing the error mode.
340 run_modes() - hash table containing mode => function mappings.
341 tmpl_path() - text scalar or array reference containing path(s) to template files.
342
343 Your setup() method may call any of the instance methods of your
344 application. This function is a good place to define properties
345 specific to your application via the $webapp->param() method.
346
347 Your setup() method might be implemented something like this:
348
349 sub setup {
350 my $self = shift;
351 $self->tmpl_path('/path/to/my/templates/');
352 $self->start_mode('putform');
353 $self->error_mode('my_error_rm');
354 $self->run_modes({
355 'putform' => 'my_putform_func',
356 'postdata' => 'my_data_func'
357 });
358 $self->param('myprop1');
359 $self->param('myprop2', 'prop2value');
360 $self->param('myprop3', ['p3v1', 'p3v2', 'p3v3']);
361 }
362
363 However, often times all that needs to be in setup() is defining your
364 run modes and your start mode. CGI::Application::Plugin::AutoRunmode
365 allows you to do this with a simple syntax, using run mode attributes:
366
367 use CGI::Application::Plugin::AutoRunmode;
368
369 sub show_first : StartRunmode { ... };
370 sub do_next : Runmode { ... }
371
372 teardown()
373
374 If implemented, this method is called automatically after your
375 application runs. It can be used to clean up after your operations. A
376 typical use of the teardown() function is to disconnect a database
377 connection which was established in the setup() function. You could
378 also use the teardown() method to store state information about the
379 application to the server.
380
381 cgiapp_init()
382
383 If implemented, this method is called automatically right before the
384 setup() method is called. This method provides an optional
385 initialization hook, which improves the object-oriented characteristics
386 of CGI::Application. The cgiapp_init() method receives, as its
387 parameters, all the arguments which were sent to the new() method.
388
389 An example of the benefits provided by utilizing this hook is creating
390 a custom "application super-class" from which all your web applications
391 would inherit, instead of CGI::Application.
392
393 Consider the following:
394
395 # In MySuperclass.pm:
396 package MySuperclass;
397 use base 'CGI::Application';
398 sub cgiapp_init {
399 my $self = shift;
400 # Perform some project-specific init behavior
401 # such as to load settings from a database or file.
402 }
403
404
405 # In MyApplication.pm:
406 package MyApplication;
407 use base 'MySuperclass';
408 sub setup { ... }
409 sub teardown { ... }
410 # The rest of your CGI::Application-based follows...
411
412 By using CGI::Application and the cgiapp_init() method as illustrated,
413 a suite of applications could be designed to share certain
414 characteristics. This has the potential for much cleaner code built on
415 object-oriented inheritance.
416
417 cgiapp_prerun()
418
419 If implemented, this method is called automatically right before the
420 selected run mode method is called. This method provides an optional
421 pre-runmode hook, which permits functionality to be added at the point
422 right before the run mode method is called. To further leverage this
423 hook, the value of the run mode is passed into cgiapp_prerun().
424
425 Another benefit provided by utilizing this hook is creating a custom
426 "application super-class" from which all your web applications would
427 inherit, instead of CGI::Application.
428
429 Consider the following:
430
431 # In MySuperclass.pm:
432 package MySuperclass;
433 use base 'CGI::Application';
434 sub cgiapp_prerun {
435 my $self = shift;
436 # Perform some project-specific init behavior
437 # such as to implement run mode specific
438 # authorization functions.
439 }
440
441
442 # In MyApplication.pm:
443 package MyApplication;
444 use base 'MySuperclass';
445 sub setup { ... }
446 sub teardown { ... }
447 # The rest of your CGI::Application-based follows...
448
449 By using CGI::Application and the cgiapp_prerun() method as
450 illustrated, a suite of applications could be designed to share certain
451 characteristics. This has the potential for much cleaner code built on
452 object-oriented inheritance.
453
454 It is also possible, within your cgiapp_prerun() method, to change the
455 run mode of your application. This can be done via the prerun_mode()
456 method, which is discussed elsewhere in this POD.
457
458 cgiapp_postrun()
459
460 If implemented, this hook will be called after the run mode method has
461 returned its output, but before HTTP headers are generated. This will
462 give you an opportunity to modify the body and headers before they are
463 returned to the web browser.
464
465 A typical use for this hook is pipelining the output of a CGI-
466 Application through a series of "filter" processors. For example:
467
468 * You want to enclose the output of all your CGI-Applications in
469 an HTML table in a larger page.
470
471 * Your run modes return structured data (such as XML), which you
472 want to transform using a standard mechanism (such as XSLT).
473
474 * You want to post-process CGI-App output through another system,
475 such as HTML::Mason.
476
477 * You want to modify HTTP headers in a particular way across all
478 run modes, based on particular criteria.
479
480 The cgiapp_postrun() hook receives a reference to the output from your
481 run mode method, in addition to the CGI-App object. A typical
482 cgiapp_postrun() method might be implemented as follows:
483
484 sub cgiapp_postrun {
485 my $self = shift;
486 my $output_ref = shift;
487
488 # Enclose output HTML table
489 my $new_output = "<table border=1>";
490 $new_output .= "<tr><td> Hello, World! </td></tr>";
491 $new_output .= "<tr><td>". $$output_ref ."</td></tr>";
492 $new_output .= "</table>";
493
494 # Replace old output with new output
495 $$output_ref = $new_output;
496 }
497
498 Obviously, with access to the CGI-App object you have full access to
499 use all the methods normally available in a run mode. You could, for
500 example, use "load_tmpl()" to replace the static HTML in this example
501 with HTML::Template. You could change the HTTP headers (via
502 "header_type()" and "header_props()" methods) to set up a redirect.
503 You could also use the objects properties to apply changes only under
504 certain circumstance, such as a in only certain run modes, and when a
505 "param()" is a particular value.
506
507 cgiapp_get_query()
508
509 my $q = $webapp->cgiapp_get_query;
510
511 Override this method to retrieve the query object if you wish to use a
512 different query interface instead of CGI.pm.
513
514 CGI.pm is only loaded if it is used on a given request.
515
516 If you can use an alternative to CGI.pm, it needs to have some
517 compatibility with the CGI.pm API. For normal use, just having a
518 compatible "param" method should be sufficient.
519
520 If you use the "path_info" option to the mode_param() method, then we
521 will call the "path_info()" method on the query object.
522
523 If you use the "Dump" method in CGI::Application, we will call the
524 "Dump" and "escapeHTML" methods on the query object.
525
526 Essential Application Methods
527 The following methods are inherited from CGI::Application, and are
528 available to be called by your application within your Application
529 Module. They are called essential because you will use all are most of
530 them to get any application up and running. These functions are listed
531 in alphabetical order.
532
533 load_tmpl()
534
535 my $tmpl_obj = $webapp->load_tmpl;
536 my $tmpl_obj = $webapp->load_tmpl('some.html');
537 my $tmpl_obj = $webapp->load_tmpl( \$template_content );
538 my $tmpl_obj = $webapp->load_tmpl( FILEHANDLE );
539
540 This method takes the name of a template file, a reference to template
541 data or a FILEHANDLE and returns an HTML::Template object. If the
542 filename is undefined or missing, CGI::Application will default to
543 trying to use the current run mode name, plus the extension ".html".
544
545 If you use the default template naming system, you should also use
546 CGI::Application::Plugin::Forward, which simply helps to keep the
547 current name accurate when you pass control from one run mode to
548 another.
549
550 ( For integration with other template systems and automated template
551 names, see "Alternatives to load_tmpl() below. )
552
553 When you pass in a filename, the HTML::Template->new_file() constructor
554 is used for create the object. When you pass in a reference to the
555 template content, the HTML::Template->new_scalar_ref() constructor is
556 used and when you pass in a filehandle, the
557 HTML::Template->new_filehandle() constructor is used.
558
559 Refer to HTML::Template for specific usage of HTML::Template.
560
561 If tmpl_path() has been specified, load_tmpl() will set the
562 HTML::Template "path" option to the path(s) provided. This further
563 assists in encapsulating template usage.
564
565 The load_tmpl() method will pass any extra parameters sent to it
566 directly to HTML::Template->new_file() (or new_scalar_ref() or
567 new_filehandle()). This will allow the HTML::Template object to be
568 further customized:
569
570 my $tmpl_obj = $webapp->load_tmpl('some_other.html',
571 die_on_bad_params => 0,
572 cache => 1
573 );
574
575 Note that if you want to pass extra arguments but use the default
576 template name, you still need to provide a name of "undef":
577
578 my $tmpl_obj = $webapp->load_tmpl(undef,
579 die_on_bad_params => 0,
580 cache => 1
581 );
582
583 Alternatives to load_tmpl()
584
585 If your application requires more specialized behavior than this, you
586 can always replace it by overriding load_tmpl() by implementing your
587 own load_tmpl() in your CGI::Application sub-class application module.
588
589 First, you may want to check out the template related plugins.
590
591 CGI::Application::Plugin::TT focuses just on Template Toolkit
592 integration, and features pre-and-post features, singleton support and
593 more.
594
595 CGI::Application::Plugin::Stream can help if you want to return a
596 stream and not a file. It features a simple syntax and MIME-type
597 detection.
598
599 specifying the template class with html_tmpl_class()
600
601 You may specify an API-compatible alternative to HTML::Template by
602 setting a new "html_tmpl_class()":
603
604 $self->html_tmpl_class('HTML::Template::Dumper');
605
606 The default is "HTML::Template". The alternate class should provide at
607 least the following parts of the HTML::Template API:
608
609 $t = $class->new( scalarref => ... ); # If you use scalarref templates
610 $t = $class->new( filehandle => ... ); # If you use filehandle templates
611 $t = $class->new( filename => ... );
612 $t->param(...);
613
614 Here's an example case allowing you to precisely test what's sent to
615 your templates:
616
617 $ENV{CGI_APP_RETURN_ONLY} = 1;
618 my $webapp = WebApp->new;
619 $webapp->html_tmpl_class('HTML::Template::Dumper');
620 my $out_str = $webapp->run;
621 my $tmpl_href = eval "$out_str";
622
623 # Now Precisely test what would be set to the template
624 is ($tmpl_href->{pet_name}, 'Daisy', "Daisy is sent template");
625
626 This is a powerful technique because HTML::Template::Dumper loads and
627 considers the template file that would actually be used. If the
628 'pet_name' token was missing in the template, the above test would
629 fail. So, you are testing both your code and your templates in a much
630 more precise way than using simple regular expressions to see if the
631 string "Daisy" appeared somewhere on the page.
632
633 The load_tmpl() callback
634
635 Plugin authors will be interested to know that you can register a
636 callback that will be executed just before load_tmpl() returns:
637
638 $self->add_callback('load_tmpl',\&your_method);
639
640 When "your_method()" is executed, it will be passed three arguments:
641
642 1. A hash reference of the extra params passed into C<load_tmpl>
643 2. Followed by a hash reference to template parameters.
644 With both of these, you can modify them by reference to affect
645 values that are actually passed to the new() and param() methods of the
646 template object.
647 3. The name of the template file.
648
649 Here's an example stub for a load_tmpl() callback:
650
651 sub my_load_tmpl_callback {
652 my ($c, $ht_params, $tmpl_params, $tmpl_file) = @_
653 # modify $ht_params or $tmpl_params by reference...
654 }
655
656 param()
657
658 $webapp->param('pname', $somevalue);
659
660 The param() method provides a facility through which you may set
661 application instance properties which are accessible throughout your
662 application.
663
664 The param() method may be used in two basic ways. First, you may use
665 it to get or set the value of a parameter:
666
667 $webapp->param('scalar_param', '123');
668 my $scalar_param_values = $webapp->param('some_param');
669
670 Second, when called in the context of an array, with no parameter name
671 specified, param() returns an array containing all the parameters which
672 currently exist:
673
674 my @all_params = $webapp->param();
675
676 The param() method also allows you to set a bunch of parameters at once
677 by passing in a hash (or hashref):
678
679 $webapp->param(
680 'key1' => 'val1',
681 'key2' => 'val2',
682 'key3' => 'val3',
683 );
684
685 The param() method enables a very valuable system for customizing your
686 applications on a per-instance basis. One Application Module might be
687 instantiated by different Instance Scripts. Each Instance Script might
688 set different values for a set of parameters. This allows similar
689 applications to share a common code-base, but behave differently. For
690 example, imagine a mail form application with a single Application
691 Module, but multiple Instance Scripts. Each Instance Script might
692 specify a different recipient. Another example would be a web bulletin
693 boards system. There could be multiple boards, each with a different
694 topic and set of administrators.
695
696 The new() method provides a shortcut for specifying a number of run-
697 time parameters at once. Internally, CGI::Application calls the
698 param() method to set these properties. The param() method is a
699 powerful tool for greatly increasing your application's re-usability.
700
701 query()
702
703 my $q = $webapp->query();
704 my $remote_user = $q->remote_user();
705
706 This method retrieves the CGI.pm query object which has been created by
707 instantiating your Application Module. For details on usage of this
708 query object, refer to CGI. CGI::Application is built on the CGI
709 module. Generally speaking, you will want to become very familiar with
710 CGI.pm, as you will use the query object whenever you want to interact
711 with form data.
712
713 When the new() method is called, a CGI query object is automatically
714 created. If, for some reason, you want to use your own CGI query
715 object, the new() method supports passing in your existing query object
716 on construction using the QUERY attribute.
717
718 There are a few rare situations where you want your own query object to
719 be used after your Application Module has already been constructed. In
720 that case you can pass it to c<query()> like this:
721
722 $webapp->query($new_query_object);
723 my $q = $webapp->query(); # now uses $new_query_object
724
725 run_modes()
726
727 # The common usage: an arrayref of run mode names that exactly match subroutine names
728 $webapp->run_modes([qw/
729 form_display
730 form_process
731 /]);
732
733 # With a hashref, use a different name or a code ref
734 $webapp->run_modes(
735 'mode1' => 'some_sub_by_name',
736 'mode2' => \&some_other_sub_by_ref
737 );
738
739 This accessor/mutator specifies the dispatch table for the application
740 states, using the syntax examples above. It returns the dispatch table
741 as a hash.
742
743 The run_modes() method may be called more than once. Additional values
744 passed into run_modes() will be added to the run modes table. In the
745 case that an existing run mode is re-defined, the new value will
746 override the existing value. This behavior might be useful for
747 applications which are created via inheritance from another
748 application, or some advanced application which modifies its own
749 capabilities based on user input.
750
751 The run() method uses the data in this table to send the application to
752 the correct function as determined by reading the CGI parameter
753 specified by mode_param() (defaults to 'rm' for "Run Mode"). These
754 functions are referred to as "run mode methods".
755
756 The hash table set by this method is expected to contain the mode name
757 as a key. The value should be either a hard reference (a subref) to
758 the run mode method which you want to be called when the application
759 enters the specified run mode, or the name of the run mode method to be
760 called:
761
762 'mode_name_by_ref' => \&mode_function
763 'mode_name_by_name' => 'mode_function'
764
765 The run mode method specified is expected to return a block of text
766 (e.g.: HTML) which will eventually be sent back to the web browser.
767 The run mode method may return its block of text as a scalar or a
768 scalar-ref.
769
770 An advantage of specifying your run mode methods by name instead of by
771 reference is that you can more easily create derivative applications
772 using inheritance. For instance, if you have a new application which
773 is exactly the same as an existing application with the exception of
774 one run mode, you could simply inherit from that other application and
775 override the run mode method which is different. If you specified your
776 run mode method by reference, your child class would still use the
777 function from the parent class.
778
779 An advantage of specifying your run mode methods by reference instead
780 of by name is performance. Dereferencing a subref is faster than
781 eval()-ing a code block. If run-time performance is a critical issue,
782 specify your run mode methods by reference and not by name. The speed
783 differences are generally small, however, so specifying by name is
784 preferred.
785
786 Specifying the run modes by array reference:
787
788 $webapp->run_modes([ 'mode1', 'mode2', 'mode3' ]);
789
790 Is is the same as using a hash, with keys equal to values
791
792 $webapp->run_modes(
793 'mode1' => 'mode1',
794 'mode2' => 'mode2',
795 'mode3' => 'mode3'
796 );
797
798 Often, it makes good organizational sense to have your run modes map to
799 methods of the same name. The array-ref interface provides a shortcut
800 to that behavior while reducing verbosity of your code.
801
802 Note that another importance of specifying your run modes in either a
803 hash or array-ref is to assure that only those Perl methods which are
804 specifically designated may be called via your application.
805 Application environments which don't specify allowed methods and
806 disallow all others are insecure, potentially opening the door to
807 allowing execution of arbitrary code. CGI::Application maintains a
808 strict "default-deny" stance on all method invocation, thereby allowing
809 secure applications to be built upon it.
810
811 IMPORTANT NOTE ABOUT RUN MODE METHODS
812
813 Your application should *NEVER* print() to STDOUT. Using print() to
814 send output to STDOUT (including HTTP headers) is exclusively the
815 domain of the inherited run() method. Breaking this rule is a common
816 source of errors. If your program is erroneously sending content
817 before your HTTP header, you are probably breaking this rule.
818
819 THE RUN MODE OF LAST RESORT: "AUTOLOAD"
820
821 If CGI::Application is asked to go to a run mode which doesn't exist it
822 will usually croak() with errors. If this is not your desired
823 behavior, it is possible to catch this exception by implementing a run
824 mode with the reserved name "AUTOLOAD":
825
826 $self->run_modes(
827 "AUTOLOAD" => \&catch_my_exception
828 );
829
830 Before CGI::Application calls croak() it will check for the existence
831 of a run mode called "AUTOLOAD". If specified, this run mode will in
832 invoked just like a regular run mode, with one exception: It will
833 receive, as an argument, the name of the run mode which invoked it:
834
835 sub catch_my_exception {
836 my $self = shift;
837 my $intended_runmode = shift;
838
839 my $output = "Looking for '$intended_runmode', but found 'AUTOLOAD' instead";
840 return $output;
841 }
842
843 This functionality could be used for a simple human-readable error
844 screen, or for more sophisticated application behaviors.
845
846 start_mode()
847
848 $webapp->start_mode('mode1');
849
850 The start_mode contains the name of the mode as specified in the
851 run_modes() table. Default mode is "start". The mode key specified
852 here will be used whenever the value of the CGI form parameter
853 specified by mode_param() is not defined. Generally, this is the first
854 time your application is executed.
855
856 tmpl_path()
857
858 $webapp->tmpl_path('/path/to/some/templates/');
859
860 This access/mutator method sets the file path to the directory (or
861 directories) where the templates are stored. It is used by load_tmpl()
862 to find the template files, using HTML::Template's "path" option. To
863 set the path you can either pass in a text scalar or an array reference
864 of multiple paths.
865
866 More Application Methods
867 You can skip this section if you are just getting started.
868
869 The following additional methods are inherited from CGI::Application,
870 and are available to be called by your application within your
871 Application Module. These functions are listed in alphabetical order.
872
873 delete()
874
875 $webapp->delete('my_param');
876
877 The delete() method is used to delete a parameter that was previously
878 stored inside of your application either by using the PARAMS hash that
879 was passed in your call to new() or by a call to the param() method.
880 This is similar to the delete() method of CGI.pm. It is useful if your
881 application makes decisions based on the existence of certain params
882 that may have been removed in previous sections of your app or simply
883 to clean-up your param()s.
884
885 dump()
886
887 print STDERR $webapp->dump();
888
889 The dump() method is a debugging function which will return a chunk of
890 text which contains all the environment and web form data of the
891 request, formatted nicely for human readability. Useful for outputting
892 to STDERR.
893
894 dump_html()
895
896 my $output = $webapp->dump_html();
897
898 The dump_html() method is a debugging function which will return a
899 chunk of text which contains all the environment and web form data of
900 the request, formatted nicely for human readability via a web browser.
901 Useful for outputting to a browser.
902
903 error_mode()
904
905 $webapp->error_mode('my_error_rm');
906
907 If the runmode dies for whatever reason, "run() will" see if you have
908 set a value for "error_mode()". If you have, "run()" will call that
909 method as a run mode, passing $@ as the only parameter.
910
911 Plugins authors will be interested to know that just before
912 "error_mode()" is called, the "error" hook will be executed, with the
913 error message passed in as the only parameter.
914
915 No "error_mode" is defined by default. The death of your
916 "error_mode()" run mode is not trapped, so you can also use it to die
917 in your own special way.
918
919 For a complete integrated logging solution, check out
920 CGI::Application::Plugin::LogDispatch.
921
922 get_current_runmode()
923
924 $webapp->get_current_runmode();
925
926 The "get_current_runmode()" method will return a text scalar containing
927 the name of the run mode which is currently being executed. If the run
928 mode has not yet been determined, such as during setup(), this method
929 will return undef.
930
931 header_add()
932
933 # add or replace the 'type' header
934 $webapp->header_add( -type => 'image/png' );
935
936 - or -
937
938 # add an additional cookie
939 $webapp->header_add(-cookie=>[$extra_cookie]);
940
941 The "header_add()" method is used to add one or more headers to the
942 outgoing response headers. The parameters will eventually be passed on
943 to the CGI.pm header() method, so refer to the CGI docs for exact usage
944 details.
945
946 Unlike calling "header_props()", "header_add()" will preserve any
947 existing headers. If a scalar value is passed to "header_add()" it will
948 replace the existing value for that key.
949
950 If an array reference is passed as a value to "header_add()", values in
951 that array ref will be appended to any existing values values for that
952 key. This is primarily useful for setting an additional cookie after
953 one has already been set.
954
955 header_props()
956
957 $webapp->header_props(-type=>'image/gif',-expires=>'+3d');
958
959 The "header_props()" method expects a hash of CGI.pm-compatible HTTP
960 header properties. These properties will be passed directly to
961 CGI.pm's "header()" or "redirect()" methods. Refer to CGI for exact
962 usage details.
963
964 Calling header_props any arguments will clobber any existing headers
965 that have previously set.
966
967 "header_props()" return a hash of all the headers that have currently
968 been set. It can be called with no arguments just to get the hash
969 current headers back.
970
971 To add additional headers later without clobbering the old ones, see
972 "header_add()".
973
974 IMPORTANT NOTE REGARDING HTTP HEADERS
975
976 It is through the "header_props()" and "header_add()" method that you
977 may modify the outgoing HTTP headers. This is necessary when you want
978 to set a cookie, set the mime type to something other than "text/html",
979 or perform a redirect. The header_props() method works in conjunction
980 with the header_type() method. The value contained in header_type()
981 determines if we use CGI::header() or CGI::redirect(). The content of
982 header_props() is passed as an argument to whichever CGI.pm function is
983 called.
984
985 Understanding this relationship is important if you wish to manipulate
986 the HTTP header properly.
987
988 header_type()
989
990 $webapp->header_type('redirect');
991 $webapp->header_type('none');
992
993 This method used to declare that you are setting a redirection header,
994 or that you want no header to be returned by the framework.
995
996 The value of 'header' is almost never used, as it is the default.
997
998 Example of redirecting:
999
1000 sub some_redirect_mode {
1001 my $self = shift;
1002 # do stuff here....
1003 $self->header_type('redirect');
1004 $self->header_props(-url=> "http://site/path/doc.html" );
1005 }
1006
1007 To simplify that further, use CGI::Application::Plugin::Redirect:
1008
1009 return $self->redirect('http://www.example.com/');
1010
1011 Setting the header to 'none' may be useful if you are streaming
1012 content. In other contexts, it may be more useful to set
1013 "$ENV{CGI_APP_RETURN_ONLY} = 1;", which supresses all printing,
1014 including headers, and returns the output instead.
1015
1016 That's commonly used for testing, or when using CGI::Application as a
1017 controller for a cron script!
1018
1019 mode_param()
1020
1021 # Name the CGI form parameter that contains the run mode name.
1022 # This is the the default behavior, and is often sufficient.
1023 $webapp->mode_param('rm');
1024
1025 # Set the run mode name directly from a code ref
1026 $webapp->mode_param(\&some_method);
1027
1028 # Alternate interface, which allows you to set the run
1029 # mode name directly from $ENV{PATH_INFO}.
1030 $webapp->mode_param(
1031 path_info=> 1,
1032 param =>'rm'
1033 );
1034
1035 This accessor/mutator method is generally called in the setup() method.
1036 It is used to help determine the run mode to call. There are three
1037 options for calling it.
1038
1039 $webapp->mode_param('rm');
1040
1041 Here, a CGI form parameter is named that will contain the name of the
1042 run mode to use. This is the default behavior, with 'rm' being the
1043 parameter named used.
1044
1045 $webapp->mode_param(\&some_method);
1046
1047 Here a code reference is provided. It will return the name of the run
1048 mode to use directly. Example:
1049
1050 sub some_method {
1051 my $self = shift;
1052 return 'run_mode_x';
1053 }
1054
1055 This would allow you to programmatically set the run mode based on
1056 arbitrary logic.
1057
1058 $webapp->mode_param(
1059 path_info=> 1,
1060 param =>'rm'
1061 );
1062
1063 This syntax allows you to easily set the run mode from $ENV{PATH_INFO}.
1064 It will try to set the run mode from the first part of $ENV{PATH_INFO}
1065 (before the first "/"). To specify that you would rather get the run
1066 mode name from the 2nd part of $ENV{PATH_INFO}:
1067
1068 $webapp->mode_param( path_info=> 2 );
1069
1070 This also demonstrates that you don't need to pass in the "param" hash
1071 key. It will still default to "rm".
1072
1073 You can also set "path_info" to a negative value. This works just like
1074 a negative list index: if it is -1 the run mode name will be taken from
1075 the last part of $ENV{PATH_INFO}, if it is -2, the one before that, and
1076 so on.
1077
1078 If no run mode is found in $ENV{PATH_INFO}, it will fall back to
1079 looking in the value of a the CGI form field defined with 'param', as
1080 described above. This allows you to use the convenient $ENV{PATH_INFO}
1081 trick most of the time, but also supports the edge cases, such as when
1082 you don't know what the run mode will be ahead of time and want to
1083 define it with JavaScript.
1084
1085 More about $ENV{PATH_INFO}.
1086
1087 Using $ENV{PATH_INFO} to name your run mode creates a clean separation
1088 between the form variables you submit and how you determine the
1089 processing run mode. It also creates URLs that are more search engine
1090 friendly. Let's look at an example form submission using this syntax:
1091
1092 <form action="/cgi-bin/instance.cgi/edit_form" method=post>
1093 <input type="hidden" name="breed_id" value="4">
1094
1095 Here the run mode would be set to "edit_form". Here's another example
1096 with a query string:
1097
1098 /cgi-bin/instance.cgi/edit_form?breed_id=2
1099
1100 This demonstrates that you can use $ENV{PATH_INFO} and a query string
1101 together without problems. $ENV{PATH_INFO} is defined as part of the
1102 CGI specification should be supported by any web server that supports
1103 CGI scripts.
1104
1105 prerun_mode()
1106
1107 $webapp->prerun_mode('new_run_mode');
1108
1109 The prerun_mode() method is an accessor/mutator which can be used
1110 within your cgiapp_prerun() method to change the run mode which is
1111 about to be executed. For example, consider:
1112
1113 # In WebApp.pm:
1114 package WebApp;
1115 use base 'CGI::Application';
1116 sub cgiapp_prerun {
1117 my $self = shift;
1118
1119 # Get the web user name, if any
1120 my $q = $self->query();
1121 my $user = $q->remote_user();
1122
1123 # Redirect to login, if necessary
1124 unless ($user) {
1125 $self->prerun_mode('login');
1126 }
1127 }
1128
1129 In this example, the web user will be forced into the "login" run mode
1130 unless they have already logged in. The prerun_mode() method permits a
1131 scalar text string to be set which overrides whatever the run mode
1132 would otherwise be.
1133
1134 The use of prerun_mode() within cgiapp_prerun() differs from setting
1135 mode_param() to use a call-back via subroutine reference. It differs
1136 because cgiapp_prerun() allows you to selectively set the run mode
1137 based on some logic in your cgiapp_prerun() method. The call-back
1138 facility of mode_param() forces you to entirely replace
1139 CGI::Application's mechanism for determining the run mode with your own
1140 method. The prerun_mode() method should be used in cases where you
1141 want to use CGI::Application's normal run mode switching facility, but
1142 you want to make selective changes to the mode under specific
1143 conditions.
1144
1145 Note: The prerun_mode() method may ONLY be called in the context of a
1146 cgiapp_prerun() method. Your application will die() if you call
1147 prerun_mode() elsewhere, such as in setup() or a run mode method.
1148
1149 Dispatching Clean URIs to run modes
1150 Modern web frameworks dispense with cruft in URIs, providing in clean
1151 URIs instead. Instead of:
1152
1153 /cgi-bin/item.cgi?rm=view&id=15
1154
1155 A clean URI to describe the same resource might be:
1156
1157 /item/15/view
1158
1159 The process of mapping these URIs to run modes is called dispatching
1160 and is handled by CGI::Application::Dispatch. Dispatching is not
1161 required and is a layer you can fairly easily add to an application
1162 later.
1163
1164 Offline website development
1165 You can work on your CGI::Application project on your desktop or laptop
1166 without installing a full-featured web-server like Apache. Instead,
1167 install CGI::Application::Server from CPAN. After a few minutes of
1168 setup, you'll have your own private application server up and running.
1169
1170 Automated Testing
1171 There a couple of testing modules specifically made for
1172 CGI::Application.
1173
1174 Test::WWW::Mechanize::CGIApp allows functional testing of a
1175 CGI::App-based project without starting a web server.
1176 Test::WWW::Mechanize could be used to test the app through a real web
1177 server.
1178
1179 Test::WWW::Selenium::CGIApp is similar, but uses Selenium for the
1180 testing, meaning that a local web-browser would be used, allowing
1181 testing of websites that contain JavaScript.
1182
1183 Direct testing is also easy. CGI::Application will normally print the
1184 output of it's run modes directly to STDOUT. This can be suppressed
1185 with an enviroment variable, CGI_APP_RETURN_ONLY. For example:
1186
1187 $ENV{CGI_APP_RETURN_ONLY} = 1;
1188 $output = $webapp->run();
1189 like($output, qr/good/, "output is good");
1190
1191 Examples of this style can be seen in our own test suite.
1192
1194 CGI::Application has a plug-in architecture that is easy to use and
1195 easy to develop new plug-ins for.
1196
1197 Recommended Plug-ins
1198 The following plugins are recommended for general purpose web/db
1199 development:
1200
1201 · CGI::Application::Plugin::Redirect - is a simple plugin to provide
1202 a shorter syntax for executing a redirect.
1203
1204 · CGI::Application::Plugin::ConfigAuto - Keeping your config details
1205 in a separate file is recommended for every project. This one
1206 integrates with Config::Auto. Several more config plugin options
1207 are listed below.
1208
1209 · CGI::Application::Plugin::DBH - Provides easy management of one or
1210 more database handles and can delay making the database connection
1211 until the moment it is actually used.
1212
1213 · CGI::Application::Plugin::FillInForm - makes it a breeze to fill in
1214 an HTML form from data originating from a CGI query or a database
1215 record.
1216
1217 · CGI::Application::Plugin::Session - For a project that requires
1218 session management, this plugin provides a useful wrapper around
1219 CGI::Session
1220
1221 · CGI::Application::Plugin::ValidateRM - Integration with
1222 Data::FormValidator and HTML::FillInForm
1223
1224 More plug-ins
1225 Many more plugins are available as alternatives and for specific uses.
1226 For a current complete list, please consult CPAN:
1227
1228 http://search.cpan.org/search?m=dist&q=CGI%2DApplication%2DPlugin
1229
1230 · CGI::Application::Plugin::AnyTemplate - Use any templating system
1231 from within CGI::Application using a unified interface
1232
1233 · CGI::Application::Plugin::Apache - Use Apache::* modules without
1234 interference
1235
1236 · CGI::Application::Plugin::AutoRunmode - Automatically register
1237 runmodes
1238
1239 · CGI::Application::Plugin::Config::Context - Integration with
1240 Config::Context.
1241
1242 · CGI::Application::Plugin::Config::General - Integration with
1243 Config::General.
1244
1245 · CGI::Application::Plugin::Config::Simple - Integration with
1246 Config::Simple.
1247
1248 · CGI::Application::Plugin::CompressGzip - Add Gzip compression
1249
1250 · CGI::Application::Plugin::LogDispatch - Integration with
1251 Log::Dispatch
1252
1253 · CGI::Application::Plugin::Stream - Help stream files to the browser
1254
1255 · CGI::Application::Plugin::TemplateRunner - Allows for more of an
1256 ASP-style code structure, with the difference that code and HTML
1257 for each screen are in separate files.
1258
1259 · CGI::Application::Plugin::TT - Use Template::Toolkit as an
1260 alternative to HTML::Template.
1261
1262 Consult each plug-in for the exact usage syntax.
1263
1264 Writing Plug-ins
1265 Writing plug-ins is simple. Simply create a new package, and export the
1266 methods that you want to become part of a CGI::Application project. See
1267 CGI::Application::Plugin::ValidateRM for an example.
1268
1269 In order to avoid namespace conflicts within a CGI::Application object,
1270 plugin developers are recommended to use a unique prefix, such as the
1271 name of plugin package, when storing information. For instance:
1272
1273 $app->{__PARAM} = 'foo'; # BAD! Could conflict.
1274 $app->{'MyPlugin::Module::__PARAM'} = 'foo'; # Good.
1275 $app->{'MyPlugin::Module'}{__PARAM} = 'foo'; # Good.
1276
1277 Writing Advanced Plug-ins - Using callbacks
1278 When writing a plug-in, you may want some action to happen
1279 automatically at a particular stage, such as setting up a database
1280 connection or initializing a session. By using these 'callback'
1281 methods, you can register a subroutine to run at a particular phase,
1282 accomplishing this goal.
1283
1284 Callback Examples
1285
1286 # register a callback to the standard CGI::Application hooks
1287 # one of 'init', 'prerun', 'postrun', 'teardown' or 'load_tmpl'
1288 # As a plug-in author, this is probably the only method you need.
1289
1290 # Class-based: callback will persist for all runs of the application
1291 $class->add_callback('init', \&some_other_method);
1292
1293 # Object-based: callback will only last for lifetime of this object
1294 $self->add_callback('prerun', \&some_method);
1295
1296 # If you want to create a new hook location in your application,
1297 # You'll need to know about the following two methods to create
1298 # the hook and call it.
1299
1300 # Create a new hook
1301 $self->new_hook('pretemplate');
1302
1303 # Then later execute all the callbacks registered at this hook
1304 $self->call_hook('pretemplate');
1305
1306 Callback Methods
1307
1308 add_callback()
1309
1310 $self->add_callback ('teardown', \&callback);
1311 $class->add_callback('teardown', 'method');
1312
1313 The add_callback method allows you to register a callback function that
1314 is to be called at the given stage of execution. Valid hooks include
1315 'init', 'prerun', 'postrun' and 'teardown', 'load_tmpl', and any other
1316 hooks defined using the "new_hook" method.
1317
1318 The callback should be a reference to a subroutine or the name of a
1319 method.
1320
1321 If multiple callbacks are added to the same hook, they will all be
1322 executed one after the other. The exact order depends on which class
1323 installed each callback, as described below under Callback Ordering.
1324
1325 Callbacks can either be object-based or class-based, depending upon
1326 whether you call "add_callback" as an object method or a class method:
1327
1328 # add object-based callback
1329 $self->add_callback('teardown', \&callback);
1330
1331 # add class-based callbacks
1332 $class->add_callback('teardown', \&callback);
1333 My::Project->add_callback('teardown', \&callback);
1334
1335 Object-based callbacks are stored in your web application's $c object;
1336 at the end of the request when the $c object goes out of scope, the
1337 callbacks are gone too.
1338
1339 Object-based callbacks are useful for one-time tasks that apply only to
1340 the current running application. For instance you could install a
1341 "teardown" callback to trigger a long-running process to execute at the
1342 end of the current request, after all the HTML has been sent to the
1343 browser.
1344
1345 Class-based callbacks survive for the duration of the running Perl
1346 process. (In a persistent environment such as "mod_perl" or
1347 "PersistentPerl", a single Perl process can serve many web requests.)
1348
1349 Class-based callbacks are useful for plugins to add features to all web
1350 applications.
1351
1352 Another feature of class-based callbacks is that your plugin can create
1353 hooks and add callbacks at any time - even before the web application's
1354 $c object has been initialized. A good place to do this is in your
1355 plugin's "import" subroutine:
1356
1357 package CGI::Application::Plugin::MyPlugin;
1358 use base 'Exporter';
1359 sub import {
1360 my $caller = scalar(caller);
1361 $caller->add_callback('init', 'my_setup');
1362 goto &Exporter::import;
1363 }
1364
1365 Notice that "$caller->add_callback" installs the callback on behalf of
1366 the module that contained the line:
1367
1368 use CGI::Application::Plugin::MyPlugin;
1369
1370 new_hook(HOOK)
1371
1372 $self->new_hook('pretemplate');
1373
1374 The "new_hook()" method can be used to create a new location for
1375 developers to register callbacks. It takes one argument, a hook name.
1376 The hook location is created if it does not already exist. A true value
1377 is always returned.
1378
1379 For an example, CGI::Application::Plugin::TT adds hooks before and
1380 after every template is processed.
1381
1382 See "call_hook(HOOK)" for more details about how hooks are called.
1383
1384 call_hook(HOOK)
1385
1386 $self->call_hook('pretemplate', @args);
1387
1388 The "call_hook" method is used to executed the callbacks that have been
1389 registered at the given hook. It is used in conjunction with the
1390 "new_hook" method which allows you to create a new hook location.
1391
1392 The first argument to "call_hook" is the hook name. Any remaining
1393 arguments are passed to every callback executed at the hook location.
1394 So, a stub for a callback at the 'pretemplate' hook would look like
1395 this:
1396
1397 sub my_hook {
1398 my ($c,@args) = @_;
1399 # ....
1400 }
1401
1402 Note that hooks are semi-public locations. Calling a hook means
1403 executing callbacks that were registered to that hook by the current
1404 object and also those registered by any of the current object's parent
1405 classes. See below for the exact ordering.
1406
1407 Callback Ordering
1408
1409 Object-based callbacks are run before class-based callbacks.
1410
1411 The order of class-based callbacks is determined by the inheritance
1412 tree of the running application. The built-in methods of "cgiapp_init",
1413 "cgiapp_prerun", "cgiapp_postrun", and "teardown" are also executed
1414 this way, according to the ordering below.
1415
1416 In a persistent environment, there might be a lot of applications in
1417 memory at the same time. For instance:
1418
1419 CGI::Application
1420 Other::Project # uses CGI::Application::Plugin::Baz
1421 Other::App # uses CGI::Application::Plugin::Bam
1422
1423 My::Project # uses CGI::Application::Plugin::Foo
1424 My::App # uses CGI::Application::Plugin::Bar
1425
1426 Suppose that each of the above plugins each added a callback to be run
1427 at the 'init' stage:
1428
1429 Plugin init callback
1430 ------ -------------
1431 CGI::Application::Plugin::Baz baz_startup
1432 CGI::Application::Plugin::Bam bam_startup
1433
1434 CGI::Application::Plugin::Foo foo_startup
1435 CGI::Application::Plugin::Bar bar_startup
1436
1437 When "My::App" runs, only "foo_callback" and "bar_callback" will run.
1438 The other callbacks are skipped.
1439
1440 The @ISA list of "My::App" is:
1441
1442 My::App
1443 My::Project
1444 CGI::Application
1445
1446 This order determines the order of callbacks run.
1447
1448 When "call_hook('init')" is run on a "My::App" application, callbacks
1449 installed by these modules are run in order, resulting in:
1450 "bar_startup", "foo_startup", and then finally "cgiapp_init".
1451
1452 If a single class installs more than one callback at the same hook,
1453 then these callbacks are run in the order they were registered (FIFO).
1454
1456 Therese are primary resources available for those who wish to learn
1457 more about CGI::Application and discuss it with others.
1458
1459 Wiki
1460
1461 This is a community built and maintained resource that anyone is
1462 welcome to contribute to. It contains a number of articles of its own
1463 and links to many other CGI::Application related pages:
1464
1465 http://www.cgi-app.org <http://www.cgi-app.org>
1466
1467 Support Mailing List
1468
1469 If you have any questions, comments, bug reports or feature
1470 suggestions, post them to the support mailing list! To join the
1471 mailing list, simply send a blank message to
1472 "cgiapp-subscribe@lists.erlbaum.net".
1473
1474 IRC
1475
1476 You can also drop by "#cgiapp" on "irc.perl.org" with a good chance of
1477 finding some people involved with the project there.
1478
1479 Source Code
1480
1481 This project is managed using the darcs source control system (
1482 http://www.darcs.net/ ). The darcs archive is here:
1483 http://mark.stosberg.com/darcs_hive/cgi-app
1484
1486 o CGI
1487
1488 o HTML::Template
1489
1490 o CGI::Application::Framework - A full-featured web application based
1491 on CGI::Application. http://www.cafweb.org/
1492
1494 If you're interested in finding out more about CGI::Application, the
1495 following articles are available on Perl.com:
1496
1497 Using CGI::Application
1498 http://www.perl.com/pub/a/2001/06/05/cgi.html
1499
1500 Rapid Website Development with CGI::Application
1501 http://www.perl.com/pub/a/2006/10/19/cgi_application.html
1502
1503 Thanks to O'Reilly for publishing these articles, and for the
1504 incredible value they provide to the Perl community!
1505
1507 Jesse Erlbaum <jesse@erlbaum.net>
1508
1509 Mark Stosberg has served as a co-maintainer since version 3.2, with the
1510 help of the numerous contributors documented in the Changes file.
1511
1513 CGI::Application was originally developed by The Erlbaum Group, a
1514 software engineering and consulting firm in New York City.
1515
1516 Thanks to Vanguard Media (http://www.vm.com) for funding the initial
1517 development of this library and for encouraging Jesse Erlbaum to
1518 release it to the world.
1519
1520 Many thanks to Sam Tregar (author of the most excellent HTML::Template
1521 module!) for his innumerable contributions to this module over the
1522 years, and most of all for getting me off my ass to finally get this
1523 thing up on CPAN!
1524
1525 Many other people have contributed specific suggestions or patches,
1526 which are documented in the "Changes" file.
1527
1528 Thanks also to all the members of the CGI-App mailing list! Your
1529 ideas, suggestions, insights (and criticism!) have helped shape this
1530 module immeasurably. (To join the mailing list, simply send a blank
1531 message to "cgiapp-subscribe@lists.erlbaum.net".)
1532
1534 CGI::Application : Framework for building reusable web-applications
1535 Copyright (C) 2000-2003 Jesse Erlbaum <jesse@erlbaum.net>
1536
1537 This module is free software; you can redistribute it and/or modify it
1538 under the terms of either:
1539
1540 a) the GNU General Public License as published by the Free Software
1541 Foundation; either version 1, or (at your option) any later version,
1542
1543 or
1544
1545 b) the "Artistic License" which comes with this module.
1546
1547 This program is distributed in the hope that it will be useful, but
1548 WITHOUT ANY WARRANTY; without even the implied warranty of
1549 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
1550 GNU General Public License or the Artistic License for more details.
1551
1552 You should have received a copy of the Artistic License with this
1553 module, in the file ARTISTIC. If not, I'll be glad to provide one.
1554
1555 You should have received a copy of the GNU General Public License along
1556 with this program; if not, write to the Free Software Foundation, Inc.,
1557 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
1558
1559
1560
1561perl v5.12.3 2011-05-14 CGI::Application(3)