.\" -*- mode: troff; coding: utf-8 -*- .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43) .\" .\" 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 .. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. .ie n \{\ . ds C` "" . ds C' "" 'br\} .el\{\ . 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 >0, 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 .\" ======================================================================== .\" .IX Title "Plack::Middleware::Conditional 3pm" .TH Plack::Middleware::Conditional 3pm 2024-01-20 "perl v5.38.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 Plack::Middleware::Conditional \- Conditional wrapper for Plack middleware .SH SYNOPSIS .IX Header "SYNOPSIS" .Vb 1 \& use Plack::Builder; \& \& builder { \& enable_if { $_[0]\->{REMOTE_ADDR} eq \*(Aq127.0.0.1\*(Aq } \*(AqStackTrace\*(Aq, force => 1; \& $app; \& }; \& \& # or using the OO interface: \& $app = Plack::Middleware::Conditional\->wrap( \& $app, \& condition => sub { $_[0]\->{REMOTE_ADDR} eq \*(Aq127.0.0.1\*(Aq }, \& builder => sub { Plack::Middleware::StackTrace\->wrap($_[0], force => 1) }, \& ); .Ve .SH DESCRIPTION .IX Header "DESCRIPTION" Plack::Middleware::Conditional is a piece of meta-middleware, to run a specific middleware component under runtime conditions. The goal of this middleware is to avoid baking runtime configuration options in individual middleware components, and rather share them as another middleware component. .SH EXAMPLES .IX Header "EXAMPLES" Note that some of the middleware component names are just made up for the explanation and might not exist. .PP .Vb 2 \& # Minify JavaScript if the browser is Firefox \& enable_if { $_[0]\->{HTTP_USER_AGENT} =~ /Firefox/ } \*(AqJavaScriptMinifier\*(Aq; \& \& # Enable Stacktrace when being accessed from the local network \& enable_if { $_[0]\->{REMOTE_ADDR} =~ /^10\e.0\e.1\e.*/ } \*(AqStackTrace\*(Aq; \& \& # Work with other conditional setter middleware: \& # Transcode Jpeg on the fly for mobile clients \& builder { \& enable \*(AqMobileDetector\*(Aq; \& enable_if { $_[0]\->{\*(Aqplack.mobile_detected\*(Aq} } \& \*(AqTranscodeJpeg\*(Aq, max_size => 30_000; \& $app; \& }; .Ve .PP Note that in the last example \fIMobileDetector\fR should come first because the conditional check runs in \fIpre-run\fR conditions, which is from outer to inner: that is, from the top to the bottom in the Builder DSL code. .SH AUTHOR .IX Header "AUTHOR" Tatsuhiko Miyagawa .PP Steve Cook .SH "SEE ALSO" .IX Header "SEE ALSO" Plack::Builder