Plack is a superglue for Perl web frameworks that provides a common interface called PSGI (Perl Web Server Gateway Interface) inspired by WSGI and Rack. PSGI allows any web application or framework to run on any web server by providing a standard way for applications to communicate with servers. Plack also includes tools like Plackup for running PSGI applications from the command line and middleware for common functionality that can be shared across frameworks. Many existing Perl web frameworks have been adapted to run under PSGI through Plack.
2. Tatsuhiko Miyagawa
• Lives in San Francisco
• Software Engineer @ Six Apart
• http://search.cpan.org/~miyagawa/
• @miyagawa
• http://bulknews.typepad.com/
6. use FCGI;
my $req = FCGI::Request();
while ($req->Accept >= 0) {
print “Content-Type: text/plainrnrn”;
print “Hello World”;
}
7. package HelloWorld;
use strict;
use Apache::RequestRec;
use Apache::RequestIO;
use Apache::Const -compile => qw(OK);
sub handler {
my $r = shift;
$r->content_type(‘text/plain’);
$r->print(“Hello World”);
return Apache::Const::OK;
}
1;
8. package HelloWorld;
use base qw(HTTP::Server::Simple::CGI);
sub handle_request {
my($self, $cgi) = @_;
print “HTTP/1.0 200 OKrn”;
print “Content-Type: text/plainrnrn”;
print “Hello World”;
}
1;
80. my $app = sub {
my $env = shift;
return [ $status, $header, $body ];
};
my $mw = sub {
my $env = shift;
# do something with $env
my $res = $app->($env);
# do something with $res;
return $res;
};
89. use CatApp;
use CGIApp;
my $c1 = sub { CatApp->run };
my $c2 = sub { CGIApp->run_psgi };
use Plack::Builder;
builder {
mount “/cat” => $c1;
mount “/cgi-app” => builder {
enable “StackTrace”;
$c2;
};
}
94. use Plack::Test;
use HTTP::Request::Common;
my $app = sub {
my $env = shift;
return [ $status, $header, $body ];
};
test_psgi app => $app, client => sub {
my $cb = shift;
my $req = GET “http://localhost/foo”;
my $res = $cb->($req);
# test $res;
};
95. use Plack::Test;
use HTTP::Request::Common;
$Plack::Test::Impl = “Server”;
my $app = sub {
my $env = shift;
return [ $status, $header, $body ];
};
test_psgi app => $app, client => sub {
my $cb = shift;
my $req = GET “http://localhost/foo”;
my $res = $cb->($req);
# test $res;
};
111. You can even try:
system(“rm -fr /”);
while (1) { }
112. Summary
• PSGI is an interface, Plack is the code.
• We have many (pretty fast) PSGI servers.
• We have adapters and tools for most web
frameworks.
• Use it!