changes line for 0.002 and bump version in Web/Simple.pm
[catagits/Web-Simple.git] / lib / Web / Simple.pm
index a994d66..a9711fe 100644 (file)
@@ -2,6 +2,9 @@ package Web::Simple;
 
 use strict;
 use warnings FATAL => 'all';
+use 5.008;
+
+our $VERSION = '0.002';
 
 sub setup_all_strictures {
   strict->import;
@@ -56,15 +59,17 @@ Web::Simple - A quick and easy way to build simple web applications
 
 =head1 WARNING
 
-This is really quite new. If you're reading this from git, it means it's
-really really new and we're still playing with things. If you're reading
-this on CPAN, it means the stuff that's here we're probably happy with. But
-only probably. So we may have to change stuff.
+This is really quite new. If you're reading this on CPAN, it means the stuff
+that's here we're probably happy with. But only probably. So we may have to
+change stuff. And if you're reading this from git, come check with irc.perl.org
+#web-simple that we're actually sure we're going to keep anything that's
+different from the CPAN version.
 
-If we do find we have to change stuff we'll add a section explaining how to
-switch your code across to the new version, and we'll do our best to make it
-as painless as possible because we've got Web::Simple applications too. But
-we can't promise not to change things at all. Not yet. Sorry.
+If we do find we have to change stuff we'll add to the
+L<CHANGES BETWEEN RELEASES> section explaining how to switch your code across
+to the new version, and we'll do our best to make it as painless as possible
+because we've got Web::Simple applications too. But we can't promise not to
+change things at all. Not yet. Sorry.
 
 =head1 SYNOPSIS
 
@@ -82,7 +87,7 @@ we can't promise not to change things at all. Not yet. Sorry.
       sub () {
         [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ]
       }
-    ];
+    };
   }
 
   HelloWorld->run_if_script;
@@ -92,7 +97,8 @@ If you save this file into your cgi-bin as hello-world.cgi and then visit
   http://my.server.name/cgi-bin/hello-world.cgi/
 
 you'll get the "Hello world!" string output to your browser. For more complex
-examples and non-CGI deployment, see below.
+examples and non-CGI deployment, see below. To get help with Web::Simple,
+please connect to the irc.perl.org IRC network and join #web-simple.
 
 =head1 WHY?
 
@@ -169,121 +175,52 @@ so that perl will not attempt to load the application again even if
 
 is encountered in other code.
 
