.\" Automatically generated by Pod::Man 2.27 (Pod::Simple 3.28) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is turned on, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{ . if \nF \{ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Web::Simple::Application 3pm" .TH Web::Simple::Application 3pm "2014-05-23" "perl v5.18.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Web::Simple::Application \- A base class for your Web\-Simple application .SH "DESCRIPTION" .IX Header "DESCRIPTION" This is a base class for your Web::Simple application. You probably don't need to construct this class yourself, since Web::Simple does the 'heavy lifting' for you in that regards. .SH "METHODS" .IX Header "METHODS" This class exposes the following public methods. .SS "default_config" .IX Subsection "default_config" Merges with the \f(CW\*(C`config\*(C'\fR initializer to provide configuration information for your application. For example: .PP .Vb 6 \& sub default_config { \& ( \& title => \*(AqBloggery\*(Aq, \& posts_dir => $FindBin::Bin.\*(Aq/posts\*(Aq, \& ); \& } .Ve .PP Now, the \f(CW\*(C`config\*(C'\fR attribute of \f(CW$self\fR will be set to a HashRef containing keys 'title' and 'posts_dir'. .PP The keys from default_config are merged into any config supplied, so if you construct your application like: .PP .Vb 3 \& MyWebSimpleApp::Web\->new( \& config => { title => \*(AqSpoon\*(Aq, environment => \*(Aqdev\*(Aq } \& ) .Ve .PP then \f(CW\*(C`config\*(C'\fR will contain: .PP .Vb 5 \& { \& title => \*(AqSpoon\*(Aq, \& posts_dir => \*(Aq/path/to/myapp/posts\*(Aq, \& environment => \*(Aqdev\*(Aq \& } .Ve .SS "run_if_script" .IX Subsection "run_if_script" The run_if_script method is designed to be used at the end of the script or .pm file where your application class is defined \- for example: .PP .Vb 3 \& ## my_web_simple_app.pl \& #!/usr/bin/env perl \& use Web::Simple \*(AqHelloWorld\*(Aq; \& \& { \& package HelloWorld; \& \& sub dispatch_request { \& sub (GET) { \& [ 200, [ \*(AqContent\-type\*(Aq, \*(Aqtext/plain\*(Aq ], [ \*(AqHello world!\*(Aq ] ] \& }, \& sub () { \& [ 405, [ \*(AqContent\-type\*(Aq, \*(Aqtext/plain\*(Aq ], [ \*(AqMethod not allowed\*(Aq ] ] \& } \& } \& } \& \& HelloWorld\->run_if_script; .Ve .PP This returns a true value, so your file is now valid as a module \- so .PP .Vb 1 \& require \*(Aqmy_web_simple_app.pl\*(Aq; \& \& my $hw = HelloWorld\->new; .Ve .PP will work fine (and you can rename it to lib/HelloWorld.pm later to make it a real use-able module). .PP However, it detects if it's being run as a script (via testing \f(CW$0\fR) and if so attempts to do the right thing. .PP If run under a \s-1CGI\s0 environment, your application will execute as a \s-1CGI.\s0 .PP If run under a FastCGI environment, your application will execute as a FastCGI process (this works both for dynamic shared-hosting-style FastCGI and for apache FastCgiServer style setups). .PP If run from the commandline with a \s-1URL\s0 path, it runs a \s-1GET\s0 request against that path \- .PP .Vb 3 \& $ perl \-Ilib examples/hello\-world/hello\-world.cgi / \& 200 OK \& Content\-Type: text/plain \& \& Hello world! .Ve .PP You can also provide a method name \- .PP .Vb 3 \& $ perl \-Ilib examples/hello\-world/hello\-world.cgi POST / \& 405 Method Not Allowed \& Content\-Type: text/plain \& \& Method not allowed .Ve .PP For a \s-1POST\s0 or \s-1PUT\s0 request, pairs on the command line will be treated as form variables. For any request, pairs on the command line ending in : are treated as headers, and 'Content:' will set the request body \- .PP .Vb 1 \& $ ./myapp POST / Accept: text/html form_field_name form_field_value \& \& $ ./myapp POST / Content\-Type: text/json Content: \*(Aq{ "json": "here" }\*(Aq .Ve .PP The body of the response is sent to \s-1STDOUT\s0 and the headers to \s-1STDERR,\s0 so .PP .Vb 1 \& $ ./myapp GET / >index.html .Ve .PP will generally do the right thing. .PP To send basic authentication credentials, use user:pass@ syntax \- .PP .Vb 1 \& $ ./myapp GET bob:secret@/protected/path .Ve .PP Additionally, you can treat the file as though it were a standard \s-1PSGI\s0 application file (*.psgi). For example you can start up up with \f(CW\*(C`plackup\*(C'\fR .PP .Vb 1 \& plackup my_web_simple_app.pl .Ve .PP or \f(CW\*(C`starman\*(C'\fR .PP .Vb 1 \& starman my_web_simple_app.pl .Ve .SS "to_psgi_app" .IX Subsection "to_psgi_app" This method is called by \*(L"run_if_script\*(R" to create the \s-1PSGI\s0 app coderef for use via Plack and plackup. If you want to globally add middleware, you can override this method: .PP .Vb 2 \& use Web::Simple \*(AqHelloWorld\*(Aq; \& use Plack::Builder; \& \& { \& package HelloWorld; \& \& \& around \*(Aqto_psgi_app\*(Aq, sub { \& my ($orig, $self) = (shift, shift); \& my $app = $self\->$orig(@_); \& builder { \& enable ...; ## whatever middleware you want \& $app; \& }; \& }; \& } .Ve .PP This method can also be used to mount a Web::Simple application within a separate \f(CW\*(C`*.psgi\*(C'\fR file \- .PP .Vb 4 \& use strictures 1; \& use Plack::Builder; \& use WSApp; \& use AnotherWSApp; \& \& builder { \& mount \*(Aq/\*(Aq => WSApp\->to_psgi_app; \& mount \*(Aq/another\*(Aq => AnotherWSApp\->to_psgi_app; \& }; .Ve .PP This method can be called as a class method, in which case it implicitly calls \->new, or as an object method ... in which case it doesn't. .SS "run" .IX Subsection "run" Used for running your application under stand-alone \s-1CGI\s0 and \s-1FCGI\s0 modes. .PP I should document this more extensively but run_if_script will call it when you need it, so don't worry about it too much. .SS "run_test_request" .IX Subsection "run_test_request" .Vb 1 \& my $res = $app\->run_test_request(GET => \*(Aq/\*(Aq => %headers); \& \& my $res = $app\->run_test_request(POST => \*(Aq/\*(Aq => %headers_or_form); \& \& my $res = $app\->run_test_request($http_request); .Ve .PP Accepts either an HTTP::Request object or ($method, \f(CW$path\fR) and runs that request against the application, returning an HTTP::Response object. .PP If the \s-1HTTP\s0 method is \s-1POST\s0 or \s-1PUT,\s0 then a series of pairs can be passed after this to create a form style message body. If you need to test an upload, then create an HTTP::Request object by hand or use the \f(CW\*(C`POST\*(C'\fR subroutine provided by HTTP::Request::Common. .PP If you prefix the \s-1URL\s0 with 'user:pass@' this will be converted into an Authorization header for \s-1HTTP\s0 basic auth: .PP .Vb 3 \& my $res = $app\->run_test_request( \& GET => \*(Aqbob:secret@/protected/resource\*(Aq \& ); .Ve .PP If pairs are passed where the key ends in :, it is instead treated as a headers, so: .PP .Vb 5 \& my $res = $app\->run_test_request( \& POST => \*(Aq/\*(Aq, \& \*(AqAccept:\*(Aq => \*(Aqtext/html\*(Aq, \& some_form_key => \*(Aqvalue\*(Aq \& ); .Ve .PP will do what you expect. You can also pass a special key of Content: to set the request body: .PP .Vb 5 \& my $res = $app\->run_test_request( \& POST => \*(Aq/\*(Aq, \& \*(AqContent\-Type:\*(Aq => \*(Aqtext/json\*(Aq, \& \*(AqContent:\*(Aq => \*(Aq{ "json": "here" }\*(Aq, \& ); .Ve .SH "AUTHORS" .IX Header "AUTHORS" See Web::Simple for authors. .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" See Web::Simple for the copyright and license.