stevebob.net

This Blog is Written in Perl

2011-12-25 23:50

Let me take a minute to explain myself. Perl was not my first choice of language when I decided to write a blog website. I initially started writing the site in Ruby using Sinatra and I really liked it. Coding websites in Ruby with Sinatra is intuitive and the code is readable.

As with most web apps written in Ruby, Sinatra apps run as their own process and include a web server (such as Thin). That is, the web app includes a script that launches the app and starts a web server. This is in contrast to CGI - the more traditional approach to dynamic web content, where an app is a script that prints the HTML to be sent to the browser and the web server is a separate process (such as Apache). The benefit of the former approach over the latter approach is performance. Each time a request is made to a CGI web app, the entire program must be loaded into memory and executed. When a web app is its own process (as is the case with Sinatra apps), it is loaded when the server is started and remains in memory as long as the server runs. This streamlines the process of servicing requests made to the app, making it a faster approach to CGI.

So with this performance benefit, why did I write this site in Perl CGI?

My web server does not allow me to run my own processes meaning I could not use it for Sinatra apps. Not to be perturbed, I looked for alternative hosting options and came across Heroku. Heroku is a platform for deploying web apps written in Ruby. Apps are deployed to Heroku using a git repository. Once configured, the latest version of the app can be deployed to Heroku using git push. Apps deployed to Heroku are compiled into optimized packages known as “slugs”, boosting their performance.

In addition to the web component, my blog needed access to a database in which to store posts, and some means of uploading and storing images so I can post photos.

Apps deployed on Heroku do not have complete access to a filesystem. Apps may observe their filesystem (using commands like ls and cat file) but may not create or modify files directly. This meant I could not simply upload photos, as this requires creating files. Users of Heroku are encouraged to use third party sites for file hosting, and there are modules available that may be used to connect to these services.

Furthermore, Heroku apps must use Postgres as a database, and the only way to freely (without paying) use a database is to be part of a shared database. I modified my blog to use Postgres (it previously used Mysql) but was unable to connect to the database. Heroku provides documentation for connecting to a shared database but it is aimed at apps built on Rails. To get a database working for my blog, I made a web wrapper for a mysql database running on my web server. SQL statements were sent to the wrapper as post parameters, the statement was executed on the server, which responded with HTML containing the result of the query which could be passed by the Ruby app on Heroku. This worked, but it was abandoned as it seemed insecure and very hacky.

At this point I was frustrated by all the restrictions being placed upon me by Heroku. I could write a blog in Ruby using Sinatra but I would have no way to deploy it. I didn’t want to find other sources and just wanted to get something online, so I opted for rewriting the blog as a CGI app. Writing CGI apps in Ruby is discouraged [0], possibly due to the language’s poor performance when compared with other scripting languages [1]. I chose Perl because I know it better than the other candidate scripting languages (such as Python or PHP).

This was either the biggest or second biggest Perl project I have undertaken, and it was my first time writing object oriented Perl. In Perl, most object oriented concepts (such as defining classes, creating instances of classes, extending classes, calling methods on objects) are possible, however do not feel as elegant as they do in other object oriented languages I have used (Java, Ruby, Python). Needless to say, I didn’t get into Perl for the syntax. Once you get past the syntax however, Perl’s object oriented features are quite usable. The main use of OO in this blog is elegant handling of database records (by treating them as objects with getters and setters for fields) and grouping together related functions (by making them all methods of a class). For these purposes, I found Perl’s object oriented features to be adequate.

Camelia - the spokesbug for Perl 6
camelia

The current version of Perl is Perl 5. Perl 6 is currently in development, and will be a complete re-write of Perl in which the entire Perl community is invited to take part. A glance at its Wikipedia article shows examples of static types, improved object oriented syntax and infinite lists (resembling those found in Haskell). There is currently no official release date on Perl 6, however there are several implementations of the language.

[0]http://www.dreamincode.net/forums/topic/259314-development-server-fpr-ruby/

[1]http://xodian.net/serendipity/index.php?/archives/27-Benchmark-PHP-vs.-Python-vs.-Perl-vs.-Ruby.html