-=head1 EXPORTED SUBROUTINES
-
-=head2 default_config
-
-  default_config(
-    one_key => 'foo',
-    another_key => 'bar',
-  );
-
-  ...
-
-  $self->config->{one_key} # 'foo'
-
-This creates the default configuration for the application, by creating a
-
-  sub _default_config {
-     return (one_key => 'foo', another_key => 'bar');
-  }
-
-in the application namespace when executed. Note that this means that
-you should only run default_config once - calling it a second time will
-cause an exception to be thrown.
-
-=head2 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 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</DISPATCH SPECIFICATIONS> 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 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).
-
-See L</DISPATCH STRATEGY> below for details on how the Web::Simple dispatch
-system uses the return values of these subroutines to determine how to
-continue, alter or abort dispatch.
-
-Note that _setup_dispatcher creates a
-
-  sub _dispatcher {
-    return <root dispatcher object here>;
-  }
-
-method in your class so as with default_config, calling dispatch a second time
-will result in an exception.
-
-=head2 response_filter
-
-  response_filter {
-    # Hide errors from the user because we hates them, preciousss
-    if (ref($_[1]) eq 'ARRAY' && $_[1]->[0] == 500) {
-      $_[1] = [ 200, @{$_[1]}[1..$#{$_[1]}] ];
-    }
-    return $_[1];
-  };
-
-The response_filter subroutine is designed for use inside dispatch subroutines.
-
-It creates and returns a special dispatcher that always matches, and calls
-the block passed to it as a filter on the result of running the rest of the
-current dispatch chain.
-
-Thus the filter above runs further dispatch as normal, but if the result of
-dispatch is a 500 (Internal Server Error) response, changes this to a 200 (OK)
-response without altering the headers or body.
-
-=head2 redispatch_to
-
-  redispatch_to '/other/url';
-
-The redispatch_to subroutine is designed for use inside dispatch subroutines.
-
-It creates and returns a special dispatcher that always matches, and instead
-of continuing dispatch re-delegates it to the start of the dispatch process,
-but with the path of the request altered to the supplied URL.
-
-Thus if you receive a POST to '/some/url' and return a redipstch to
-'/other/url', the dispatch behaviour will be exactly as if the same POST
-request had been made to '/other/url' instead.
-
-=head2 subdispatch
-
-  subdispatch sub (/user/*/) {
-    my $u = $self->user($_[1]);
-    [
-      sub (GET) { $u },
-      sub (DELETE) { $u->delete },
-    ]
-  }
-
-The subdispatch subroutine is designed for use in dispatcher construction.
-
-It creates a dispatcher which, if it matches, treats its return value not
-as a final value but an arrayref of dispatch specifications such as could
-be passed to the dispatch subroutine itself. These are turned into a dispatcher
-which is then invoked. Any changes the match makes to the request are in
-scope for this inner dispatcher only - so if the initial match is a
-destructive one like .html the full path will be restored if the
-subdispatch fails.
-
 =head1 DISPATCH STRATEGY
 
+=head2 Examples
+
+ dispatch {
+   # matches: GET /user/1.htm?show_details=1
+   #          GET /user/1.htm
+   sub (GET + /user/* + ?show_details~ + .htm|.html|.xhtml) {
+     shift; my ($user_id, $show_details) = @_;
+     ...
+   },
+   # matches: POST /user?username=frew
+   #          POST /user?username=mst&first_name=matt&last_name=trout
+   sub (POST + /user + ?username=&*) {
+      shift; my ($username, $misc_params) = @_;
+     ...
+   },
+   # matches: DELETE /user/1/friend/2
+   sub (DELETE + /user/*/friend/*) {
+     shift; my ($user_id, $friend_id) = @_;
+     ...
+   },
+   # matches: PUT /user/1?first_name=Matt&last_name=Trout
+   sub (PUT + /user/* + ?first_name~&last_name~) {
+     shift; my ($user_id, $first_name, $last_name) = @_;
+     ...
+   },
+   sub (/user/*/...) {
+      my $user_id = $_[1];
+      subdispatch sub {
+         [
+            # matches: PUT /user/1/role/1
+            sub (PUT + /role/*) {
+              my $role_id = $_[1];
+              ...
+            },
+            # matches: DELETE /user/1/role/1
+            sub (DELETE + /role/*) {
+              my $role_id = shift;
+              ...
+            },
+         ];
+      }
+   },
+ }
+
 =head2 Description of the dispatcher object
 
 Web::Simple::Dispatcher objects have three components:
@@ -359,7 +296,7 @@ should unpack them like so:
 
 =head3 Method matches
 
-  sub (GET ...) {
+  sub (GET) {
 
 A match specification beginning with a capital letter matches HTTP requests
 with that request method.
@@ -473,12 +410,29 @@ would write:
 
 to implement paging and ordering against a L<DBIx::Class::ResultSet> object.
 
+Note that if a parameter is specified as single and multiple values are found,
+the last one will be used.
+
 To get all parameters as a hashref of arrayrefs, write:
 
   sub(?@*) {
     my ($self, $params) = @_;
     ...
 
+To get two parameters as a hashref, write:
+
+  sub(?:user~&:domain~) {
+    my ($self, $params) = @_; # params contains only 'user' and 'domain' keys
+
+You can also mix these, so:
+
+  sub (?foo=&@bar~&:coffee=&@*) {
+     my ($self, $foo, $bar, $params);
+
+where $bar is an arrayref (possibly an empty one), and $params contains
+arrayref values for all parameters -not- mentioned and a scalar value for
+the 'coffee' parameter.
+
 =head3 Combining matches
 
 Matches may be combined with the + character - e.g.
@@ -535,6 +489,177 @@ from subroutine prototypes, so this is equivalent to
 
   sub (GET+/user/*) {
 
+=head1 EXPORTED SUBROUTINES
+
+=head2 default_config
+
+  default_config(
+    one_key => 'foo',
+    another_key => 'bar',
+  );
+
+  ...
+
+  $self->config->{one_key} # 'foo'
+
+This creates the default configuration for the application, by creating a
+
+  sub _default_config {
+     return (one_key => 'foo', another_key => 'bar');
+  }
+
+in the application namespace when executed. Note that this means that
+you should only run default_config once - calling it a second time will
+cause an exception to be thrown.
+
+=head2 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 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</DISPATCH SPECIFICATIONS> 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 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).
+
+See L</DISPATCH STRATEGY> below for details on how the Web::Simple dispatch
+system uses the return values of these subroutines to determine how to
+continue, alter or abort dispatch.
+
+Note that _setup_dispatcher creates a
+
+  sub _dispatcher {
+    return <root dispatcher object here>;
+  }
+
+method in your class so as with default_config, calling dispatch a second time
+will result in an exception.
+
+=head2 response_filter
+
+  response_filter {
+    # Hide errors from the user because we hates them, preciousss
+    if (ref($_[1]) eq 'ARRAY' && $_[1]->[0] == 500) {
+      $_[1] = [ 200, @{$_[1]}[1..$#{$_[1]}] ];
+    }
+    return $_[1];
+  };
+
+The response_filter subroutine is designed for use inside dispatch subroutines.
+
+It creates and returns a special dispatcher that always matches, and calls
+the block passed to it as a filter on the result of running the rest of the
+current dispatch chain.
+
+Thus the filter above runs further dispatch as normal, but if the result of
+dispatch is a 500 (Internal Server Error) response, changes this to a 200 (OK)
+response without altering the headers or body.
+
+=head2 redispatch_to
+
+  redispatch_to '/other/url';
+
+The redispatch_to subroutine is designed for use inside dispatch subroutines.
+
+It creates and returns a special dispatcher that always matches, and instead
+of continuing dispatch re-delegates it to the start of the dispatch process,
+but with the path of the request altered to the supplied URL.
+
+Thus if you receive a POST to '/some/url' and return a redipstch to
+'/other/url', the dispatch behaviour will be exactly as if the same POST
+request had been made to '/other/url' instead.
+
+=head2 subdispatch
+
+  subdispatch sub (/user/*/) {
+    my $u = $self->user($_[1]);
+    [
+      sub (GET) { $u },
+      sub (DELETE) { $u->delete },
+    ]
+  }
+
+The subdispatch subroutine is designed for use in dispatcher construction.
+
+It creates a dispatcher which, if it matches, treats its return value not
+as a final value but an arrayref of dispatch specifications such as could
+be passed to the dispatch subroutine itself. These are turned into a dispatcher
+which is then invoked. Any changes the match makes to the request are in
+scope for this inner dispatcher only - so if the initial match is a
+destructive one like .html the full path will be restored if the
+subdispatch fails.
+
+=head1 CHANGES BETWEEN RELEASES
+
+=head2 Changes since Antiquated Perl
+
+=over 4
+
+=item * filter_response renamed to response_filter
+
+This is a pure rename; a global search and replace should fix it.
+
+=item * dispatch [] changed to dispatch {}
+
+Simply changing
+
+  dispatch [ sub(...) { ... }, ... ];
+
+to
+
+  dispatch { sub(...) { ... }, ... };
+
+should work fine.
+
+=back
+
+=head1 COMMUNITY AND SUPPORT
+
+=head2 IRC channel
+
+irc.perl.org #web-simple
+
+=head2 No mailing list yet
+
+Because mst's non-work email is a bombsite so he'd never read it anyway.
+
+=head2 Git repository
+
+Gitweb is on http://git.shadowcat.co.uk/ and the clone URL is:
+
+  git clone git://git.shadowcat.co.uk/catagits/Web-Simple.git
+
+=head1 AUTHOR
+
+Matt S. Trout <mst@shadowcat.co.uk>
+
+=head1 CONTRIBUTORS
+
+None required yet. Maybe this module is perfect (hahahahaha ...).
+
+=head1 COPYRIGHT
+
+Copyright (c) 2009 the Web::Simple L</AUTHOR> and L</CONTRIBUTORS>
+as listed above.
+
+=head1 LICENSE
+
+This library is free software and may be distributed under the same terms
+as perl itself.
+
 =cut
 
 1;