Statocles

Tags:

Statocles - A static blog generator in Perl.

This site is a static blog, so on the server there is just plain old HTML, the software that generates the pages is on my laptop, not on the server. Static content is efficient and fast, and I would suggest more secure. There is not code (beyond web-server) that people can attack.

Previously I have used Hugo which like Statocles runs locally and produces static content you upload to your server. Unlike Statocles, Hugo is written in Go, and as this website is all about Perl, I wanted to use a Perl based tool.

Wanting a Perl based tool, I search CPAN for "blog generator" and explored. Statocles had been favourited 26 times, a lot more than the other options, so I focused on it.

Statocles feels familiar to Hugo, there is a command line tool that helps you manage the content and serve a local version of the site. The last update was April 2020 (Aprils fools day made me a little nervous I confess) but I installed it locally and all worked pretty much out of the box.

Continue reading Statocles...

Perl Weekly Challenge

Tags:

The Perl Weekly Challenge is an amazing project run by Mohammad Anwar that provides coding challenges on a weekly basis; and more importantly a good variety of approaches written up (or shared as video) by developers in Perl and other programming languages.

This week for example the challenges are/were:

TASK #1: Reverse Words

You are given a string $S. Write a script to reverse the order of words in the given string. The string may contain leading/trailing spaces. The string may have more than one space between words in the string. Print the result without leading/trailing spaces and there should be only one space between words.

TASK #2: Edit Distance

You are given two strings $S1 and $S2. Write a script to find out the minimum operations required to convert $S1 into $S2. The operations can be 'insert', 'remove' or 'replace' a character.

Solving the challenge can be a challenge of finding an approach that works, though sometimes they are also simple. When simple I have found it enjoyable and rewarding to use it as a code kata where I might try different approaches or try solving in multiple programming languages.

So this weeks is one of those ones that clicked in my head so here is a small overview of my solution:

Here is the test...

    # t/pw96.t

    use strict;
    use warnings;

    use Test2::V0 -target => 'pw96';

    my $input    = 'The Weekly Challenge';
    my $expected = 'Challenge Weekly The';

    is $CLASS->reverse_words($input),
        $expected,
        'Example one is correct';


    $input    = '    Perl and   Raku are  part of the same family  ';
    $expected = 'family same the of part are Raku and Perl';

    is $CLASS->reverse_words($input),
        $expected,
        'Example two is correct';

    done_testing;

So as I have mentioned in other places, I default to Test2:V0 where possible. It's modern and full featured. It is a solid "Batteries included" testing library. This is a pretty basic set of tests.

Here is the code that followed the tests...

    # lib/pw96.pm

    package pw96;

    use strict;
    use warnings;

    use Moo;

    sub reverse_words {
        my ($self, $phrase) = @_;

        my @words = split ' ', $phrase;
        return join ' ', reverse @words;
    }

    1;

As you can see it's pretty simple; though in fact it's more complex than actually required.

Why?

Because I have another "default setting", I tend to use the Moo module for some object orientation ease. I don't "need" it so technically I am not doing the "simplest thing that works" depending on how you define simple. In the situation of doing this sort task; using a comfortable setup (Test2::V0 and Moo) means consistency which makes it simple over time?

This is more the making things simple to write, rather than writing the simplest thing.

At this stage I should point out that I did TDD this code; so the first thing I wrote was this:

    use Test2:V0 -target => 'pw96';
    done_testing;

The above failed, as I had not created the lib/pw96.pm file. So I created that with a little boiler plate:

    package pw96;
    1;

Which made the test pass. Next I tested for the method I wanted to write add in:

    can_ok($CLASS, 'reverse_words');

After watching that fail, I added this in the module:

    use Moo;
    sub reverse_words{}

All good! After which I added my first practical test (and deleting the can_ok test).

    my $input    = 'The Weekly Challenge';
    my $expected = 'Challenge Weekly The';

    is $CLASS->reverse_words($input),
        $expected,
        'Example one is correct';

After which I wrote the method in a basic way. Once that was working; I refined it to the two line solution. You could take it down a bit more; but where it sits is a nice balance of conciseness and simplicity to read.

So there you have it my solution to this weeks Perl Weekly Challenge.

A wild Perl.Kiwi appears...

Tags:

Kiaora dear reader,

welcome to this site all about the wonderful world of software development with the Perl programming language. My name is Lance, a New Zealander (hence the .kiwi domain) who writes computer software in the amazing language that is Perl.

In my day job I am a Software Development Manager for a large (20+ million users) recruitment website; there we use a variety of languages and technologies including Perl and C#. But this site is not about work, rather about the fun and power of the Perl programming language.

Perl is one of the most mature and well supported languages in the world today (2021), it's been used in production for decades and it powered the first wave of the world wide web. It's used as "glue code" a lot; and is powerful, fast, flexible and expressive.

After the first internet bubble bursting; Perl lost some of it's "cool". That said it's never gone away and continues to grow and evolve whilst powering productions systems day in and day out.

It's maturity means it has a vast array of third party libraries; virtually all free and open source. These libraries are generally heavily tested across a multitude of language versions, operating systems and hardware platforms.

Despite Perl's fall from fame; it remains a great language to use and this site is here to share my passion for this language and the things it allows me to do.As someone not currently coding day to day (I'm a manager for my sins), this site is where I shall be sharing my personal recreational coding in Perl (and some other languages).

As I write this initial post I have a few projects on the go. The main one being a old project from approximate 2003 that I picked up the code base again over the Christmas break and did the refurbishment work to get this old .CGI based app running again. I will share some posts on that process as saving and improving a legacy code base is an activity I don't often see written about; yet we all end up doing it.

More recently I have taken that base code and moved it into a more modern MVC style web framework turning it into a psgi application. This has some interesting things to cover as well. There is a different skill set and mindset between working on a legacy code base and creating a new application.

In 2020, I spent some time participating in the Perl Weekly Challenge; a Perl community initiative where you solve a coding challenge each week. I live-streamed some of those and will share here too. And I hope to live stream and share more of me doing these code kata style challenges.

So... the plan for this website is to separate out my Perl and coding passion from the rest of me and share it here. This means if you follow the RSS or the @PerlKiwi twitter feed it will just be coding and mostly Perl. All my other "stuff" will be elsewhere, trust me; it's a good thing. My Judo obsession will bleed over to here I am sure, but primarily this is going to be about Perl.

I hope if you are reading this that you are ready to share some of my experiences "pottering around in Perl". This site is mainly for myself to type up my Perl learning's and experiences as an archive of the fun and enjoyment it brings me. I do hope that some of what I write will serve a future Perl developer who googles for just the right thing and finds one of these pages has some practical experience of a challenge they are facing and helps them see how I tackled it.

Kia Manawanui!

Lance