Easier coding with MooseX::Method::Signatures

[ Perl tips index ]
[ Subscribe to Perl tips ]

If you haven't heard about Moose for object-oriented programming in Perl, then you're really missing something special. But even if you're never touched Moose before, you can still be excited about the syntax which is now available using MooseX::Method::Signatures.

Getting started

We're going to assume our class starts with a small preamble. In this case, we're creating a Shark class that extends the Fish class. Here we're loading MooseX::Method::Signatures, but not yet using its features. The extends keyword comes from vanilla Moose:

    use v5.10.0;

    package Shark;

    use Moose;
    use MooseX::Method::Signatures;

    extends 'Fish';

In its most basic form, MooseX::Method::Signatures (MMS) provides a cleaner syntax for writing method calls, as well as providing a ready-to-use $self variable:

    method hello {
        say "RAWR! I'm a shark!";
        say 'My name is ', $self->name;
    }

However the real beauty of MMS is that it provides very powerful method signatures:

    method hello($person) {
        say "RAWR! I'm a shark!";
        say 'My name is ', $self->name;
        say "Hello, $person!";
    }

By using MMS, we avoid the hassles of having to directly manipulate @_. Working with multiple parameters is as easy as you'd expect:

    method swim($speed, $directon) {
        ...
    }

By default, these parameters are positional:

    my $shark = Shark->new;
    $shark->swim( $fast, $north );

However by including a colon before variables, we can make them named:

    method swim(:$speed, :$direction) { ...  }

    $shark->swim( speed => fast, direction => $north );

It's also possible to have optional parameters, which are indicated with a question-mark. Here we're allowing optional $roll, $pitch and $yaw parameters:

    method swim(:$speed, :$direction, :$roll?, :$pitch?, :$yaw?) { ... }

    $shark->swim(
        speed     => fast,
        direction => $north,
        pitch     => $upwards
    );

Of course, it's also really useful to have defaults. Providing a default also implies that parameter is optional:

    method swim( :$speed, :$direction, :$roll = 0, :$pitch = 0, :$yaw = 0) { ... }

We can even have type checking, and extend method signatures over multiple lines:

    method swim(
        Speed     :$speed,
        Direction :$direction,
        Angle     :$roll  = 0,
        Angle     :$pitch = 0,
        Angle     :$yaw   = 0
    ) { ... }

The type checking in MooseX::Method::Signatures is the same as type-checking performed by Moose attributes;

Even more syntactic sugar

If MooseX::Method::Signatures wasn't sweet enough for you, then MooseX::Declare builds on MooseX::Method::Signatures to do everything we've shown in this perl-tip, and more!

    use MooseX::Declare;

    class Laser::Shark extends Shark with Lasers {
        method annihilate (Mortal $target) {
            $self->laser->aim($target)->fire;
        }
    }

More information

[ Perl tips index ]
[ Subscribe to Perl tips ]


This Perl tip and associated text is copyright Perl Training Australia. You may freely distribute this text so long as it is distributed in full with this Copyright noticed attached.

If you have any questions please don't hesitate to contact us:

Email: contact@perltraining.com.au
Phone: 03 9354 6001 (Australia)
International: +61 3 9354 6001

Valid XHTML 1.0 Valid CSS