From: Matt S Trout Date: Mon, 23 Nov 2009 22:34:58 +0000 (-0500) Subject: switch dispatch [] to dispatch {} X-Git-Tag: v0.003~39 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FWeb-Simple.git;a=commitdiff_plain;h=92e23550709cc075dd06e14d50dc302d0b405977 switch dispatch [] to dispatch {} --- diff --git a/examples/bloggery/bloggery.cgi b/examples/bloggery/bloggery.cgi index 947180f..1a2fee1 100755 --- a/examples/bloggery/bloggery.cgi +++ b/examples/bloggery/bloggery.cgi @@ -84,7 +84,7 @@ sub post { $self->post_list->post($post); } -dispatch [ +dispatch { sub (.html) { response_filter { $self->render_html($_[1]) }, }, @@ -103,7 +103,7 @@ dispatch [ sub { [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] }, -]; +}; sub render_html { my ($self, $data) = @_; diff --git a/lib/Web/Simple.pm b/lib/Web/Simple.pm index 5e38ba8..f65ecb5 100644 --- a/lib/Web/Simple.pm +++ b/lib/Web/Simple.pm @@ -27,8 +27,8 @@ sub _export_into { my ($class, $app_package) = @_; { no strict 'refs'; - *{"${app_package}::dispatch"} = sub { - $app_package->_setup_dispatcher(@_); + *{"${app_package}::dispatch"} = sub (&) { + $app_package->_setup_dispatcher([ $_[0]->() ]); }; *{"${app_package}::response_filter"} = sub (&) { $app_package->_construct_response_filter($_[0]); @@ -75,7 +75,7 @@ we can't promise not to change things at all. Not yet. Sorry. { package HelloWorld; - dispatch [ + dispatch { sub (GET) { [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ] }, @@ -146,7 +146,7 @@ It also exports the following subroutines: ... ); - dispatch [ sub (...) { ... }, ... ]; + dispatch { sub (...) { ... }, ... }; response_filter { ... }; @@ -194,24 +194,24 @@ cause an exception to be thrown. =head2 dispatch - dispatch [ + dispatch { sub (GET) { [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ] }, sub () { [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] } - ]; + }; The dispatch subroutine calls NameOfApplication->_setup_dispatcher with -the subroutines passed to it, which then creates your Web::Simple -application's dispatcher from these subs. The prototype of the subroutine +the return value of the block passed to it, which then creates your Web::Simple +application's dispatcher from these subs. The prototype of each subroutine is expected to be a Web::Simple dispatch specification (see L below for more details), and the body of the subroutine is the code to execute if the specification matches. -Each dispatcher is given the dispatcher constructed from the next element -of the arrayref as its next dispatcher, except for the final element, which +Each dispatcher is given the dispatcher constructed from the next subroutine +returned as its next dispatcher, except for the final subroutine, which is given the return value of NameOfApplication->_build_final_dispatcher as its next dispatcher (by default this returns a 500 error response). diff --git a/t/post.t b/t/post.t index 4316808..8ca1321 100644 --- a/t/post.t +++ b/t/post.t @@ -10,7 +10,7 @@ use Test::More ( { use Web::Simple 'PostTest'; package PostTest; - dispatch [ + dispatch { sub (%foo=&bar~) { $_[1]->{bar} ||= 'EMPTY'; [ 200, @@ -18,7 +18,7 @@ use Test::More ( [ join(' ',@{$_[1]}{qw(foo bar)}) ] ] }, - ] + } } use HTTP::Request::Common qw(GET POST);