Day 2 – Interacting with the command line with MAIN subs

In Unix environment, many scripts take arguments and options from the command line. With Perl 6 it’s very easy to accept those:

    $ cat
    sub MAIN($x, $y) {
        say $x + $y
    $ perl6 3 4
    $ perl6 too many arguments
    Usage: x y

By just writing a subroutine called MAIN with a signature, you automatically get a command line parser, binding from the command line arguments into the signature variables $x and $y, and a usage message if the command line arguments don’t fit.

The usage message is customizable by adding another sub called USAGE:

    $ cat
    sub MAIN($x, $y) {
        say $x + $y
    sub USAGE() {
        say "Usage: <num1> <num2>";
    $ perl6 too many arguments
    Usage: <num1> <num2>

Declaring the MAIN sub as multi allows declaring several alternative syntaxes, or dispatch based on some constant:

    $ cat calc
    #!/usr/bin/env perl6
    multi MAIN('add', $x, $y)  { say $x + $y }
    multi MAIN('div', $x, $y)  { say $x / $y }
    multi MAIN('mult', $x, $y) { say $x * $y }
    $ ./calc add 3 5
    $ ./calc mult 3 5
    $ ./calc
    ./calc add x y
    ./calc div x y
    ./calc mult x y

Named parameters correspond to options:

    $ cat
    sub MAIN($source, $target, Bool :$verbose) {
        say "Copying '$source' to '$target'" if $verbose;
        run "cp $source $target";
    $ perl6 calc calc2
    $ perl6  --verbose calc calc2
    Copying 'calc' to 'calc2'

Declaring the parameter as Bool makes it accept no value; without a type constraint of Bool it will take an argument:

    $ cat
    sub MAIN(:$how = 'fast') {
        say "Do nothing, but do it $how";
    $ perl6
    Do nothing, but do it fast
    $ perl6 --how=well
    Do nothing, but do it well
    $ perl6 what?
    Usage: [--how=value-of-how]

In summary, Perl 6 offers you built-in command line parsing and usage messages, just by using subroutine signatures and multi subs.

Writing good, declarative code has never been so easy before.