Strangely Consistent

Musings about programming, Perl 6, and programming Perl 6

A Perl one-liner grows up

I wanted an overview of S29. More exactly, I wanted a list of functions. The table of contents in S29 itself didn't cut it, because it stopped short at the level of classes, and left out the functions. That's when I decided to extract the relevant information using a script of my own. I mean, how hard can it be?

Turns out it was easy.

Duh, right? We have Perl this is what Perl does: extract and report. (Practically.) Because it turned out to be a three-minute one-liner in Perl 5, I decided to turn it into a Perl 5 script file, and then into a Perl 6 script file.

The results were a bit interesting, and I thought I'd share.

First, a description of the problem:

Here's my one-line solution:

$ perl -e '$_ = <> until /Function Packages/; while (<>) { if (/^=(\S+) ↩
(.*)/) { next if $1 eq "over"; print " " x 4 if $1 eq "item"; print $2,↩
"\n" } }' S29-functions.pod

If you're interested in the output it produces, try running it after cd-ing into the directory docs/Perl6/Spec in the Pugs repo.

Here's the corresponding turned-into-a-file script (still Perl 5):

use strict;

open my $S29, '<', 'S29-Functions.pod' or die $!;

until (<$S29> =~ /Function Packages/) {}

while (my $line = <$S29>) {
    if ($line =~ / ^ = (\S+) ' ' (.*) /x) {
        next if $1 eq 'over';
        if ($1 eq 'item') {
            print ' ' x 4;
        }
        print $2, "\n"
    }
}

Things are a bit more indented, a bit more Best Practices, but no revolutionary changes. It's the same script.

Now, take a look at the Perl 6 script:

use v6;

my $S29 = open('S29-Functions.pod', :r) or die $!;

repeat {} until =$S29 ~~ /'Function Packages'/;

for =$S29 -> $line {
    if $line ~~ / ^ '=' (\S+) ' ' (.*) / {
        next if $0 eq 'over';
        if $0 eq 'item' {
            print ' ' x 4;
        }
        say $1;
    }
}

Noteworthy differences:

There you have it. A Perl one-liner grows up.