.\" Automatically generated by Pod::Man 2.22 (Pod::Simple 3.07)
.\"
.\" 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" ''
'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.
.ie \nF \{\
. de IX
. tm Index:\\$1\t\\n%\t"\\$2"
..
. nr % 0
. rr F
.\}
.el \{\
. de IX
..
.\}
.\"
.\" 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 "Plack::Builder 3"
.TH Plack::Builder 3 "2011-06-22" "perl v5.10.1" "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"
Plack::Builder \- OO and DSL to enable Plack Middlewares
.SH "SYNOPSIS"
.IX Header "SYNOPSIS"
.Vb 2
\& # in .psgi
\& use Plack::Builder;
\&
\& my $app = sub { ... };
\&
\& builder {
\& enable "Plack::Middleware::Foo";
\& enable "Plack::Middleware::Bar", opt => "val";
\& enable "Plack::Middleware::Baz";
\& $app;
\& };
\&
\& # use URLMap
\&
\& builder {
\& mount "/foo" => builder {
\& enable "Plack::Middleware::Foo";
\& $app;
\& };
\&
\& mount "/bar" => $app2;
\& };
\&
\& # using OO interface
\&
\& my $builder = Plack::Builder\->new();
\& $builder\->add_middleware(\*(AqFoo\*(Aq, opt => 1);
\& $app = $builder\->mount(\*(Aq/app\*(Aq => $app);
\& $app = $builder\->to_app($app);
.Ve
.SH "DESCRIPTION"
.IX Header "DESCRIPTION"
Plack::Builder gives you a quick domain specific language (\s-1DSL\s0) to
wrap your application with Plack::Middleware subclasses. The
middleware you're trying to use should use Plack::Middleware as a
base class to use this \s-1DSL\s0, inspired by Rack::Builder.
.PP
Whenever you call \f(CW\*(C`enable\*(C'\fR on any middleware, the middleware app is
pushed to the stack inside the builder, and then reversed when it
actually creates a wrapped application handler, so:
.PP
.Vb 5
\& builder {
\& enable "Plack::Middleware::Foo";
\& enable "Plack::Middleware::Bar", opt => "val";
\& $app;
\& };
.Ve
.PP
is syntactically equal to:
.PP
.Vb 2
\& $app = Plack::Middleware::Bar\->wrap($app, opt => "val");
\& $app = Plack::Middleware::Foo\->wrap($app);
.Ve
.PP
In other words, you're supposed to \f(CW\*(C`enable\*(C'\fR middleware from outer to inner.
.SH "INLINE MIDDLEWARE"
.IX Header "INLINE MIDDLEWARE"
Plack::Builder allows you to code middleware inline using a nested
code reference.
.PP
If the first argument to \f(CW\*(C`enable\*(C'\fR is a code reference, it will be
passed an \f(CW$app\fR and is supposed to return another code reference
which is \s-1PSGI\s0 application that consumes \f(CW$env\fR in runtime. So:
.PP
.Vb 10
\& builder {
\& enable sub {
\& my $app = shift;
\& sub {
\& my $env = shift;
\& # do preprocessing
\& my $res = $app\->($env);
\& # do postprocessing
\& return $res;
\& };
\& };
\& $app;
\& };
.Ve
.PP
is equal to:
.PP
.Vb 4
\& my $mw = sub {
\& my $app = shift;
\& sub { my $env = shift; $app\->($env) };
\& };
\&
\& $app = $mw\->($app);
.Ve
.SH "URLMap support"
.IX Header "URLMap support"
Plack::Builder has a native support for Plack::App::URLMap with \f(CW\*(C`mount\*(C'\fR method.
.PP
.Vb 8
\& use Plack::Builder;
\& my $app = builder {
\& mount "/foo" => $app1;
\& mount "/bar" => builder {
\& enable "Plack::Middleware::Foo";
\& $app2;
\& };
\& };
.Ve
.PP
See Plack::App::URLMap's \f(CW\*(C`map\*(C'\fR method to see what they mean. With
builder you can't use \f(CW\*(C`map\*(C'\fR as a \s-1DSL\s0, for the obvious reason :)
.PP
\&\fB\s-1NOTE\s0\fR: Once you use \f(CW\*(C`mount\*(C'\fR in your builder code, you have to use
\&\f(CW\*(C`mount\*(C'\fR for all the paths, including the root path (\f(CW\*(C`/\*(C'\fR). You can't
have the default app in the last line of \f(CW\*(C`builder\*(C'\fR like:
.PP
.Vb 4
\& my $app = sub {
\& my $env = shift;
\& ...
\& };
\&
\& builder {
\& mount "/foo" => sub { ... };
\& $app; # THIS DOESN\*(AqT WORK
\& };
.Ve
.PP
You'll get warnings saying that your mount configuration will be
ignored. Instead you should use \f(CW\*(C`mount "/" => ...\*(C'\fR in the last
line to set the default fallback app.
.PP
.Vb 4
\& builder {
\& mount "/foo" => sub { ... };
\& mount "/" => $app;
\& }
.Ve
.PP
Note that the \f(CW\*(C`builder\*(C'\fR \s-1DSL\s0 returns a whole new \s-1PSGI\s0 application, which means
.IP "\(bu" 4
\&\f(CW\*(C`builder { ... }\*(C'\fR should normally the last statement of a \f(CW\*(C`.psgi\*(C'\fR
file, because the return value of \f(CW\*(C`builder\*(C'\fR is the application that
actually is executed.
.IP "\(bu" 4
You can nest your \f(CW\*(C`builder\*(C'\fR block, mixed with \f(CW\*(C`mount\*(C'\fR (see URLMap
support above):
.Sp
.Vb 5
\& builder {
\& mount "/foo" => builder {
\& mount "/bar" => $app;
\& }
\& }
.Ve
.Sp
will locate the \f(CW$app\fR under \f(CW\*(C`/foo/bar\*(C'\fR since the inner \f(CW\*(C`builder\*(C'\fR
block puts it under \f(CW\*(C`/bar\*(C'\fR and it results a new \s-1PSGI\s0 application
which is located under \f(CW\*(C`/foo\*(C'\fR because of the outer \f(CW\*(C`builder\*(C'\fR block.
.SH "CONDITIONAL MIDDLEWARE SUPPORT"
.IX Header "CONDITIONAL MIDDLEWARE SUPPORT"
You can use \f(CW\*(C`enable_if\*(C'\fR to conditionally enable middleware based on
the runtime environment. See Plack::Middleware::Conditional for
details.
.SH "SEE ALSO"
.IX Header "SEE ALSO"
Plack::Middleware Plack::App::URLMap Plack::Middleware::Conditional