Accounts package in #perl6

I have written various accounts packages over the years using Haskell, Lisp, Python, Golang, Forth, C++, amongst others. The largest code-base  I had was approaching 4000 lines of code, and had such features as downloading share prices.

With the disappearance of good stock price sources, I decided it was time to ditch my current software and see if I could create something minimalistic, and more in tune with the UNIX philosophy.

After a few attempts, I eventually settled on a perl 5 script that I created that weighed in at 117 lines of code. It is able to prepare a set of accounts, show entries with running balances for an account of choice, creates a portfolio suitable for loading into Stockopedia, and even a file for uploading into CGTcalculator, which computes capital gains on a stock portfolio.

The code does, of course, keep things really simple. The input data files are text files of “commands”, which can be handily placed under version control. The trick to getting the code down to 117 lines is to use a high-level programming language, producing only the stuff you need, none of the stuff you don’t, and paring down your requirements.

In the past, I have toyed with the hugely popular wiegley ledger, which is also a plain text accounting system. I don’t recall how large the code base is, but I would imagine it is easily over 10k lines of code. GNUcash would dwarf even that. These programs do not play well with the UNIX pipeline, either, so I can’t really use them as pre- or post- processors.

My knowledge of perl 5 is near zero, as is my knowldege of perl 6. I had been interested in P6 a couple of years ago, but never stuck with it. I saw a few YouTube videos on P6 recently, and it sparked my interest again. I wondered if it was possible to rewrite my P5 proggie in P6.

After a few hours spread over a few days, I managed to get it working. Fortunately, P6 is starting to appear in repos like Ubuntu, which eliminates at least one barrier to entry. My finished programmed weighed in at 91 lines, and performs the same functions. That’s a 22% saving in code, or, put another way, my original code was 28% longer. I’m quite chuffed with that; a usable program in under 100 lines of code.

There’s some nice little touches in P6. One feature I particularly like is the “.” operator, which allows you to chain computations. This seems like a very natural way of doing things. You don’t have to introduce variables, and the sequence of expressions flow from left to right.

The gather/take command is rather handy, too. It allows you to generate multiple values to be be collected (gather’ed) over each loop.

Very nice.

P6 is rather sluggish, mind. I see this as a huge off-putting factor to most developers. My original P5 code took 0.051s (real time) to execute. The P6 code took 1.019s, which is around 20X as long. Oh well.

Also worth mentioning is that the last time I looked, it didn’t have a Windows 32-bit version. I dare say that the situation hasn’t changed. I am using Linux currently, and I may be disappointed if I have to move back to Windows. I do not have a 64-bit version of Windows.

It was fun working in P6, and I think it is likely that I will use it in other projects.

Update 05-May-2019: For those wanting to see the code for both P5 and P6, a gist is available. I hesitate to post, because both it and the underlying file format is undocumented. It also has a number of features that will not be of interest to anyone else. Most people won’t be interested in the portfolio feature, either. If all these extraneous features were stripped away, then an accounting package would only require about a dozen lines of code.

I noticed that I could perform a neat little reduction in my P6 code. Here:

my $qty-text ={sprintf("%-10s   %5d", .key, .value) }).sort.join("\n");
spurt('out/qty1.txt', $qty-text);

makes use of an intermediate variable. We can eliminate it using perl’s pipelining operator:{sprintf("%-10s   %5d", .key, .value) }).sort.join("\n") ==> spurt('out/qty1.txt');

This is very neat feature of Perl 6 indeed. It seems to have an automatic currying feature. In fact, anonymous functions seem to feature heavily in P6, and are simply written in curly braces. There’s no need for declarations like

(lambda (x y) (whatever ...))

that you would have in Scheme/Python. This makes the code quite compact, and cuts out a lot of visual clutter.

Perl 6 clearly works hard to be a DWIM (Do What I Mean) language.

About mcturra2000

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

2 Responses to Accounts package in #perl6

  1. ralph says:

    .oO ( It would be nice to see the P5 and P6 code )

Leave a Reply

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

You are commenting using your 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