Commit | Line | Data |
5c33dda5 |
1 | package Web::Simple; |
2 | |
8bd060f4 |
3 | use strictures 1; |
8c4ffad3 |
4 | use 5.008; |
8bd060f4 |
5 | use warnings::illegalproto (); |
876e62e1 |
6 | use Moo (); |
7 | use Web::Dispatch::Wrapper (); |
8c4ffad3 |
8 | |
d57f3db4 |
9 | our $VERSION = '0.010'; |
5c33dda5 |
10 | |
44db8e76 |
11 | sub import { |
5c33dda5 |
12 | my ($class, $app_package) = @_; |
876e62e1 |
13 | $app_package ||= caller; |
14 | $class->_export_into($app_package); |
15 | eval "package $app_package; use Web::Dispatch::Wrapper; use Moo; 1" |
16 | or die "Failed to setup app package: $@"; |
445b3ea0 |
17 | strictures->import; |
8bd060f4 |
18 | warnings::illegalproto->unimport; |
5c33dda5 |
19 | } |
20 | |
21 | sub _export_into { |
22 | my ($class, $app_package) = @_; |
23 | { |
24 | no strict 'refs'; |
c7b1c57f |
25 | *{"${app_package}::PSGI_ENV"} = sub () { -1 }; |
5c33dda5 |
26 | require Web::Simple::Application; |
27 | unshift(@{"${app_package}::ISA"}, 'Web::Simple::Application'); |
28 | } |
b7063124 |
29 | (my $name = $app_package) =~ s/::/\//g; |
30 | $INC{"${name}.pm"} = 'Set by "use Web::Simple;" invocation'; |
5c33dda5 |
31 | } |
32 | |
7401408e |
33 | =head1 NAME |
34 | |
35 | Web::Simple - A quick and easy way to build simple web applications |
36 | |
7401408e |
37 | |
38 | =head1 SYNOPSIS |
39 | |
05ad188d |
40 | #!/usr/bin/env perl |
7401408e |
41 | |
4f83bde7 |
42 | package HelloWorld; |
43 | use Web::Simple |
44 | |
45 | sub dispatch_request { |
46 | sub (GET) { |
47 | [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ] |
48 | }, |
49 | sub () { |
50 | [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] |
445b3ea0 |
51 | } |
7401408e |
52 | } |
53 | |
54 | HelloWorld->run_if_script; |
55 | |
05ad188d |
56 | If you save this file into your cgi-bin as C<hello-world.cgi> and then visit: |
7401408e |
57 | |
58 | http://my.server.name/cgi-bin/hello-world.cgi/ |
59 | |
4f83bde7 |
60 | you'll get the "Hello world!" string output to your browser. At the same time |
61 | this file will also act as a class module, so you can save it as HelloWorld.pm |
62 | and use it as-is in test scripts or other deployment mechanisms. |
63 | |
ca30a017 |
64 | Note that you should retain the ->run_if_script even if your app is a |
65 | module, since this additionally makes it valid as a .psgi file, which can |
66 | be extremely useful during development. |
67 | |
4f83bde7 |
68 | For more complex examples and non-CGI deployment, see |
69 | L<Web::Simple::Deployment>. To get help with L<Web::Simple>, please connect to |
70 | the irc.perl.org IRC network and join #web-simple. |
7401408e |
71 | |
fb771406 |
72 | =head1 DESCRIPTION |
7401408e |
73 | |
6a4808bf |
74 | The philosophy of L<Web::Simple> is to keep to an absolute bare minimum for |
7401408e |
75 | everything. It is not designed to be used for large scale applications; |
76 | the L<Catalyst> web framework already works very nicely for that and is |
77 | a far more mature, well supported piece of software. |
78 | |
79 | However, if you have an application that only does a couple of things, and |
3895385d |
80 | want to not have to think about complexities of deployment, then L<Web::Simple> |
7401408e |
81 | might be just the thing for you. |
82 | |
6a4808bf |
83 | The only public interface the L<Web::Simple> module itself provides is an |
84 | C<import> based one: |
7401408e |
85 | |
86 | use Web::Simple 'NameOfApplication'; |
87 | |
6a4808bf |
88 | This sets up your package (in this case "NameOfApplication" is your package) |
3895385d |
89 | so that it inherits from L<Web::Simple::Application> and imports L<strictures>, |
38d5b336 |
90 | as well as installs a C<PSGI_ENV> constant for convenience, as well as some |
3895385d |
91 | other subroutines. |
92 | |
6a4808bf |
93 | Importing L<strictures> will automatically make your code use the C<strict> and |
3895385d |
94 | C<warnings> pragma, so you can skip the usual: |
7401408e |
95 | |
96 | use strict; |
3895385d |
97 | use warnings FATAL => 'aa'; |
7401408e |
98 | |
99 | provided you 'use Web::Simple' at the top of the file. Note that we turn |
100 | on *fatal* warnings so if you have any warnings at any point from the file |
101 | that you did 'use Web::Simple' in, then your application will die. This is, |
102 | so far, considered a feature. |
103 | |
a5006b25 |
104 | When we inherit from L<Web::Simple::Application> we also use L<Moo>, which is |
3895385d |
105 | the the equivalent of: |
7401408e |
106 | |
107 | { |
108 | package NameOfApplication; |
445b3ea0 |
109 | use Moo; |
110 | extends 'Web::Simple::Application'; |
7401408e |
111 | } |
112 | |
6a4808bf |
113 | So you can use L<Moo> features in your application, such as creating attributes |
114 | using the C<has> subroutine, etc. Please see the documentation for L<Moo> for |
115 | more information. |
116 | |
445b3ea0 |
117 | It also exports the following subroutines for use in dispatchers: |
7401408e |
118 | |
74afe4b7 |
119 | response_filter { ... }; |
7401408e |
120 | |
121 | redispatch_to '/somewhere'; |
122 | |
b7063124 |
123 | Finally, import sets |
124 | |
125 | $INC{"NameOfApplication.pm"} = 'Set by "use Web::Simple;" invocation'; |
126 | |
127 | so that perl will not attempt to load the application again even if |
128 | |
129 | require NameOfApplication; |
130 | |
131 | is encountered in other code. |
132 | |
3583ca04 |
133 | =head1 DISPATCH STRATEGY |
134 | |
6a4808bf |
135 | L<Web::Simple> despite being straightforward to use, has a powerful system |
3895385d |
136 | for matching all sorts of incoming URLs to one or more subroutines. These |
137 | subroutines can be simple actions to take for a given URL, or something |
138 | more complicated, including entire L<Plack> applications, L<Plack::Middleware> |
139 | and nested subdispatchers. |
140 | |
c21c9f07 |
141 | =head2 Examples |
142 | |
445b3ea0 |
143 | sub dispatch_request { |
c21c9f07 |
144 | # matches: GET /user/1.htm?show_details=1 |
145 | # GET /user/1.htm |
146 | sub (GET + /user/* + ?show_details~ + .htm|.html|.xhtml) { |
c254b30e |
147 | my ($self, $user_id, $show_details) = @_; |
c21c9f07 |
148 | ... |
149 | }, |
150 | # matches: POST /user?username=frew |
151 | # POST /user?username=mst&first_name=matt&last_name=trout |
152 | sub (POST + /user + ?username=&*) { |
c254b30e |
153 | my ($self, $username, $misc_params) = @_; |
c21c9f07 |
154 | ... |
155 | }, |
156 | # matches: DELETE /user/1/friend/2 |
157 | sub (DELETE + /user/*/friend/*) { |
c254b30e |
158 | my ($self, $user_id, $friend_id) = @_; |
c21c9f07 |
159 | ... |
160 | }, |
161 | # matches: PUT /user/1?first_name=Matt&last_name=Trout |
162 | sub (PUT + /user/* + ?first_name~&last_name~) { |
c254b30e |
163 | my ($self, $user_id, $first_name, $last_name) = @_; |
c21c9f07 |
164 | ... |
165 | }, |
166 | sub (/user/*/...) { |
445b3ea0 |
167 | my $user_id = $_[1]; |
168 | # matches: PUT /user/1/role/1 |
169 | sub (PUT + /role/*) { |
170 | my $role_id = $_[1]; |
171 | ... |
172 | }, |
173 | # matches: DELETE /user/1/role/1 |
174 | sub (DELETE + /role/*) { |
175 | my $role_id = $_[1]; |
176 | ... |
177 | }, |
c21c9f07 |
178 | }, |
179 | } |
180 | |
3706e2a0 |
181 | =head2 The dispatch cycle |
81a5b03e |
182 | |
3706e2a0 |
183 | At the beginning of a request, your app's dispatch_request method is called |
184 | with the PSGI $env as an argument. You can handle the request entirely in |
185 | here and return a PSGI response arrayref if you want: |
81a5b03e |
186 | |
3706e2a0 |
187 | sub dispatch_request { |
188 | my ($self, $env) = @_; |
189 | [ 404, [ 'Content-type' => 'text/plain' ], [ 'Amnesia == fail' ] ] |
190 | } |
81a5b03e |
191 | |
3706e2a0 |
192 | However, generally, instead of that, you return a set of dispatch subs: |
81a5b03e |
193 | |
3706e2a0 |
194 | sub dispatch_request { |
195 | my $self = shift; |
196 | sub (/) { redispatch_to '/index.html' }, |
197 | sub (/user/*) { $self->show_user($_[1]) }, |
198 | ... |
199 | } |
81a5b03e |
200 | |
3706e2a0 |
201 | If you return a subroutine with a prototype, the prototype is treated |
202 | as a match specification - and if the test is passed, the body of the |
203 | sub is called as a method any matched arguments (see below for more details). |
81a5b03e |
204 | |
3706e2a0 |
205 | You can also return a plain subroutine which will be called with just $env |
206 | - remember that in this case if you need $self you -must- close over it. |
81a5b03e |
207 | |
3895385d |
208 | If you return a normal object, L<Web::Simple> will simply return it upwards on |
209 | the assumption that a response_filter (or some arbitrary L<Plack::Middleware>) |
210 | somewhere will convert it to something useful. This allows: |
81a5b03e |
211 | |
3706e2a0 |
212 | sub dispatch_request { |
213 | my $self = shift; |
214 | sub (.html) { response_filter { $self->render_zoom($_[0]) } }, |
215 | sub (/user/*) { $self->users->get($_[1]) }, |
216 | } |
81a5b03e |
217 | |
3895385d |
218 | to render a user object to HTML, if there is an incoming URL such as: |
219 | |
220 | http://myweb.org/user/111.html |
221 | |
222 | This works because as we descend down the dispachers, we first match |
223 | C<sub (.html)>, which adds a C<response_filter> (basically a specialized routine |
224 | that follows the L<Plack::Middleware> specification), and then later we also |
225 | match C<sub (/user/*)> which gets a user and returns that as the response. |
226 | This user object 'bubbles up' through all the wrapping middleware until it hits |
227 | the C<response_filter> we defined, after which the return is converted to a |
228 | true html response. |
81a5b03e |
229 | |
3706e2a0 |
230 | However, two types of object are treated specially - a Plack::App object |
3895385d |
231 | will have its C<->to_app> method called and be used as a dispatcher: |
81a5b03e |
232 | |
3706e2a0 |
233 | sub dispatch_request { |
234 | my $self = shift; |
235 | sub (/static/...) { Plack::App::File->new(...) }, |
236 | ... |
81a5b03e |
237 | } |
238 | |
3706e2a0 |
239 | A Plack::Middleware object will be used as a filter for the rest of the |
240 | dispatch being returned into: |
81a5b03e |
241 | |
6af22ff2 |
242 | ## responds to /admin/track_usage AND /admin/delete_accounts |
243 | |
3706e2a0 |
244 | sub dispatch_request { |
245 | my $self = shift; |
6af22ff2 |
246 | sub (/admin/**) { |
247 | Plack::Middleware::Session->new(%opts); |
248 | }, |
249 | sub (/admin/track_usage) { |
250 | ## something that needs a session |
251 | }, |
252 | sub (/admin/delete_accounts) { |
253 | ## something else that needs a session |
254 | }, |
81a5b03e |
255 | } |
256 | |
3706e2a0 |
257 | Note that this is for the dispatch being -returned- to, so if you want to |
258 | provide it inline you need to do: |
81a5b03e |
259 | |
6af22ff2 |
260 | ## ALSO responds to /admin/track_usage AND /admin/delete_accounts |
261 | |
3706e2a0 |
262 | sub dispatch_request { |
263 | my $self = shift; |
3706e2a0 |
264 | sub (/admin/...) { |
6af22ff2 |
265 | sub { |
266 | Plack::Middleware::Session->new(%opts); |
267 | }, |
268 | sub (/track_usage) { |
269 | ## something that needs a session |
270 | }, |
271 | sub (/delete_accounts) { |
272 | ## something else that needs a session |
273 | }, |
3706e2a0 |
274 | } |
81a5b03e |
275 | } |
276 | |
3706e2a0 |
277 | And that's it - but remember that all this happens recursively - it's |
3895385d |
278 | dispatchers all the way down. A URL incoming pattern will run all matching |
279 | dispatchers and then hit all added filters or L<Plack::Middleware>. |
3706e2a0 |
280 | |
81a5b03e |
281 | =head2 Web::Simple match specifications |
282 | |
283 | =head3 Method matches |
284 | |
93e30ba3 |
285 | sub (GET) { |
15dfe701 |
286 | |
287 | A match specification beginning with a capital letter matches HTTP requests |
288 | with that request method. |
289 | |
81a5b03e |
290 | =head3 Path matches |
291 | |
15dfe701 |
292 | sub (/login) { |
293 | |
294 | A match specification beginning with a / is a path match. In the simplest |
295 | case it matches a specific path. To match a path with a wildcard part, you |
296 | can do: |
297 | |
298 | sub (/user/*) { |
299 | $self->handle_user($_[1]) |
300 | |
301 | This will match /user/<anything> where <anything> does not include a literal |
302 | / character. The matched part becomes part of the match arguments. You can |
303 | also match more than one part: |
304 | |
305 | sub (/user/*/*) { |
306 | my ($self, $user_1, $user_2) = @_; |
307 | |
308 | sub (/domain/*/user/*) { |
309 | my ($self, $domain, $user) = @_; |
310 | |
311 | and so on. To match an arbitrary number of parts, use - |
312 | |
313 | sub (/page/**) { |
314 | |
315 | This will result in an element per /-separated part so matched. Note that |
316 | you can do |
317 | |
318 | sub (/page/**/edit) { |
319 | |
320 | to match an arbitrary number of parts up to but not including some final |
321 | part. |
322 | |
da8429c9 |
323 | Finally, |
324 | |
325 | sub (/foo/...) { |
326 | |
6a4808bf |
327 | Will match /foo/ on the beginning of the path -and- strip it. This is designed |
328 | to be used to construct nested dispatch structures, but can also prove useful |
329 | for having e.g. an optional language specification at the start of a path. |
da8429c9 |
330 | |
331 | Note that the '...' is a "maybe something here, maybe not" so the above |
332 | specification will match like this: |
333 | |
334 | /foo # no match |
335 | /foo/ # match and strip path to '/' |
336 | /foo/bar/baz # match and strip path to '/bar/baz' |
337 | |
15e679c1 |
338 | Note: Since Web::Simple handles a concept of file extensions, * and ** |
339 | matchers will not by default match things after a final dot, and this |
340 | can be modified by using *.* and **.* in the final position, i.e.: |
341 | |
342 | /one/* matches /one/two.three and captures "two" |
343 | /one/*.* matches /one/two.three and captures "two.three" |
344 | /** matches /one/two.three and captures "one/two" |
345 | /**.* matches /one/two.three and captures "one/two.three" |
346 | |
81a5b03e |
347 | =head3 Extension matches |
348 | |
15dfe701 |
349 | sub (.html) { |
350 | |
6a4808bf |
351 | will match .html from the path (assuming the subroutine itself returns |
352 | something, of course). This is normally used for rendering - e.g. |
15dfe701 |
353 | |
354 | sub (.html) { |
74afe4b7 |
355 | response_filter { $self->render_html($_[1]) } |
15dfe701 |
356 | } |
357 | |
b8bd7bd1 |
358 | Additionally, |
359 | |
360 | sub (.*) { |
361 | |
6a4808bf |
362 | will match any extension and supplies the extension as a match argument. |
b8bd7bd1 |
363 | |
9b9866ae |
364 | =head3 Query and body parameter matches |
365 | |
366 | Query and body parameters can be match via |
367 | |
368 | sub (?<param spec>) { # match URI query |
369 | sub (%<param spec>) { # match body params |
370 | |
cb12d2a3 |
371 | The body spec will match if the request content is either |
372 | application/x-www-form-urlencoded or multipart/form-data - the latter |
373 | of which is required for uploads, which are now handled experimentally |
374 | - see below. |
9b9866ae |
375 | |
376 | The param spec is elements of one of the following forms - |
377 | |
378 | param~ # optional parameter |
379 | param= # required parameter |
380 | @param~ # optional multiple parameter |
381 | @param= # required multiple parameter |
eb9e0e25 |
382 | :param~ # optional parameter in hashref |
383 | :param= # required parameter in hashref |
384 | :@param~ # optional multiple in hashref |
385 | :@param= # required multiple in hashref |
386 | * # include all other parameters in hashref |
387 | @* # include all other parameters as multiple in hashref |
9b9866ae |
388 | |
eb9e0e25 |
389 | separated by the & character. The arguments added to the request are |
390 | one per non-:/* parameter (scalar for normal, arrayref for multiple), |
391 | plus if any :/* specs exist a hashref containing those values. |
9b9866ae |
392 | |
3895385d |
393 | Please note that if you specify a multiple type parameter match, you are |
394 | ensured of getting an arrayref for the value, EVEN if the current incoming |
395 | request has only one value. However if a parameter is specified as single |
396 | and multiple values are found, the last one will be used. |
397 | |
398 | For example to match a page parameter with an optional order_by parameter one |
9b9866ae |
399 | would write: |
400 | |
401 | sub (?page=&order_by~) { |
eb9e0e25 |
402 | my ($self, $page, $order_by) = @_; |
403 | return unless $page =~ /^\d+$/; |
404 | $page ||= 'id'; |
9b9866ae |
405 | response_filter { |
406 | $_[1]->search_rs({}, $p); |
407 | } |
408 | } |
409 | |
410 | to implement paging and ordering against a L<DBIx::Class::ResultSet> object. |
411 | |
3895385d |
412 | Another Example: To get all parameters as a hashref of arrayrefs, write: |
eb9e0e25 |
413 | |
414 | sub(?@*) { |
415 | my ($self, $params) = @_; |
416 | ... |
417 | |
8c4ffad3 |
418 | To get two parameters as a hashref, write: |
419 | |
420 | sub(?:user~&:domain~) { |
421 | my ($self, $params) = @_; # params contains only 'user' and 'domain' keys |
422 | |
423 | You can also mix these, so: |
424 | |
425 | sub (?foo=&@bar~&:coffee=&@*) { |
426 | my ($self, $foo, $bar, $params); |
427 | |
428 | where $bar is an arrayref (possibly an empty one), and $params contains |
429 | arrayref values for all parameters -not- mentioned and a scalar value for |
430 | the 'coffee' parameter. |
431 | |
3895385d |
432 | Note, in the case where you combine arrayref, single parameter and named |
433 | hashref style, the arrayref and single parameters will appear in C<@_> in the |
38d5b336 |
434 | order you defined them in the protoype, but all hashrefs will merge into a |
3895385d |
435 | single C<$params>, as in the example above. |
436 | |
05aafc1a |
437 | =head3 Upload matches (EXPERIMENTAL) |
438 | |
439 | Note: This feature is experimental. This means that it may not remain |
440 | 100% in its current form. If we change it, notes on updating your code |
441 | will be added to the L</CHANGES BETWEEN RELEASES> section below. |
442 | |
443 | sub (*foo=) { # param specifier can be anything valid for query or body |
444 | |
445 | The upload match system functions exactly like a query/body match, except |
446 | that the values returned (if any) are C<Web::Dispatch::Upload> objects. |
447 | |
448 | Note that this match type will succeed in two circumstances where you might |
449 | not expect it to - first, when the field exists but is not an upload field |
450 | and second, when the field exists but the form is not an upload form (i.e. |
451 | content type "application/x-www-form-urlencoded" rather than |
452 | "multipart/form-data"). In either of these cases, what you'll get back is |
453 | a C<Web::Dispatch::NotAnUpload> object, which will C<die> with an error |
454 | pointing out the problem if you try and use it. To be sure you have a real |
455 | upload object, call |
456 | |
457 | $upload->is_upload # returns 1 on a valid upload, 0 on a non-upload field |
458 | |
459 | and to get the reason why such an object is not an upload, call |
460 | |
461 | $upload->reason # returns a reason or '' on a valid upload. |
462 | |
463 | Other than these two methods, the upload object provides the same interface |
464 | as L<Plack::Request::Upload> with the addition of a stringify to the temporary |
465 | filename to make copying it somewhere else easier to handle. |
466 | |
81a5b03e |
467 | =head3 Combining matches |
468 | |
15dfe701 |
469 | Matches may be combined with the + character - e.g. |
470 | |
b8bd7bd1 |
471 | sub (GET + /user/*) { |
472 | |
473 | to create an AND match. They may also be combined withe the | character - e.g. |
474 | |
475 | sub (GET|POST) { |
476 | |
477 | to create an OR match. Matches can be nested with () - e.g. |
478 | |
479 | sub ((GET|POST) + /user/*) { |
480 | |
481 | and negated with ! - e.g. |
482 | |
483 | sub (!/user/foo + /user/*) { |
484 | |
485 | ! binds to the immediate rightmost match specification, so if you want |
486 | to negate a combination you will need to use |
487 | |
488 | sub ( !(POST|PUT|DELETE) ) { |
489 | |
490 | and | binds tighter than +, so |
491 | |
492 | sub ((GET|POST) + /user/*) { |
493 | |
494 | and |
495 | |
496 | sub (GET|POST + /user/*) { |
497 | |
498 | are equivalent, but |
499 | |
1760e999 |
500 | sub ((GET + /admin/...) | (POST + /admin/...)) { |
b8bd7bd1 |
501 | |
502 | and |
503 | |
1760e999 |
504 | sub (GET + /admin/... | POST + /admin/...) { |
b8bd7bd1 |
505 | |
506 | are not - the latter is equivalent to |
507 | |
1760e999 |
508 | sub (GET + (/admin/...|POST) + /admin/...) { |
b8bd7bd1 |
509 | |
3895385d |
510 | which will never match! |
b8bd7bd1 |
511 | |
512 | =head3 Whitespace |
15dfe701 |
513 | |
514 | Note that for legibility you are permitted to use whitespace - |
515 | |
44db8e76 |
516 | sub (GET + /user/*) { |
15dfe701 |
517 | |
b8bd7bd1 |
518 | but it will be ignored. This is because the perl parser strips whitespace |
519 | from subroutine prototypes, so this is equivalent to |
520 | |
521 | sub (GET+/user/*) { |
15dfe701 |
522 | |
24175cb5 |
523 | =head3 Accessing the PSGI env hash |
524 | |
3706e2a0 |
525 | In some cases you may wish to get the raw PSGI env hash - to do this, |
526 | you can either use a plain sub - |
527 | |
528 | sub { |
529 | my ($env) = @_; |
530 | ... |
531 | } |
24175cb5 |
532 | |
3706e2a0 |
533 | or use the PSGI_ENV constant exported to retrieve it: |
c21c9f07 |
534 | |
3706e2a0 |
535 | sub (GET + /foo + ?some_param=) { |
536 | my $param = $_[1]; |
537 | my $env = $_[PSGI_ENV]; |
538 | } |
c21c9f07 |
539 | |
3706e2a0 |
540 | but note that if you're trying to add a middleware, you should simply use |
541 | Web::Simple's direct support for doing so. |
c21c9f07 |
542 | |
445b3ea0 |
543 | =head1 EXPORTED SUBROUTINES |
c21c9f07 |
544 | |
545 | =head2 response_filter |
546 | |
547 | response_filter { |
548 | # Hide errors from the user because we hates them, preciousss |
445b3ea0 |
549 | if (ref($_[0]) eq 'ARRAY' && $_[0]->[0] == 500) { |
550 | $_[0] = [ 200, @{$_[0]}[1..$#{$_[0]}] ]; |
c21c9f07 |
551 | } |
445b3ea0 |
552 | return $_[0]; |
c21c9f07 |
553 | }; |
554 | |
555 | The response_filter subroutine is designed for use inside dispatch subroutines. |
556 | |
557 | It creates and returns a special dispatcher that always matches, and calls |
558 | the block passed to it as a filter on the result of running the rest of the |
559 | current dispatch chain. |
560 | |
561 | Thus the filter above runs further dispatch as normal, but if the result of |
562 | dispatch is a 500 (Internal Server Error) response, changes this to a 200 (OK) |
563 | response without altering the headers or body. |
564 | |
565 | =head2 redispatch_to |
566 | |
567 | redispatch_to '/other/url'; |
568 | |
569 | The redispatch_to subroutine is designed for use inside dispatch subroutines. |
570 | |
571 | It creates and returns a special dispatcher that always matches, and instead |
572 | of continuing dispatch re-delegates it to the start of the dispatch process, |
573 | but with the path of the request altered to the supplied URL. |
574 | |
950d8829 |
575 | Thus if you receive a POST to '/some/url' and return a redispatch to |
c21c9f07 |
576 | '/other/url', the dispatch behaviour will be exactly as if the same POST |
577 | request had been made to '/other/url' instead. |
578 | |
3895385d |
579 | Note, this is not the same as returning an HTTP 3xx redirect as a response; |
38d5b336 |
580 | rather it is a much more efficient internal process. |
3895385d |
581 | |
8c4ffad3 |
582 | =head1 CHANGES BETWEEN RELEASES |
445b3ea0 |
583 | |
584 | =head2 Changes between 0.004 and 0.005 |
585 | |
586 | =over 4 |
587 | |
588 | =item * dispatch {} replaced by declaring a dispatch_request method |
589 | |
590 | dispatch {} has gone away - instead, you write: |
591 | |
592 | sub dispatch_request { |
e4122532 |
593 | my $self = shift; |
445b3ea0 |
594 | sub (GET /foo/) { ... }, |
595 | ... |
596 | } |
597 | |
598 | Note that this method is still -returning- the dispatch code - just like |
599 | dispatch did. |
600 | |
e4122532 |
601 | Also note that you need the 'my $self = shift' since the magic $self |
602 | variable went away. |
603 | |
604 | =item * the magic $self variable went away. |
605 | |
606 | Just add 'my $self = shift;' while writing your 'sub dispatch_request {' |
607 | like a normal perl method. |
608 | |
445b3ea0 |
609 | =item * subdispatch deleted - all dispatchers can now subdispatch |
610 | |
611 | In earlier releases you needed to write: |
612 | |
613 | subdispatch sub (/foo/...) { |
614 | ... |
615 | [ |
616 | sub (GET /bar/) { ... }, |
617 | ... |
618 | ] |
619 | } |
620 | |
621 | As of 0.005, you can instead write simply: |
622 | |
623 | sub (/foo/...) { |
624 | ... |
625 | ( |
626 | sub (GET /bar/) { ... }, |
627 | ... |
628 | ) |
629 | } |
8c4ffad3 |
630 | |
c2150f7d |
631 | =back |
632 | |
8c4ffad3 |
633 | =head2 Changes since Antiquated Perl |
634 | |
635 | =over 4 |
636 | |
637 | =item * filter_response renamed to response_filter |
638 | |
639 | This is a pure rename; a global search and replace should fix it. |
640 | |
c21c9f07 |
641 | =item * dispatch [] changed to dispatch {} |
8c4ffad3 |
642 | |
643 | Simply changing |
644 | |
645 | dispatch [ sub(...) { ... }, ... ]; |
646 | |
647 | to |
648 | |
649 | dispatch { sub(...) { ... }, ... }; |
650 | |
651 | should work fine. |
652 | |
653 | =back |
654 | |
fb771406 |
655 | =head1 DEVELOPMENT HISTORY |
656 | |
657 | Web::Simple was originally written to form part of my Antiquated Perl talk for |
658 | Italian Perl Workshop 2009, but in writing the bloggery example I realised |
659 | that having a bare minimum system for writing web applications that doesn't |
660 | drive me insane was rather nice and decided to spend my attempt at nanowrimo |
661 | for 2009 improving and documenting it to the point where others could use it. |
662 | |
58fd1f7f |
663 | The Antiquated Perl talk can be found at L<http://www.shadowcat.co.uk/archive/conference-video/> and the slides are reproduced in this distribution under |
664 | L<Web::Simple::AntiquatedPerl>. |
fb771406 |
665 | |
8c4ffad3 |
666 | =head1 COMMUNITY AND SUPPORT |
667 | |
668 | =head2 IRC channel |
669 | |
670 | irc.perl.org #web-simple |
671 | |
672 | =head2 No mailing list yet |
673 | |
674 | Because mst's non-work email is a bombsite so he'd never read it anyway. |
675 | |
676 | =head2 Git repository |
677 | |
678 | Gitweb is on http://git.shadowcat.co.uk/ and the clone URL is: |
679 | |
680 | git clone git://git.shadowcat.co.uk/catagits/Web-Simple.git |
681 | |
682 | =head1 AUTHOR |
683 | |
c2150f7d |
684 | Matt S. Trout (mst) <mst@shadowcat.co.uk> |
8c4ffad3 |
685 | |
686 | =head1 CONTRIBUTORS |
687 | |
48904f80 |
688 | Devin Austin (dhoss) <dhoss@cpan.org> |
689 | |
690 | Arthur Axel 'fREW' Schmidt <frioux@gmail.com> |
691 | |
c2150f7d |
692 | gregor herrmann (gregoa) <gregoa@debian.org> |
8c4ffad3 |
693 | |
48904f80 |
694 | John Napiorkowski (jnap) <jjn1056@yahoo.com> |
695 | |
696 | Josh McMichael <jmcmicha@linus222.gsc.wustl.edu> |
697 | |
698 | Justin Hunter <justin.d.hunter@gmail.com> |
699 | |
700 | Kjetil Kjernsmo <kjetil@kjernsmo.net> |
701 | |
702 | markie <markie@nulletch64.dreamhost.com> |
703 | |
704 | Christian Walde (Mithaldu) <walde.christian@googlemail.com> |
705 | |
706 | nperez <nperez@cpan.org> |
707 | |
708 | Robin Edwards <robin.ge@gmail.com> |
709 | |
8c4ffad3 |
710 | =head1 COPYRIGHT |
711 | |
6a4808bf |
712 | Copyright (c) 2010 the Web::Simple L</AUTHOR> and L</CONTRIBUTORS> |
8c4ffad3 |
713 | as listed above. |
714 | |
715 | =head1 LICENSE |
716 | |
717 | This library is free software and may be distributed under the same terms |
718 | as perl itself. |
719 | |
3583ca04 |
720 | =cut |
7401408e |
721 | |
5c33dda5 |
722 | 1; |