X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FWeb%2FSimple.pm;h=db835352a36f75acbd89b3aa629d5053004841c9;hb=c2c68a873d5c6ce0c12759b3e8d3da4c78b18947;hp=891e115681814cf43307850824c7e67a5edd0508;hpb=05aafc1a964aa5c2e86690602d5c49a8329fb90c;p=catagits%2FWeb-Simple.git diff --git a/lib/Web/Simple.pm b/lib/Web/Simple.pm index 891e115..db83535 100644 --- a/lib/Web/Simple.pm +++ b/lib/Web/Simple.pm @@ -6,7 +6,7 @@ use warnings::illegalproto (); use Moo (); use Web::Dispatch::Wrapper (); -our $VERSION = '0.008'; +our $VERSION = '0.011'; sub import { my ($class, $app_package) = @_; @@ -39,18 +39,15 @@ Web::Simple - A quick and easy way to build simple web applications #!/usr/bin/env perl - use Web::Simple 'HelloWorld'; + package HelloWorld; + use Web::Simple - { - package HelloWorld; - - sub dispatch_request { - sub (GET) { - [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ] - }, - sub () { - [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] - } + sub dispatch_request { + sub (GET) { + [ 200, [ 'Content-type', 'text/plain' ], [ 'Hello world!' ] ] + }, + sub () { + [ 405, [ 'Content-type', 'text/plain' ], [ 'Method not allowed' ] ] } } @@ -60,9 +57,17 @@ If you save this file into your cgi-bin as C 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. To get help with L, -please connect to the irc.perl.org IRC network and join #web-simple. +you'll get the "Hello world!" string output to your browser. At the same time +this file will also act as a class module, so you can save it as HelloWorld.pm +and use it as-is in test scripts or other deployment mechanisms. + +Note that you should retain the ->run_if_script even if your app is a +module, since this additionally makes it valid as a .psgi file, which can +be extremely useful during development. + +For more complex examples and non-CGI deployment, see +L. To get help with L, please connect to +the irc.perl.org IRC network and join #web-simple. =head1 DESCRIPTION @@ -82,7 +87,7 @@ C based one: This sets up your package (in this case "NameOfApplication" is your package) so that it inherits from L and imports L, -as well as installs a C constant for convenience, as well as some +as well as installs a C constant for convenience, as well as some other subroutines. Importing L will automatically make your code use the C and @@ -193,6 +198,17 @@ However, generally, instead of that, you return a set of dispatch subs: ... } +Well, a sub is a valid PSGI response too (for ultimate streaming and async +cleverness). If you want to return a PSGI sub you have to wrap it into an +array ref. + + sub dispatch_request { + [ sub { + my $respond = shift; + # This is pure PSGI here, so read perldoc PSGI + } ] + } + If you return a subroutine with a prototype, the prototype is treated as a match specification - and if the test is passed, the body of the sub is called as a method any matched arguments (see below for more details). @@ -306,9 +322,9 @@ also match more than one part: and so on. To match an arbitrary number of parts, use - sub (/page/**) { + my ($self, $match) = @_; -This will result in an element per /-separated part so matched. Note that -you can do +This will result in a single element for the entire match. Note that you can do sub (/page/**/edit) { @@ -363,9 +379,10 @@ Query and body parameters can be match via sub (?) { # match URI query sub (%) { # match body params -The body is only matched if the content type is -application/x-www-form-urlencoded (note this means that Web::Simple does -not yet handle uploads; this will be addressed in a later release). +The body spec will match if the request content is either +application/x-www-form-urlencoded or multipart/form-data - the latter +of which is required for uploads, which are now handled experimentally +- see below. The param spec is elements of one of the following forms - @@ -425,7 +442,7 @@ the 'coffee' parameter. Note, in the case where you combine arrayref, single parameter and named hashref style, the arrayref and single parameters will appear in C<@_> in the -order you defined them in the protoype, but all hashrefs will merge into a +order you defined them in the protoype, but all hashrefs will merge into a single C<$params>, as in the example above. =head3 Upload matches (EXPERIMENTAL) @@ -571,7 +588,7 @@ Thus if you receive a POST to '/some/url' and return a redispatch to request had been made to '/other/url' instead. Note, this is not the same as returning an HTTP 3xx redirect as a response; -rather it is a much more efficient internal process. +rather it is a much more efficient internal process. =head1 CHANGES BETWEEN RELEASES @@ -622,6 +639,8 @@ As of 0.005, you can instead write simply: ) } +=back + =head2 Changes since Antiquated Perl =over 4 @@ -673,15 +692,35 @@ Gitweb is on http://git.shadowcat.co.uk/ and the clone URL is: =head1 AUTHOR -Matt S. Trout +Matt S. Trout (mst) =head1 CONTRIBUTORS -None required yet. Maybe this module is perfect (hahahahaha ...). +Devin Austin (dhoss) + +Arthur Axel 'fREW' Schmidt + +gregor herrmann (gregoa) + +John Napiorkowski (jnap) + +Josh McMichael + +Justin Hunter (arcanez) + +Kjetil Kjernsmo + +markie + +Christian Walde (Mithaldu) + +nperez + +Robin Edwards =head1 COPYRIGHT -Copyright (c) 2010 the Web::Simple L and L +Copyright (c) 2011 the Web::Simple L and L as listed above. =head1 LICENSE