Log in

No account? Create an account
Studying Moose Top-Down - Shlomif's Technical Posts Community — LiveJournal [entries|archive|friends|userinfo]
Shlomif's Technical Posts Community

[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

[Links:| Shlomi Fish's Homepage Main Journal Homesite Blog Planet Linux-IL Amir Aharoni in Unicode open dot dot dot ]

Studying Moose Top-Down [Dec. 7th, 2009|10:41 pm]
Shlomif's Technical Posts Community


[Tags|, , , , , , , , ]
[Current Location |Home]
[Current Music |ds_akt.xm - A Knight's Tale (from modarchive.com)]

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;


    return $self;

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

    # Initialise from $args.


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.