Plack:...ionals(3pm)
π Plack::Builder::Conditionals β bestie edition π
okay so this is a page from the User Contributed Perl Documentation, and it's literally Plack::Builder::Conditionals β a Plack::Builder extension that lets you write middleware conditions that actually make sense. no cap, it's giving "why did I ever write nested ifs" energy. π¦
β¨ NAME β¨
Plack::Builder::Conditionals β a Plack::Builder extension, and yes that's the whole name, she's modest π
πΈ SYNOPSIS πΈ
okay so first things first, you load it up and it hands you a whole little friend group of exports: match_if, addr, path, method, header, browser, any, all. π
use Plack::Builder;
use Plack::Builder::Conditionals;
# exports "match_if, addr, path, method, header, browser, any, all"
builder {
enable match_if addr(['192.168.0.0/24','127.0.0.1']),
"Plack::Middleware::ReverseProxy";
enable match_if all( path(qr!^/private!), addr( '!', [qw!127.0.0.1 ::1!] ) ),
"Plack::Middleware::Auth::Basic", authenticator => \&authen_cb;
enable match_if sub { my $env = shift; $env->{HTTP_X_ENABLE_FRAMEWORK} },
"Plack::Middleware::XFramework", framework => 'Test';
$app;
};
use Plack::Builder::Conditionals -prefx => 'c';
# exports "c_match_if, c_addr, c_path, c_method, c_header, c_any, c_all"
and if you want your exports to come pre-decorated with a prefix, you can do that too β see the -prefx bit at the bottom, she's got a whole thing π
π DESCRIPTION π
Plack::Builder::Conditionals is.. π«Ά
(yes bestie, that's genuinely where the description stops. iconic of her honestly. the FUNCTIONS section is where the real tea is β)
π§ FUNCTIONS π§
match_if
enable match_if addr('127.0.0.1'), "Plack::Middleware::ReverseProxy";
enable match_if sub { $_[0]->{REMOTE_ADDR} eq '127.0.0.1' }, "Plack::Middleware::ReverseProxy";
this one is basically Plack::Builder's enable_if but with the glow-up π β she enables the middleware whenever the conditions you handed her come back true.
addr
addr('127.0.0.1');
addr([qw!192.168.0.0/24 127.0.0.1 ::1!]);
addr('!','127.0.0.1');
returns true when REMOTE_ADDR is chillin' somewhere inside the CIDR range. π΅οΈββοΈ and plot twist: if the first argument is '!', she flips the whole result β the opposite, periodt. also she supports IPv6 addresses, which, slay. πβ¨
path
path('/')
path(qr!^/(\w+)/!)
path('!', qr!^/private!)
this one is matching PATH_INFO. that's the vibe check. π¦
method
method('GET')
method(qr/^(get|head)$/i)
method(qw(GET HEAD))
method('!','GET')
method('!', qr/^(post|put)$/i)
method('!', qw(POST PUT))
request method matching, and she takes it in literally every format you could want β plain string, regex, a whole list, or any of those with a '!' in front to flip it. no notes. π
header
header('User-Agent',qr/iphone/)
header('If-Modified-Since') #exists check
header('!', 'User-Agent',qr/MSIE/)
check a header against a regex, or just check that a header exists at all (that's the one-argument #exists check version, bestie), or throw a '!' at the front to get the opposite. πΈ
browser
browser(qr/\bMSIE (7|8)/)
browser('!',qr!^Mozilla/4!);
a shortcut for header('User-Agent') β because typing the whole thing every single time is exhausting and you have better things to do. π
all
all( method('GET'), path(qr!^/static!) )
returns true only if all the conditions return true. she's an overachiever, she needs everyone on board. π
any
any( path(qr!^/static!), path('/favicon.ico') )
returns true if any condition returns true. lowkey the chill one of the group. π
π EXPORT π
use Plack::Builder::Conditionals -prefx => 'c';
# exports "c_match_if, c_addr, c_path, c_method, c_header, c_any, c_all"
you can add a selected prefix to the exported functions β so if you're worried about name collisions in your namespace, just hand her a prefix and she'll slap it on the front of everything. π
π AUTHOR π
Masahiro Nagano <kazeburo {at} gmail.com> β the icon, the legend, the main character who wrote all this π
π SEE ALSO π
Plack::Builder
π LICENSE π
this library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. fr fr, go wild, just keep the terms. π«Ά
okay and that's the whole page bestie!! it came from perl v5.34.0, dated 2022-06-16, and the manual itself is Plack:...ionals(3pm) β yes the title is abbreviated like that in the header and footer, she's mysterious, we love her. go build something iconic with your conditionals. β¨π¦π
π¦ where this page comes from
- debian bookworm: libplack-builder-conditionals-perl 0.05-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libplack-builder-conditionals-perl 0.05-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libplack-builder-conditionals-perl 0.05-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libplack-builder-conditionals-perl 0.05-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libplack-builder-conditionals-perl 0.05-2 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms