Find file
Fetching contributors…
Cannot retrieve contributors at this time
61 lines (44 sloc) 2.48 KB


The part of called Core aims to abstract away the tasks common to most web applications: getting information from the environment, parsing URLs, escapting HTML content, handling GET and POST requests, etc. It also decouples the server engine from the web application logic, making the former easier to swap out for some other server engine.

The assumption made by the core is that your web application is a Callable object taking the environment as a parameter, and returning an HTTP response code, a header, and a body. In pseudocode:

 sub my-web-app(%env) {
     # here's where the actual web application happens
     # ...
     return [$response, %header, @body];
 } &my-web-app );

That last line there plugs together the web application contained in the subroutine my-web-app with a particular server engine (in this case HTTP::Daemon), mediated through a Web::Handler. More about those below.

In a way, Core is the glue that ties all the things together in an application such as the above. Something has to make sure that the server engine (say, HTTP::Daemon) sends the request information in the form of an environment hash over to your web application, and then that the web application sends the response-header-body tuple back. That's what Core does. It talks to the backend for you.

The scheme is quite simple as it is, but it can be made even simpler. Two convenience classes unburden most of the tedium of handling the environment and the response-header-body tuple, respectively: Web::Request and Web::Response.


Web::Request wraps the environment. The environment is just a Hash, much like the Perl 6 %*ENV. In fact, in the case of CGI scripts, it is %*ENV. Web::Request provides a read/write API through which you can access this hash efficiently.

 my $req =;
 # do things with $req, described below

The class does not copy the environment Hash. Any modifications you make through Web::Request are made on the original object you sent in.


Using Web::Response, you don't have to construct the response-header-body tuple manually. Instead, you prep your Web::Response object in various ways, until it contains what you want, and then serialize the object into a tuple.

 my $res =;
 # do things with $res, described below
 return $res.finish(); # the finish method returns [$response, %header, @body]