| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package CGI::Prototype; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 3 |  |  | 3 |  | 2885 | use 5.006; | 
|  | 3 |  |  |  |  | 12 |  | 
|  | 3 |  |  |  |  | 121 |  | 
| 4 | 3 |  |  | 3 |  | 16 | use strict; | 
|  | 3 |  |  |  |  | 5 |  | 
|  | 3 |  |  |  |  | 96 |  | 
| 5 | 3 |  |  | 3 |  | 25 | use warnings; | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 114 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 3 |  |  | 3 |  | 21 | use base qw(Class::Prototyped); | 
|  | 3 |  |  |  |  | 4 |  | 
|  | 3 |  |  |  |  | 4467 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | ## no exports | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our $VERSION = '0.9054'; | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | our $_mirror = __PACKAGE__->reflect; # for slots that aren't subs | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =head1 NAME | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | CGI::Prototype - Create a CGI application by subclassing | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | package My::HelloWorld; | 
| 22 |  |  |  |  |  |  | use base CGI::Prototype; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | sub template { \ <<'END_OF_TEMPLATE' } | 
| 25 |  |  |  |  |  |  | [% self.CGI.header; %] | 
| 26 |  |  |  |  |  |  | Hello world at [% USE Date; Date.format(date.now) | html %]! | 
| 27 |  |  |  |  |  |  | END_OF_TEMPLATE | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | My::HelloWorld->activate; | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | The core of every CGI application seems to be roughly the same: | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | =over 4 | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  | =item * | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | Analyze the incoming parameters, cookies, and URLs to determine the | 
| 40 |  |  |  |  |  |  | state of the application (let's call this "dispatch"). | 
| 41 |  |  |  |  |  |  |  | 
| 42 |  |  |  |  |  |  | =item * | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | Based on the current state, analyze the incoming parameters to respond | 
| 45 |  |  |  |  |  |  | to any form submitted ("respond"). | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | =item * | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | From there, decide what response page should be generated, and produce | 
| 50 |  |  |  |  |  |  | it ("render"). | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =back | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | L creates a C engine for doing all | 
| 55 |  |  |  |  |  |  | this, with the right amount of callback hooks to customize the | 
| 56 |  |  |  |  |  |  | process.  Because I'm biased toward Template Toolkit for rendering | 
| 57 |  |  |  |  |  |  | HTML, I've also integrated that as my rendering engine of choice. | 
| 58 |  |  |  |  |  |  | And, being a fan of clean MVC designs, the classes become the | 
| 59 |  |  |  |  |  |  | controllers, and the templates become the views, with clean separation | 
| 60 |  |  |  |  |  |  | of responsibilities, and C a sort of "archetypal" | 
| 61 |  |  |  |  |  |  | controller. | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | You can create the null application by simply I it: | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | use CGI::Prototype; | 
| 66 |  |  |  |  |  |  | CGI::Prototype->activate; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | But this won't be very interesting.  You'll want to subclass this | 
| 69 |  |  |  |  |  |  | class in a C-style manner to override most of its | 
| 70 |  |  |  |  |  |  | behavior.  Slots can be added to add or alter behavior.  You can | 
| 71 |  |  |  |  |  |  | subclass your subclasses when groups of your CGI pages share similar | 
| 72 |  |  |  |  |  |  | behavior.  The possibilities are mind-boggling. | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | Within the templates, C refers to the current controller.  Thus, | 
| 75 |  |  |  |  |  |  | you can define callbacks trivially.  In your template, if you need some | 
| 76 |  |  |  |  |  |  | data, you can pull it as a request: | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | [% my_data = self.get_some_big_data %] | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | which is supplied by simply adding the same slot (method or data) in | 
| 81 |  |  |  |  |  |  | the controlling class: | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | sub get_some_big_data { | 
| 84 |  |  |  |  |  |  | my $self = shift; | 
| 85 |  |  |  |  |  |  | return $self->some_other_method(size => 'big'); | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | And since the classes are hierarchical, you can start out with an | 
| 89 |  |  |  |  |  |  | implementation for one page, then move it to a region or globally | 
| 90 |  |  |  |  |  |  | quickly. | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | Although the name C implies a CGI protocol, I see no | 
| 93 |  |  |  |  |  |  | reason that this would not work with C in a | 
| 94 |  |  |  |  |  |  | C environment, or a direct content handler such as: | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | package My::App; | 
| 97 |  |  |  |  |  |  | use base CGI::Prototype; | 
| 98 |  |  |  |  |  |  | sub handler { | 
| 99 |  |  |  |  |  |  | __PACKAGE__->activate; | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | Note that the C<$r> request object will have to be created if needed | 
| 103 |  |  |  |  |  |  | if you use this approach. | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | =head2 CORE SLOTS | 
| 106 |  |  |  |  |  |  |  | 
| 107 |  |  |  |  |  |  | These slots provide core functionality.  You will probably not | 
| 108 |  |  |  |  |  |  | need to override these. | 
| 109 |  |  |  |  |  |  |  | 
| 110 |  |  |  |  |  |  | =over 4 | 
| 111 |  |  |  |  |  |  |  | 
| 112 |  |  |  |  |  |  | =item activate | 
| 113 |  |  |  |  |  |  |  | 
| 114 |  |  |  |  |  |  | Invoke the C slot to "activate" your application, | 
| 115 |  |  |  |  |  |  | causing it to process the incoming CGI values, select a page to be | 
| 116 |  |  |  |  |  |  | respond to the parameters, which in turn selects a page to render, and | 
| 117 |  |  |  |  |  |  | then responds with that page.  For example, your App might consist | 
| 118 |  |  |  |  |  |  | only of: | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | package My::App; | 
| 121 |  |  |  |  |  |  | use base qw(CGI::Prototype); | 
| 122 |  |  |  |  |  |  | My::App->activate; | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | Again, this will not be interesting, but it shows that the null app | 
| 125 |  |  |  |  |  |  | is easy to create.  Almost always, you will want to override some | 
| 126 |  |  |  |  |  |  | of the "callback" slots below. | 
| 127 |  |  |  |  |  |  |  | 
| 128 |  |  |  |  |  |  | =cut | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | sub activate { | 
| 131 | 4 |  |  | 4 | 1 | 9308 | my $self = shift; | 
| 132 | 4 |  |  |  |  | 8 | eval { | 
| 133 | 4 |  |  |  |  | 22 | $self->prototype_enter; | 
| 134 | 4 |  |  |  |  | 603 | $self->app_enter; | 
| 135 | 4 |  |  |  |  | 34 | my $this_page = $self->dispatch; | 
| 136 | 4 |  |  |  |  | 52 | $this_page->control_enter; | 
| 137 | 4 |  |  |  |  | 103 | $this_page->respond_enter; | 
| 138 | 4 |  |  |  |  | 30 | my $next_page = $this_page->respond; | 
| 139 | 4 |  |  |  |  | 37 | $this_page->respond_leave; | 
| 140 | 4 | 100 |  |  |  | 37 | if ($this_page ne $next_page) { | 
| 141 | 1 |  |  |  |  | 479 | $this_page->control_leave; | 
| 142 | 1 |  |  |  |  | 21 | $next_page->control_enter; | 
| 143 |  |  |  |  |  |  | } | 
| 144 | 4 |  |  |  |  | 125 | $next_page->render_enter; | 
| 145 | 4 |  |  |  |  | 39 | $next_page->render; | 
| 146 | 3 |  |  |  |  | 63 | $next_page->render_leave; | 
| 147 | 3 |  |  |  |  | 33 | $next_page->control_leave; | 
| 148 | 3 |  |  |  |  | 35 | $self->app_leave; | 
| 149 | 3 |  |  |  |  | 39 | $self->prototype_leave; | 
| 150 |  |  |  |  |  |  | }; | 
| 151 | 4 | 100 |  |  |  | 36098 | $self->error($@) if $@;	# failed something, go to safe mode | 
| 152 |  |  |  |  |  |  | } | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =item CGI | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | Invoking C<< $self->CGI >> gives you access to the CGI.pm object | 
| 157 |  |  |  |  |  |  | representing the incoming parameters and other CGI.pm-related values. | 
| 158 |  |  |  |  |  |  | For example, | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | $self->CGI->self_url | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | generates a self-referencing URL.  From a template, this is: | 
| 163 |  |  |  |  |  |  |  | 
| 164 |  |  |  |  |  |  | [% self.CGI.self_url %] | 
| 165 |  |  |  |  |  |  |  | 
| 166 |  |  |  |  |  |  | for the same thing. | 
| 167 |  |  |  |  |  |  |  | 
| 168 |  |  |  |  |  |  | See C for how this slot gets established. | 
| 169 |  |  |  |  |  |  |  | 
| 170 |  |  |  |  |  |  | =cut | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | $_mirror->addSlot | 
| 173 | 2 |  |  | 2 |  | 4509 | (CGI => sub { die shift, "->initialize_CGI not called" }); | 
| 174 |  |  |  |  |  |  |  | 
| 175 |  |  |  |  |  |  | =item render | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | The C method uses the results from C and C | 
| 178 |  |  |  |  |  |  | to process a selected template through Template Toolkit.  If the | 
| 179 |  |  |  |  |  |  | result does not throw an error, C<< $self->display >> is called to | 
| 180 |  |  |  |  |  |  | show the result. | 
| 181 |  |  |  |  |  |  |  | 
| 182 |  |  |  |  |  |  | =cut | 
| 183 |  |  |  |  |  |  |  | 
| 184 |  |  |  |  |  |  | sub render { | 
| 185 | 4 |  |  | 4 | 1 | 7 | my $self = shift; | 
| 186 | 4 |  |  |  |  | 27 | my $tt = $self->engine; | 
| 187 | 4 |  |  |  |  | 92712 | my $self_object = $self->reflect->object; # in case we have a classname | 
| 188 | 4 | 100 |  |  |  | 428 | $tt->process($self->template, { self => $self_object }, \my $output) | 
| 189 |  |  |  |  |  |  | or die $tt->error;	# passes Template::Exception upward | 
| 190 | 3 |  |  |  |  | 67822 | $self->display($output); | 
| 191 |  |  |  |  |  |  | } | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | =item display | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  | The C method is called to render the output of the template | 
| 196 |  |  |  |  |  |  | under normal circumstances, normally dumping the first parameter to | 
| 197 |  |  |  |  |  |  | C.  Test harnesses may override this method to cause the | 
| 198 |  |  |  |  |  |  | output to appear into a variable, but normally this method is left | 
| 199 |  |  |  |  |  |  | alone. | 
| 200 |  |  |  |  |  |  |  | 
| 201 |  |  |  |  |  |  | =cut | 
| 202 |  |  |  |  |  |  |  | 
| 203 |  |  |  |  |  |  | sub display {			# override this to grab output for testing | 
| 204 | 2 |  |  | 2 | 1 | 15 | my $self = shift; | 
| 205 | 2 |  |  |  |  | 6 | my $output = shift; | 
| 206 | 2 |  |  |  |  | 152 | print $output; | 
| 207 |  |  |  |  |  |  | } | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | =item param | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | The C method is a convenience method that maps to | 
| 212 |  |  |  |  |  |  | C<< $self->CGI->param >>, because accessing params is a very common thing. | 
| 213 |  |  |  |  |  |  |  | 
| 214 |  |  |  |  |  |  | =cut | 
| 215 |  |  |  |  |  |  |  | 
| 216 |  |  |  |  |  |  | sub param { | 
| 217 | 1 |  |  | 1 | 1 | 984 | shift->CGI->param(@_);	# convenience method | 
| 218 |  |  |  |  |  |  | } | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | =item interstitial | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | B | 
| 223 |  |  |  |  |  |  | and subject to change.> | 
| 224 |  |  |  |  |  |  |  | 
| 225 |  |  |  |  |  |  | Use this in your per-page respond methods if you have a lot of heavy | 
| 226 |  |  |  |  |  |  | processing to perform.  For example, suppose you're deleting | 
| 227 |  |  |  |  |  |  | something, and it takes 5 seconds to do the first step, and 3 seconds | 
| 228 |  |  |  |  |  |  | to do the second step, and then you want to go back to normal web | 
| 229 |  |  |  |  |  |  | interaction.  Simulating the heavy lifting with sleep, we get: | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | my $p = $self->interstitial | 
| 232 |  |  |  |  |  |  | ({ message => "Your delete is being processed...", | 
| 233 |  |  |  |  |  |  | action => sub { sleep 5 }, | 
| 234 |  |  |  |  |  |  | }, | 
| 235 |  |  |  |  |  |  | { message => "Just a few seconds more....", | 
| 236 |  |  |  |  |  |  | action => sub { sleep 3 }, | 
| 237 |  |  |  |  |  |  | }, | 
| 238 |  |  |  |  |  |  | ); | 
| 239 |  |  |  |  |  |  | return $p if $p; | 
| 240 |  |  |  |  |  |  |  | 
| 241 |  |  |  |  |  |  | C returns either a page that should be returned so that | 
| 242 |  |  |  |  |  |  | it can be rendered (inside a wrapper that provides the standard top | 
| 243 |  |  |  |  |  |  | and bottom of your application page), or C. | 
| 244 |  |  |  |  |  |  |  | 
| 245 |  |  |  |  |  |  | The list passed to | 
| 246 |  |  |  |  |  |  | C should be a series of hashrefs with one or more | 
| 247 |  |  |  |  |  |  | parameters reflecting the steps: | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | =over 4 | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  | =item message | 
| 252 |  |  |  |  |  |  |  | 
| 253 |  |  |  |  |  |  | What the user should see while the step is computing. | 
| 254 |  |  |  |  |  |  | (Default: C.) | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | =item action | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | A coderef with the action performed server-side during the message. | 
| 259 |  |  |  |  |  |  | (Default: no action.) | 
| 260 |  |  |  |  |  |  |  | 
| 261 |  |  |  |  |  |  | =item delay | 
| 262 |  |  |  |  |  |  |  | 
| 263 |  |  |  |  |  |  | The number of seconds the browser should wait before initiating | 
| 264 |  |  |  |  |  |  | the next connection, triggering the start of C. | 
| 265 |  |  |  |  |  |  | (Default: 0 seconds.) | 
| 266 |  |  |  |  |  |  |  | 
| 267 |  |  |  |  |  |  | =back | 
| 268 |  |  |  |  |  |  |  | 
| 269 |  |  |  |  |  |  | The user sees the first message at the first call to C | 
| 270 |  |  |  |  |  |  | (via the first returned page), at which time a meta-refresh will | 
| 271 |  |  |  |  |  |  | immediately repost the same parameters as on the call that got you | 
| 272 |  |  |  |  |  |  | here.  (Thus, it's important not to have changed the params yet, or | 
| 273 |  |  |  |  |  |  | you might end up in a different part of your code.)  When the call to | 
| 274 |  |  |  |  |  |  | C is re-executed, the first coderef is then performed. | 
| 275 |  |  |  |  |  |  | At the end of that coderef, the second interstitial page is returned, | 
| 276 |  |  |  |  |  |  | and the user sees the second message, which then performs the next | 
| 277 |  |  |  |  |  |  | meta-refresh, which gets us back to this call to C again | 
| 278 |  |  |  |  |  |  | (whew).  The second coderef is executed while the user is seeing the | 
| 279 |  |  |  |  |  |  | second message, and then C returns C, letting us | 
| 280 |  |  |  |  |  |  | roll through to the final code.  Slick. | 
| 281 |  |  |  |  |  |  |  | 
| 282 |  |  |  |  |  |  | =cut | 
| 283 |  |  |  |  |  |  |  | 
| 284 |  |  |  |  |  |  | sub interstitial { | 
| 285 | 0 |  |  | 0 | 1 | 0 | my $self = shift; | 
| 286 | 0 |  |  |  |  | 0 | my @steps = @_; | 
| 287 |  |  |  |  |  |  |  | 
| 288 | 0 |  |  |  |  | 0 | my $cip = $self->config_interstitial_param; | 
| 289 | 0 |  | 0 |  |  | 0 | my $step = $self->param($cip) || 0; | 
| 290 |  |  |  |  |  |  | ## todo: validate $state is a small integer in range | 
| 291 |  |  |  |  |  |  |  | 
| 292 | 0 | 0 | 0 |  |  | 0 | if ($step >= 1 and $step <= @steps) { # we got work to do | 
| 293 | 0 | 0 |  |  |  | 0 | if (defined (my $code = $steps[$step - 1]{action})) { | 
| 294 | 0 |  |  |  |  | 0 | $code->();		# run the action | 
| 295 |  |  |  |  |  |  | } | 
| 296 |  |  |  |  |  |  | } | 
| 297 |  |  |  |  |  |  |  | 
| 298 |  |  |  |  |  |  | ## now show the user the message during the next step | 
| 299 | 0 |  |  |  |  | 0 | $step++; | 
| 300 |  |  |  |  |  |  |  | 
| 301 | 0 | 0 | 0 |  |  | 0 | unless ($step >= 1 and $step <= @steps) { | 
| 302 | 0 |  |  |  |  | 0 | $self->CGI->delete($cip); | 
| 303 | 0 |  |  |  |  | 0 | return undef;		# signal steps being done | 
| 304 |  |  |  |  |  |  | } | 
| 305 | 0 |  |  |  |  | 0 | $self->param($cip, $step); | 
| 306 |  |  |  |  |  |  |  | 
| 307 | 0 |  | 0 |  |  | 0 | my $message = $steps[$step - 1]{message} || "Working..."; | 
| 308 | 0 |  | 0 |  |  | 0 | my $delay = $steps[$step - 1]{delay} || 0; | 
| 309 |  |  |  |  |  |  |  | 
| 310 |  |  |  |  |  |  | ## generate interstitial page as light class | 
| 311 | 0 |  |  |  |  | 0 | return $self->new( | 
| 312 |  |  |  |  |  |  | url => $self->CGI->self_url, | 
| 313 |  |  |  |  |  |  | message => $message, | 
| 314 |  |  |  |  |  |  | delay => $delay, | 
| 315 |  |  |  |  |  |  | shortname => $self->shortname, | 
| 316 |  |  |  |  |  |  | template => \ <<'', | 
| 317 |  |  |  |  |  |  |  | 
| 318 |  |  |  |  |  |  | [% self.message %] 
 | 
| 319 |  |  |  |  |  |  | (If your browser isn't automatically trying to fetch a page right now, | 
| 320 |  |  |  |  |  |  | please continue manually.) | 
| 321 |  |  |  |  |  |  |  | 
| 322 |  |  |  |  |  |  | ); | 
| 323 |  |  |  |  |  |  | } | 
| 324 |  |  |  |  |  |  |  | 
| 325 |  |  |  |  |  |  | =item config_interstitial_param | 
| 326 |  |  |  |  |  |  |  | 
| 327 |  |  |  |  |  |  | This parameter is used by C to determine the | 
| 328 |  |  |  |  |  |  | processing step.  You should ensure that the name doesn't conflict | 
| 329 |  |  |  |  |  |  | with any other param that you might need. | 
| 330 |  |  |  |  |  |  |  | 
| 331 |  |  |  |  |  |  | The default value is C<_interstitial>. | 
| 332 |  |  |  |  |  |  |  | 
| 333 |  |  |  |  |  |  | =cut | 
| 334 |  |  |  |  |  |  |  | 
| 335 | 0 |  |  | 0 | 1 | 0 | sub config_interstitial_param { "_interstitial" } | 
| 336 |  |  |  |  |  |  |  | 
| 337 |  |  |  |  |  |  | =back | 
| 338 |  |  |  |  |  |  |  | 
| 339 |  |  |  |  |  |  | =head2 CALLBACK SLOTS | 
| 340 |  |  |  |  |  |  |  | 
| 341 |  |  |  |  |  |  | =over 4 | 
| 342 |  |  |  |  |  |  |  | 
| 343 |  |  |  |  |  |  | =item engine | 
| 344 |  |  |  |  |  |  |  | 
| 345 |  |  |  |  |  |  | The engine returns a Template object that will be generating any | 
| 346 |  |  |  |  |  |  | response.  The object is computed lazily (with autoloading) when | 
| 347 |  |  |  |  |  |  | needed. | 
| 348 |  |  |  |  |  |  |  | 
| 349 |  |  |  |  |  |  | The Template object is passed the configuration returned from | 
| 350 |  |  |  |  |  |  | the C callback. | 
| 351 |  |  |  |  |  |  |  | 
| 352 |  |  |  |  |  |  | =cut | 
| 353 |  |  |  |  |  |  |  | 
| 354 |  |  |  |  |  |  | $_mirror->addSlot | 
| 355 |  |  |  |  |  |  | ([qw(engine FIELD autoload)] => sub { | 
| 356 |  |  |  |  |  |  | my $self = shift; | 
| 357 |  |  |  |  |  |  | require Template; | 
| 358 |  |  |  |  |  |  | Template->new($self->engine_config) | 
| 359 |  |  |  |  |  |  | or die "Creating tt: $Template::ERROR\n"; | 
| 360 |  |  |  |  |  |  | }); | 
| 361 |  |  |  |  |  |  |  | 
| 362 |  |  |  |  |  |  | =item engine_config | 
| 363 |  |  |  |  |  |  |  | 
| 364 |  |  |  |  |  |  | Returns a hashref of desired parameters to pass to | 
| 365 |  |  |  |  |  |  | the C C method as a configuration.  Defaults | 
| 366 |  |  |  |  |  |  | to an empty hash. | 
| 367 |  |  |  |  |  |  |  | 
| 368 |  |  |  |  |  |  | =cut | 
| 369 |  |  |  |  |  |  |  | 
| 370 |  |  |  |  |  |  | sub engine_config { | 
| 371 | 3 |  |  | 3 | 1 | 35 | return {}; | 
| 372 |  |  |  |  |  |  | } | 
| 373 |  |  |  |  |  |  |  | 
| 374 |  |  |  |  |  |  | =item prototype_enter | 
| 375 |  |  |  |  |  |  |  | 
| 376 |  |  |  |  |  |  | Called when the prototype mechanism is entered, at the very beginning | 
| 377 |  |  |  |  |  |  | of each hit.  Defaults to calling C<->initialize_CGI>, which see. | 
| 378 |  |  |  |  |  |  |  | 
| 379 |  |  |  |  |  |  | Generally, you should not override this method. If you do, be sure to | 
| 380 |  |  |  |  |  |  | call the SUPER method, in case future versions of this module need | 
| 381 |  |  |  |  |  |  | additional initialization. | 
| 382 |  |  |  |  |  |  |  | 
| 383 |  |  |  |  |  |  | =cut | 
| 384 |  |  |  |  |  |  |  | 
| 385 |  |  |  |  |  |  | sub prototype_enter { | 
| 386 | 2 |  |  | 2 | 1 | 12 | shift->initialize_CGI; | 
| 387 |  |  |  |  |  |  | } | 
| 388 |  |  |  |  |  |  |  | 
| 389 |  |  |  |  |  |  | =item prototype_leave | 
| 390 |  |  |  |  |  |  |  | 
| 391 |  |  |  |  |  |  | Called when the prototype mechanism is exited, at the very end of each hit. | 
| 392 |  |  |  |  |  |  | Defaults to no action. | 
| 393 |  |  |  |  |  |  |  | 
| 394 |  |  |  |  |  |  | Generally, you should not override this method. If you do, be sure to | 
| 395 |  |  |  |  |  |  | call the SUPER method, in case future versions of this module need | 
| 396 |  |  |  |  |  |  | additional teardown. | 
| 397 |  |  |  |  |  |  |  | 
| 398 |  |  |  |  |  |  | =cut | 
| 399 |  |  |  |  |  |  |  | 
| 400 | 1 |  |  | 1 | 1 | 3 | sub prototype_leave {} | 
| 401 |  |  |  |  |  |  |  | 
| 402 |  |  |  |  |  |  | =item initialize_CGI | 
| 403 |  |  |  |  |  |  |  | 
| 404 |  |  |  |  |  |  | Sets up the CGI slot as an autoload, defaulting to creating a new | 
| 405 |  |  |  |  |  |  | CGI.pm object.  Called from C. | 
| 406 |  |  |  |  |  |  |  | 
| 407 |  |  |  |  |  |  | =cut | 
| 408 |  |  |  |  |  |  |  | 
| 409 |  |  |  |  |  |  | sub initialize_CGI { | 
| 410 | 2 |  |  | 2 | 1 | 4 | my $self = shift; | 
| 411 |  |  |  |  |  |  | $self->reflect->addSlot | 
| 412 |  |  |  |  |  |  | ([qw(CGI FIELD autoload)] => sub { | 
| 413 | 1 |  |  | 1 |  | 46878 | require CGI; | 
| 414 | 1 |  |  |  |  | 21973 | CGI::_reset_globals(); | 
| 415 | 1 |  |  |  |  | 25 | CGI->new; | 
| 416 | 2 |  |  |  |  | 12 | }); | 
| 417 |  |  |  |  |  |  | } | 
| 418 |  |  |  |  |  |  |  | 
| 419 |  |  |  |  |  |  | =item app_enter | 
| 420 |  |  |  |  |  |  |  | 
| 421 |  |  |  |  |  |  | Called when the application is entered, at the very beginning of each | 
| 422 |  |  |  |  |  |  | hit.  Defaults to no action. | 
| 423 |  |  |  |  |  |  |  | 
| 424 |  |  |  |  |  |  | =cut | 
| 425 |  |  |  |  |  |  |  | 
| 426 | 2 |  |  | 2 | 1 | 5 | sub app_enter {} | 
| 427 |  |  |  |  |  |  |  | 
| 428 |  |  |  |  |  |  | =item app_leave | 
| 429 |  |  |  |  |  |  |  | 
| 430 |  |  |  |  |  |  | Called when the application is left, at the very end of each hit. | 
| 431 |  |  |  |  |  |  | Defaults to no action. | 
| 432 |  |  |  |  |  |  |  | 
| 433 |  |  |  |  |  |  | =cut | 
| 434 |  |  |  |  |  |  |  | 
| 435 | 1 |  |  | 1 | 1 | 1 | sub app_leave {} | 
| 436 |  |  |  |  |  |  |  | 
| 437 |  |  |  |  |  |  | =item control_enter | 
| 438 |  |  |  |  |  |  |  | 
| 439 |  |  |  |  |  |  | Called when a page gains control, either at the beginning for a | 
| 440 |  |  |  |  |  |  | response, or in the middle when switched for rendering.  Defaults to | 
| 441 |  |  |  |  |  |  | nothing. | 
| 442 |  |  |  |  |  |  |  | 
| 443 |  |  |  |  |  |  | This is a great place to hang per-page initialization, because you'll | 
| 444 |  |  |  |  |  |  | get this callback at most once per hit. | 
| 445 |  |  |  |  |  |  |  | 
| 446 |  |  |  |  |  |  | =cut | 
| 447 |  |  |  |  |  |  |  | 
| 448 | 2 |  |  | 2 | 1 | 5 | sub control_enter {} | 
| 449 |  |  |  |  |  |  |  | 
| 450 |  |  |  |  |  |  | =item control_leave | 
| 451 |  |  |  |  |  |  |  | 
| 452 |  |  |  |  |  |  | Called when a page loses control, either after a response phase | 
| 453 |  |  |  |  |  |  | because we're switching to a new page, or render phase after we've | 
| 454 |  |  |  |  |  |  | delivered the new text to the browser. | 
| 455 |  |  |  |  |  |  |  | 
| 456 |  |  |  |  |  |  | This is a great place to hang per-page teardown, because you'll get | 
| 457 |  |  |  |  |  |  | this callback at most once per hit. | 
| 458 |  |  |  |  |  |  |  | 
| 459 |  |  |  |  |  |  | =cut | 
| 460 |  |  |  |  |  |  |  | 
| 461 | 1 |  |  | 1 | 1 | 2 | sub control_leave {} | 
| 462 |  |  |  |  |  |  |  | 
| 463 |  |  |  |  |  |  | =item render_enter | 
| 464 |  |  |  |  |  |  |  | 
| 465 |  |  |  |  |  |  | Called when a page gains control specifically for rendering (delivering | 
| 466 |  |  |  |  |  |  | text to the browser), just after C if needed. | 
| 467 |  |  |  |  |  |  |  | 
| 468 |  |  |  |  |  |  | =cut | 
| 469 |  |  |  |  |  |  |  | 
| 470 | 2 |  |  | 2 | 1 | 4 | sub render_enter {} | 
| 471 |  |  |  |  |  |  |  | 
| 472 |  |  |  |  |  |  | =item render_leave | 
| 473 |  |  |  |  |  |  |  | 
| 474 |  |  |  |  |  |  | Called when a page loses control specifically for rendering (delivering | 
| 475 |  |  |  |  |  |  | text to the browser), just before C. | 
| 476 |  |  |  |  |  |  |  | 
| 477 |  |  |  |  |  |  | =cut | 
| 478 |  |  |  |  |  |  |  | 
| 479 | 1 |  |  | 1 | 1 | 3 | sub render_leave {} | 
| 480 |  |  |  |  |  |  |  | 
| 481 |  |  |  |  |  |  | =item respond_enter | 
| 482 |  |  |  |  |  |  |  | 
| 483 |  |  |  |  |  |  | Called when a page gains control specifically for responding | 
| 484 |  |  |  |  |  |  | (understanding the incoming parameters, and deciding what page should | 
| 485 |  |  |  |  |  |  | render the response), just after C. | 
| 486 |  |  |  |  |  |  |  | 
| 487 |  |  |  |  |  |  | =cut | 
| 488 |  |  |  |  |  |  |  | 
| 489 | 2 |  |  | 2 | 1 | 4 | sub respond_enter {} | 
| 490 |  |  |  |  |  |  |  | 
| 491 |  |  |  |  |  |  | =item respond_leave | 
| 492 |  |  |  |  |  |  |  | 
| 493 |  |  |  |  |  |  | Called when a page loses control specifically for rendering | 
| 494 |  |  |  |  |  |  | (understanding the incoming parameters, and deciding what page should | 
| 495 |  |  |  |  |  |  | render the response), just before C (if needed). | 
| 496 |  |  |  |  |  |  |  | 
| 497 |  |  |  |  |  |  | =cut | 
| 498 |  |  |  |  |  |  |  | 
| 499 | 2 |  |  | 2 | 1 | 3 | sub respond_leave {} | 
| 500 |  |  |  |  |  |  |  | 
| 501 |  |  |  |  |  |  | =item template | 
| 502 |  |  |  |  |  |  |  | 
| 503 |  |  |  |  |  |  | Delivers a template document object (something compatible to the | 
| 504 |  |  |  |  |  |  | C C method, such as a C or a | 
| 505 |  |  |  |  |  |  | filehandle or a reference to a scalar).  The default is a simple "this | 
| 506 |  |  |  |  |  |  | page intentionally left blank" template. | 
| 507 |  |  |  |  |  |  |  | 
| 508 |  |  |  |  |  |  | When rendered, the B extra global variable passed into the | 
| 509 |  |  |  |  |  |  | template is the C variable, representing the controller object. | 
| 510 |  |  |  |  |  |  | However, as seen earlier, this is sufficient to allow access to | 
| 511 |  |  |  |  |  |  | anything you need from the template, thanks to Template Toolkit's | 
| 512 |  |  |  |  |  |  | ability to call methods on an object and understand the results. | 
| 513 |  |  |  |  |  |  |  | 
| 514 |  |  |  |  |  |  | For example, to get at the C parameter: | 
| 515 |  |  |  |  |  |  |  | 
| 516 |  |  |  |  |  |  | The barney field is [% self.param("barney") | html %]. | 
| 517 |  |  |  |  |  |  |  | 
| 518 |  |  |  |  |  |  | =cut | 
| 519 |  |  |  |  |  |  |  | 
| 520 |  |  |  |  |  |  | sub template { | 
| 521 | 1 |  |  | 1 | 1 | 9 | \ '[% self.CGI.header %]This page intentionally left blank.'; | 
| 522 |  |  |  |  |  |  | } | 
| 523 |  |  |  |  |  |  |  | 
| 524 |  |  |  |  |  |  | =item error | 
| 525 |  |  |  |  |  |  |  | 
| 526 |  |  |  |  |  |  | Called if an uncaught error is triggered in any of the other steps, | 
| 527 |  |  |  |  |  |  | passing the error text or object as the first method parameter.  The | 
| 528 |  |  |  |  |  |  | default callback simply displays the output to the browser, which is | 
| 529 |  |  |  |  |  |  | highly insecure and should be overridden, perhaps with something that | 
| 530 |  |  |  |  |  |  | logs the error and puts up a generic error message with an incident | 
| 531 |  |  |  |  |  |  | code for tracking. | 
| 532 |  |  |  |  |  |  |  | 
| 533 |  |  |  |  |  |  | =cut | 
| 534 |  |  |  |  |  |  |  | 
| 535 |  |  |  |  |  |  | sub error { | 
| 536 | 1 |  |  | 1 | 1 | 22 | my $self = shift; | 
| 537 | 1 |  |  |  |  | 2 | my $error = shift; | 
| 538 | 1 |  |  |  |  | 4 | $self->display("Content-type: text/plain\n\nERROR: $error"); | 
| 539 |  |  |  |  |  |  | } | 
| 540 |  |  |  |  |  |  |  | 
| 541 |  |  |  |  |  |  | =item dispatch | 
| 542 |  |  |  |  |  |  |  | 
| 543 |  |  |  |  |  |  | Called to analyze the incoming parameters to define which page object | 
| 544 |  |  |  |  |  |  | gets control based on the incoming CGI parameters. | 
| 545 |  |  |  |  |  |  |  | 
| 546 |  |  |  |  |  |  | This callback B a page object (the object taking control | 
| 547 |  |  |  |  |  |  | during the response phase).  By default, this callback returns the | 
| 548 |  |  |  |  |  |  | application itself. | 
| 549 |  |  |  |  |  |  |  | 
| 550 |  |  |  |  |  |  | =cut | 
| 551 |  |  |  |  |  |  |  | 
| 552 |  |  |  |  |  |  | sub dispatch { | 
| 553 | 2 |  |  | 2 | 1 | 3 | my $self = shift; | 
| 554 | 2 |  |  |  |  | 4 | return $self;		# do nothing, stay here | 
| 555 |  |  |  |  |  |  | } | 
| 556 |  |  |  |  |  |  |  | 
| 557 |  |  |  |  |  |  | =item respond | 
| 558 |  |  |  |  |  |  |  | 
| 559 |  |  |  |  |  |  | Called to determine how to respond specifically to this set of | 
| 560 |  |  |  |  |  |  | incoming parameters.  Probably updates databases and such. | 
| 561 |  |  |  |  |  |  |  | 
| 562 |  |  |  |  |  |  | This callback B a page object (the object taking control | 
| 563 |  |  |  |  |  |  | during the render phase).  By default, this callback returns the same | 
| 564 |  |  |  |  |  |  | object that had control during the response phase ("stay here" logic), | 
| 565 |  |  |  |  |  |  | which works most of the time. | 
| 566 |  |  |  |  |  |  |  | 
| 567 |  |  |  |  |  |  | =cut | 
| 568 |  |  |  |  |  |  |  | 
| 569 |  |  |  |  |  |  | sub respond { | 
| 570 | 2 |  |  | 2 | 1 | 5 | my $self = shift; | 
| 571 | 2 |  |  |  |  | 9 | return $self;		# do nothing, stay here | 
| 572 |  |  |  |  |  |  | } | 
| 573 |  |  |  |  |  |  |  | 
| 574 |  |  |  |  |  |  | =back | 
| 575 |  |  |  |  |  |  |  | 
| 576 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 577 |  |  |  |  |  |  |  | 
| 578 |  |  |  |  |  |  | L, L, | 
| 579 |  |  |  |  |  |  | L. | 
| 580 |  |  |  |  |  |  |  | 
| 581 |  |  |  |  |  |  | =head1 BUG REPORTS | 
| 582 |  |  |  |  |  |  |  | 
| 583 |  |  |  |  |  |  | Please report any bugs or feature requests to | 
| 584 |  |  |  |  |  |  | bug-cgi-prototype@rt.cpan.org, or through the web interface at | 
| 585 |  |  |  |  |  |  | http://rt.cpan.org. I will be notified, and then you'll automatically | 
| 586 |  |  |  |  |  |  | be notified of progress on your bug as I make changes. | 
| 587 |  |  |  |  |  |  |  | 
| 588 |  |  |  |  |  |  | =head1 AUTHOR | 
| 589 |  |  |  |  |  |  |  | 
| 590 |  |  |  |  |  |  | Randal L. Schwartz, Emerlyn@stonehenge.comE | 
| 591 |  |  |  |  |  |  |  | 
| 592 |  |  |  |  |  |  | Special thanks to Geekcruises.com and an unnamed large university | 
| 593 |  |  |  |  |  |  | for providing funding for the development of this module. | 
| 594 |  |  |  |  |  |  |  | 
| 595 |  |  |  |  |  |  | =head1 COPYRIGHT AND LICENSE | 
| 596 |  |  |  |  |  |  |  | 
| 597 |  |  |  |  |  |  | Copyright (C) 2003, 2004, 2005 by Randal L. Schwartz | 
| 598 |  |  |  |  |  |  |  | 
| 599 |  |  |  |  |  |  | This library is free software; you can redistribute it and/or modify | 
| 600 |  |  |  |  |  |  | it under the same terms as Perl itself, either Perl version 5.8.5 or, | 
| 601 |  |  |  |  |  |  | at your option, any later version of Perl 5 you may have available. | 
| 602 |  |  |  |  |  |  |  | 
| 603 |  |  |  |  |  |  | =cut | 
| 604 |  |  |  |  |  |  |  | 
| 605 |  |  |  |  |  |  | 1; |