Router...okbook(3pm)

🌸 NAME 🌸

Okay so welcome to Router::Simple::Cookbook β€” aka The Router::Simple Cookbook. This page is Router...okbook(3pm) from the User Contributed Perl Documentation shelf, and yes the truncated title is part of the vibe, iykyk. It’s basically a little recipe box of Router::Simple patterns, and we are going through every single one. 🫢

πŸ’– FAQ πŸ’–

✨ How to create Sinatra-ish framework with Router::Simple? ✨

Bestie, literally just read the following example code. It’s giving tiny Sinatra-ish framework energy, and it’s all right here:

package MySinatraish;
use Router::Simple;
use Plack::Request;

sub import {
    my $pkg = caller(0);
    my $router = Router::Simple->new();
    my $any = sub ($$;$) {
        my ($pattern, $dest, $opt) = do {
            if (@_ == 3) {
                my ($methods, $pattern, $code) = @_;
                ($pattern, {code => $code}, +{method => [ map { uc $_ } @$methods ]});
            } else {
                my ($pattern, $code) = @_;
                ($pattern, {code => $code}, +{});
            }
        };
        $router->connect(
            $pattern,
            $dest,
            $opt,
        );
    };
    no strict 'refs';
    # any [qw/get post delete/] => '/bye' => sub { ... };
    # any '/bye' => sub { ... };
    *{"${pkg}::any"} = $any;
    *{"${pkg}::get"} = sub {
        $any->([qw/GET HEAD/], $_[0], $_[1]);
    };
    *{"${pkg}::post"} = sub {
        $any->([qw/POST/], $_[0], $_[1]);
    };
    *{"${pkg}::as_psgi_app"} = sub {
        return sub {
            if (my $p = $router->match($_[0])) {
                [200, [], [$p->{code}->()]];
            } else {
                [404, [], ['not found']];
            }
        }
    };
}

package MyApp;
use MySinatraish;
get '/' => sub {
    'top';
};
post '/new' => sub {
    'posted';
};
as_psgi_app;

No skipping, okay? Every line is doing its little job. πŸ’…

πŸ¦‹ How to switch from HTTPx::Dispatcher? πŸ¦‹

So HTTPx::Dispatcher is a class-specific declarative router β€” very structured, very β€œI have a type.” Here’s the original flavor:

package MyApp::Dispatcher;
use HTTPx::Dispatcher;
connect '/', {controller => 'foo', action => 'bar'};
1;

And the following script is same as above. Same energy, new router bestie:

package MyApp::Dispatcher;
use Router::Simple::Declare;

my $router = router {
    connect '/', {controller => 'foo', action => 'bar'};
};
sub match { $router->match() }

🌈 How to use Router::Simple with non-strictly-MVC application? 🌈

Here’s the non-strictly-MVC setup, and it’s lowkey iconic:

use Router::Simple::Declare;
my $router = router {
    connect '/foo/bar/' => { 'target' => '/foobar.asp' };
    connect '/topics/:topic' => { target => '/my-topic.asp' };
    connect '/products/{Category:.*}' => { target => '/products.asp', Category => 'All' };
    connect '/zipcode/{zip:[0-9]{5,5}}' => {target => '/zipcode.asp' };
};

And yes, you can pass the target path as destination β€” that’s the whole move. πŸŽ€

πŸ‘‘ AUTHOR πŸ‘‘

Tokuhiro Matsuno <tokuhirom AAJKLFJEF GMAIL COM>

πŸŽ€ LICENSE πŸŽ€

Copyright (C) Tokuhiro Matsuno
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

🌈 SEE ALSO 🌈

Router::Simple β€” go check it out, bestie. ✨


Cute little sign-off: this page says perl v5.42.0, dated 2015-04-13, and the footer is Router...okbook(3pm). Byeee πŸ’…πŸ’–

πŸ“¦ where this page comes from