A Big Day for a Small Language

7th May 2022

Back when I started working on Pyro, my hobby programming language, I had one ambition — I wanted to replace the ugly PHP script that powered the contact form on my website with a Pyro script.

Mission accomplished.

Actually, it turns out that it's surprisingly easy to handle HTTP requests using a brand-new, only-installed-on-one-server-in-the-entire-universe scripting language.

The key is a protocol called CGI.

CGI — The Common Gateway Interface

CGI, or the Common Gateway Interface, is a walking dinosaur — a throwback to the early days of the web. It's a simple protocol that lets a webserver proxy a HTTP request to an external program, typically a program written in a scripting language. At the time CGI was popular, back in the 1990s, that language was most likely to be Perl, but it can be any language at all.

CGI fell out of favour because it doesn't scale well to handling hundreds or thousands of concurrent requests, but it's still supported by most major webservers, and it's ridiculously easy to use.

Take Pyro as an example. To enable CGI for Pyro scripts you can configure your webserver — say Apache or Nginx — to treat all files ending in .pyro as CGI targets. Then if the server gets a request for a file ending in .pyro, instead of returning the file to the client as a blob of data, it executes it as a program.

The server passes data about the request to the program using environment variables and writes the request body (if there is one) to the program's standard input stream.

In return, the program writes its response to its standard output stream and that output is what the server sends back to the client.

CGI Pyro

Here's a proof-of-concept example of a CGI script written in Pyro. It reads a handful of CGI environment variables and writes their values back to the client as a simple HTML page.

#!/usr/bin/env pyro

var remote_addr = $env("REMOTE_ADDR");
var request_method = $env("REQUEST_METHOD");
var request_uri = $env("REQUEST_URI");

var response_body = "
<html>
    <head>
        <title>Pyro CGI Handler</title>
    </head>
    <body>
        <h1>Pyro CGI Handler</h1>
        <ul>
            <li><code>REMOTE_ADDR: ${remote_addr}</code></li>
            <li><code>REQUEST_METHOD: ${request_method}</code></li>
            <li><code>REQUEST_URI: ${request_uri}</code></li>
        </ul>
    </body>
</html>";

echo response_body;

You can see this script in action here.

So what's next for Pyro?

Breaking into the mainstream is a tough challenge for a new scripting language. Established languages like Python and Ruby may have their flaws, but they also have decades of libraries and tooling on their side — the table stakes for a new language get higher every year.

To succeed, a new language needs to find a specialized niche for itself — a limited domain it can make its own — where it can mature and develop a community of users.

If I actually had ambitions for Pyro to become a mainstream language (I don't), I think I'd target the niche that PHP continues to occupy unchallenged — the "upload a script file to a server, congratulations you have a dynamic website" niche. I'm a little surprised that nobody has knocked PHP off its perch here already, but I get the feeling that nobody has ever really tried.

And in practice, I'll be adding my name to the list of people who haven't really tried. Building a programming language is a lot of fun — promoting a programming language... not so much, at least for me.

I'm just happy to have built an elegant little language, and to have achieved my original goal of replacing PHP for my own limited use case.