読者です 読者をやめる 読者になる 読者になる

Let's start Perl6 with Crust!

Crust https://github.com/tokuhirom/p6-Crust is Plack for Perl6, developed by tokuhirom. Did you try it? If not, let's try it now!

> panda install Crust

The simplest example is below, which shows "Hello World!" when you access curl http://localhost:5000.

> crustup -e '-> $env { 200, [], ["Hello World!"] }'

How easy it is!

Crust already has some stuff. If you want to serve files in your current directory, type:

> crustup -MCrust::App::Directory -e 'Crust::App::Directory.new'

Again how easy it is!

Yes, Crust is written in Perl6, so I want to write code which cannot be written in Perl5.

Let's consider Plack/Crust Middleware. Middleware wraps your PSGI application which is thought of a plugin.

In Perl5, we create Plack::Middleware::Hoge class, and apply it to PSGI applications.

In Perl6, because sub {} is actually a Routine object, we can write an in-line Middleware like this:

my $app = sub ($env) {
    say $env<PATH_INFO>;
    [200, [], ["ok"]];
};

# middleware
$app.wrap: -> $env {
    $env<PATH_INFO> ~= "before-wrap";
    my @res = callwith($env);
    @res[2;0] ~= " after wrap";
    @res;
};

$app;

Wow!

Actually I'm just starting learning Perl6. So I definitely miss a lot of Perl6 features now.

I'm looking forward to learning awesome Perl6 features as writing Crust stuff!

Let's start Perl6 with Crust!