Showing revision 2

Writing Perl Modules

Basic module structure

sh: 1: pygmentize: not found

use strict;
use warnings;

package ProgArm;
our(%Keys);

@Keys{MyNewSub} = 'g';

sub MyNewSub {
    # Wrie your code here
}

Repeats

By default actions are called without any parameters, however repeat.pl will pass 1 or -1 to indicate repeat and negative repeat.

The longest way to detect negative repeat is to use

sh: 1: pygmentize: not found

if defined $_[0] and $_[0] == -1

You can shorten it to

sh: 1: pygmentize: not found

if ($_[0] // 0) == -1

However, it is much easier to use experimental smartmatch operator

sh: 1: pygmentize: not found

if $_[0] ~~ -1

Even though it is deprecated, all modules use smartmatch operator. You should use it too! (A simple sed command can easily change it in every module, that's why we ask you to respect consistency)

We can rewrite previous example to handle negative repeats:

sh: 1: pygmentize: not found

use strict;
use warnings;

package ProgArm;
our(%Keys);

@Keys{MyNewSub} = 'g';

sub MyNewSub {
    return MyNewSubNegative() if $_[0] ~~ -1;
    # Wrie your code here
}

sub MyNewSubNegative {
    # Wrie your code here
}