Writing an HTTP Server in Perl 6

Note! This site is about Perl 6.
If you are looking for a solution for Perl 5, please check out the Perl 5 tutorial.

Back in June 2011, I recoreded a screencast explaining how to use the then latest RAkudo * release that came with the HTTML::Server::Simple module. If you are interested, it is still mostly relevant. (Except that the msi is now created by the Rakudo team and not by myself.)

Direct link to the HTTP Server in Perl 6 screencast

panda install HTTP::Server::Simple

Rakudo where you will find a link to the download page in Github.

There you can pick the latest Windows Installer download it to your local disk and run the exe file.

Rakudo Star also comes with HTTP::Server::Simple, a Perl 6 module to implement simple web servers. Just type in

use v6;

use HTTP::Server::Simple;;

and you have a working web server. Of course if you kill this web server you will also kill the Perl 6 REPL but we don't need it any more now.

We will take a look at the examples that are part of the HTTP::Server::Simple package. The first examples: 01-simple-base.pl6 is about the same as we typed in. It looks like this:

use HTTP::Server::Simple; my HTTP::Server::Simple $server .= new; $;

The first line loads the module as usual. In the second line

my HTTP::Server::Simple $server declares a scalar variable called $server that must be of type HTTP::Server::Simple


.= new; part calls the constructor of the same class and assigns the result back to the left hand side. Finally


calls the run method on the object launching the web server on its default port of 8080.

The second example ( 02-simple-small.pl6 ) is slightly more complex:

use v6; use HTTP::Server::Simple; my $NL = "\x0D\x0A"; class Example::Simple::Small is HTTP::Server::Simple { has %!header; has $!path; has $!query_string; method header ( $key, $value ) { %!header{$key} = $value; } method path ($path) { $!path = $path; } method query_string ($query_string) { $!query_string = $query_string; } # override the request handler of the base class method handle_request () { print "HTTP/1.0 200 OK"; print "$NL$NL"; say "\n"; say "{self.WHAT} at {$!host}:{$!port}"; say q{}; for %!header.keys -> $key { my $value = %!header{$key}; say ""; } say "
"; say "Path: {$!path}
"; say "Query string: {$!query_string}
"; say "\n"; } } my Example::Simple::Small $server .= new; $; # now browse http://localhost:8080/whatever?name=value

It starts by making sure this code is only consumed by a perl 6 compiler, loads the module and then declares a new class called Example::Simple::Small subclassing the HTTP::Server::Simple. class Example::Simple::Small is HTTP::Server::Simple {

This is followed by the definition of 3 private attributes and then 3 methods to update those attributes. The main method (handle_request) will first print the name of the class, the host name and the port-number. Then it will print an HTML table showing the details of the request header. Finally it also prints the path of the request and the query string. These are the essential parts of any HTTP request.

This is of course very bare bones but it allows you to start playing with this and start implementing web applications using Perl 6.

I am also sure that in any real application you will use a templating system and will not embed html in the Perl code.


Thank you for watching this video. If you liked it, please upvote it and comment on it to encourage me to make more such screencasts.

Oh, and don't forget to subscribe to the channel!

This entry was also sent out as part of the Perl 6 Tricks and Treats. Visit here to subscribe.

Watch more Perl 6 related screencasts.

The Perl 6 Tricks and Treats newsletter has been around for a while. If you are interested to get special notification when there is new content on this site, it is the best way to keep track:
Full name:
This is a newsletter temporarily running on my personal site ( using Mailman, till I implement an alternative system in Perl 6.
Gabor Szabo
Written by Gabor Szabo

Published on 2012-07-04


In the comments, please wrap your code snippets within <pre> </pre> tags and use spaces for indentation.
comments powered by Disqus
Suggest a change
Elapsed time: 3.26481046

Perl 6 Tricks and Treats newsletter

Register to the free newsletter now, and get updates and news.