Plack:...ionals(3pm)

πŸ“¦
version 0.05 Β· 2 versions
  • 0.05 Β· debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking (latest)
  • 0.05 Β· ubuntu jammy
, in debian bookworm, debian trixie, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– 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('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