Plack::...itional(3)
β¨ NAME β¨
Plack::Middleware::Conditional β a conditional wrapper for Plack middleware, bestie π
(this is from the User Contributed Perl Documentation fam, so you know it's the good stuff)
πΈ SYNOPSIS πΈ
okay so here's the vibe, you can do it with Plack::Builder:
use Plack::Builder;
builder {
enable_if { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' } 'StackTrace', force => 1;
$app;
};
or, if you're an OO interface girlie:
# or using the OO interface:
$app = Plack::Middleware::Conditional->wrap(
$app,
condition => sub { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' },
builder => sub { Plack::Middleware::StackTrace->wrap($_[0], force => 1) },
);
π DESCRIPTION π
so Plack::Middleware::Conditional is a piece of meta-middleware, which is giving very main character energy ngl π β it runs a specific middleware component under runtime conditions.
the whole goal here? it's to avoid baking runtime configuration options into individual middleware components, and instead share them as another middleware component. like, stop hardcoding your faves into every single middleware, share the config, it's called community π
π¦ EXAMPLES π¦
heads up bestie, some of these middleware component names are just made up for the explanation and might not actually exist π
# Minify JavaScript if the browser is Firefox
enable_if { $_[0]->{HTTP_USER_AGENT} =~ /Firefox/ } 'JavaScriptMinifier';
# Enable Stacktrace when being accessed from the local network
enable_if { $_[0]->{REMOTE_ADDR} =~ /^10\.0\.1\.*/ } 'StackTrace';
# Work with other conditional setter middleware:
# Transcode Jpeg on the fly for mobile clients
builder {
enable 'MobileDetector';
enable_if { $_[0]->{'plack.mobile_detected'} }
'TranscodeJpeg', max_size => 30_000;
$app;
};
okay and the important part π«Ά β in that last example, MobileDetector has to come first, because the conditional check runs in pre-run conditions, which go from outer to inner. which means... from the top to the bottom in the Builder DSL code. periodt β¨
π AUTHOR π
- Tatsuhiko Miyagawa
- Steve Cook
π SEE ALSO π
Plack::Builder
and that's a wrap, babe πβ¨ this little slice of the docs shipped as perl v5.42.3 on 2026-10-18 β π go forth and make your middleware conditional, you absolute icon π¦
π¦ where this page comes from
- fedora 43: perl-Plack 1.0055-1.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- fedora 44: perl-Plack 1.0055-1.fc44 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms