Oh no, I’m beginning to like perl5

I’ve been writing and rewriting my plain text accounting package in various languages for some time now. Incarnations have included bash/awk/sed, C, C++, Haskell, golang, Lisp, and Scheme.

I decided to try writing it in the vilest language of all: P5 (Perl 5). It’s 75 lines long. I expect the finished version will be no longer than 100 lines.

Here’s where I’m finding the strengths of Perl 5 to be:

  • Simple things are simple
  • It’s faster than Python
  • It’s near-ubiquitous.
  • It’s easy to pick up, despite the fact that I have rarely programmed in perl 5

Simple things are simple

Perl 5 seems to have anticipated common use cases, and catered to them. I need to convert dates in the format YYYY-MM-DD to DD/MM/YYYY. Here’s how you do it in Perl 5:

use Time::Piece;
Time::Piece->strptime($dstamp, "%Y-%m-%d")->strftime("%d/%m/%Y");

I don’t have to deconstruct the date stamp into components, and reassemble them. One line, and I’m done.

Later on in the program, I want to accumulate the balances for each account. I define the balances:

my %gaaps;

and I accumulate them:

$gaaps{$dr} += $amount;
$gaaps{$cr} -= $amount;

There are two magical things that P5 does for me here:

  1. Converts a string amount into a number
  2. Provides a default value for keys that are being newly defined

It’s basically taken away a lot of the “busy work” that you would have to do with other languages.

I had written a parser in lex and yacc for my C++ code in order to split out the input commands. It was useful experience. But I realised that I could do the job much more simply in P5 if I used split. I split a command into its name and arguments. Once I know the name, I know how may arguments it should take, and I could subsplit accordingly.

I could have achieved similar functionality in C++. C++ does, after all, include a regex library, and if I were to rewrite my program, that’s what I’d probably do.

I’d also like to mention Scheme here. I wrote a little module in chicken scheme, only to find that a macro I declared in the module couldn’t find a function that was defined in the module. I’m not sure what the problem is; I think it had something to do with when things are defined. Macros don’t fit well with Unix libraries, so I’m thinking that the scheme separated out the macros and defined them elsewhere, so it couldn’t see the definitions.

Plus, Scheme is rather a small language, and I had to install some extra modules. No biggie per se, but that’s additional infrastructural work.

And that’s kinda the point with the big, “serious” languages. They seem to have a lot of sharp corners. Things that don’t work the way you expect. Things that you have to set up. A lot of “accidental” complexity.

In short, P5 seems to have more “Do What I Mean” instead of me having to fuss over minutiae.

Faster than Python

Python is an excellent programming language. I want my code to work fast, even on a RPi2 (Raspberry Pi 2). Python isn’t “too” slow, but it’s slower than I find ideal.

I do have a lot of Python coding experience, and based on my experience with P5, the former doesn’t quite match the simplicity of Python.

What’s becoming clearer to me is that the designers of Perl (5 and 6) have sat down and thought to themselves “what do programmers typically mean and want when they use a certain construct in a certain way?” The designers have then gone away and made the language “do that thing.” Contrast that with the way that most other languages work: you have to specify behaviour.

I think this makes P5 an inherently more compact language, albeit possibly more dangerous.

Ubiquity

P5 is on par with Python. Both are ubiquitous, with P5 maybe having the slightest of edges. It’s great when you don’t have to install a language on account of it already being there. Windows is an exception, of course, but they’re both easy to install.

Easy to learn

Despite not having programmed in P5 more than a handful of times, I find it surprisingly easy to pick up.

Simplify the problem

Can you really write an accounts package in a 100 lines of code?

Put it this way: by the time I’ve finished, I fully expect it to print financial statements, individual accounts, CGT prep work and portfolio export.

Programmers like to claim bragging rights to writing and managing behemoths. Such systems make you feel clever.

Bill Gates once wrote “Measuring programming progress by lines of code is like measuring aircraft building progress by weight.” Except that Bill Gates is a hypocrite. When has Microsoft ever made lightweightness a priority? Never, that’s when. The Windows way is to build large integrated solutions, in contrast to the Unix way, which is to build small solutions that can be married together in unexpected ways.

A man that I admire much more is Charles Moore, inventor of Forth. He decries standard software approaches, and says he can write code in a tenth of the size that most other people do. I am not in the same league as him by a long shot.

But what you must do is solve the problem. Nothing more. Maybe you can simplify the problem. Maybe you can remove assumptions that make certain problems disappear.

A lot of people have come away thinking that Moore is “quaint” and “naive”. Moore has done some amazing work. The stuff he has produced works for him. The rub is that they probably won’t work for anyone else. That’s undoubtedly part of his secret as to why everybody else’s code is at least 10X, 100X, 1000X more complex than Moore’s.

Not P6 (Perl 6)?

No. I’ve toyed around with P6. It’s very very slow, and I don’t think I could stomach it on a Pi 2, assuming that it works at all. Also, P6 is not available for Windows 32-bit architectures.

About mcturra2000

Computer programmer living in Scotland.
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s