Shlomi Fish (shlomif) wrote in shlomif_tech,
Shlomi Fish
shlomif
shlomif_tech

  • Location:
  • Music:

Studying Moose Top-Down

This is a post about Moose the post-modern Object-Oriented Programming system for Perl 5. Until a few days ago, I've learnt most of what I knew about Moose by reading some presentations about it and experimenting. However, a few days ago, I've decided that maybe there was a more-Moosey way of doing the following pattern that I've been doing in my classes:

# Super-base class.
sub new
{
    my $class = shift;

    my $self = {};
    bless $self, $class;

    $self->_init(@_);

    return $self;
}

# In derived classes:
sub _init
{
    my $self = shift;
    my $args = shift;

    $self->_init($args);
    # Initialise from $args.

    return;
}

Turns out there was - you can use the BUILD method to provide initialisation for each part of the class hierarchy (it gets calls in a walk-method style - not only for the most qualified sub-class). Only in my case in my en-Moosification of XML-Grammar-Fiction, I only needed a 'default' callback to the class members, and to make them 'lazy' by default.

This made me conclude that I have to learn Moose top-down to become more familiar with it. So I started reading the chapters of Moose::Manual and have already learned that I could encapsulate push() and shift() out of an array ref using its delegation, which also proved useful in XML-Grammar-Fiction. After I'm done with the manual, I'm planning to go over the Moose cookbook.

Hopefully, this will give me a lot of ways to avoid repetitive code in my CPAN modules. Of course, now, I'm facing the classic dilemma of whether to make something a base class or a role… decisions, decisions.

Tags: cpan, fiction, grammar, manual, moose, object oriented prorgamming, oop, perl, xml
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

  • 0 comments