jump to navigation

Object Oriented Perl? July 12, 2008

Posted by claudio in Uncategorized.
Tags: , ,

I love perl. Really. This is not a post inspired by “5 things I hate about Perl“, but rather a question for the wider perl community out there (hi perlshere!). How do you write your OO perl code?

I have some code out there that follows the inside-out objects methodology of Perl Best Practices. Lately I have been writing java code, and inside-out objects feel like a lot of extra – not very intuitive – work to create your classes. I have been looking at Moose, but again it looks pretty cumbersome to create classes (constructors, named parameters for methods, returns, etc.). I am just probably missing a good howto (besides the cookbooks on cpan) or even a printed book on Moose. Any tips out there to get into Moose fast or a nice complete and intuitive alternative (extra points if it’s similar to java’s OO framework)? Thanks.

EDIT (Feb. 16th 2009): it seems I have a direct line with The Perl Foundation: a grant was approved for Dave Rolsky to work on the Moose documentation. Yannick greated a nice pdf output of the manual (see this as well).


1. Shlomi Fish - July 16, 2008


I saw this post of yours in Perlsphere.net. You can look at my OO code in some of my CPAN modules. Basically, I tend to use either Class::Accessor (which gives very little) or Moose (where I tend to barely scratch the surface of its power, and tends to be kinda slow with my particular style). To do method calls I do something like:

sub mymethod
my ($self, $args) = @_;
# $args is a hash.


sub mymethod
my ($self, $first_name, $last_name, $address …. ) = @_;


It’s a little more code than OO languages with a more convenient syntax, but it’s grown on me.

Oh well – fixed in Perl Six.

Regards, — Shlomi Fish.

2. Chris Prather - November 8, 2008

“I have been looking at Moose, but again it looks pretty cumbersome to create classes (constructors, named parameters for methods, returns, etc.).”

I’m not sure what you mean by this, Moose provides a default constructor (new inherited from Moose::Object), and doesn’t named parameters for methods / returns unless you go out of your way to use a non-core extension package.

A simple Moose class is:

package Class;
use Moose;

has foo => ( is => ‘ro’ ); # read only foo


That’s it. Everything works as you’d expect from writing OO in Perl. This was one of the reasons I started working with Moose … it worked exactly as I would have expected without me having to do all the work. Granted I’m biased, I declared 2008 the Year of the Moose.

3. claudio - November 8, 2008

Maybe you can enlighten me how to do things like this:

This is a pretty classic java class constructor:

public ClassName (String name, int number) {
this.name = name;
this.number = number;

So in short, what I want to do is assign some class variables (used in methods later) and some initialization code. I played a little with the BUILD method, but wasn’t really successful. Besides, the all capitals name suggest a rather non-standard feature. So far, Moose has been a treat compared to all the work needed when using Inside-Outside Objects constructions (hashes), but I am still lacking some understanding.

4. Delon Newman - November 11, 2008


package ClassName;
has 'name' => ( is => 'rw', isa => 'Str' );
has 'number' => ( is => 'rw', isa => 'Int' );

sub BUILD {
my $self = shift;

package main;
$obj = ClassName->new(name => "MyName", number => 3.14159);
if ($obj->name == "MyName") {
# true

5. claudio - November 11, 2008


I have some projects coming up where I really need an OO style. I will use Moose on those to see how it works out. If Moose really delivers what it promises it should be included in Perl 5. There is nothing wrong with perls TIMTOWTDI – I like it – but there should be at least one good way to do OO programming that is thought on Learning Perl books, PBP and the like.

Every book/tutorial I have ever read has a different OO methodology and besides Moose, they all were *very* counter-intuitive. It’s a long wait for Perl 6, so an “official” full blown OO framework in the meanwhile would bee really nice. There is always “bless” for the minimalists.

Anyway, thanks for your time.

6. Dave Rolsky - February 3, 2009

Just an FYI for anyone who comes along later. That example in comment #4 is kind of off. The BUILD method is completely unnecessary. Moose automatically populates your object with the values passed to the constructor.

BUILD is for doing _other_ things like checking object state. Most classes will not need a BUILD method.

7. Delon Newman - July 25, 2009

Dave is right, of course, not sure in hind sight why I included the “BUILD” method (maybe it had something to do with the “doSomeInitilizingCode()” in claudios example), and obviously 3.14159 isn’t an “Int” either.😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Get every new post delivered to your Inbox.

Join 197 other followers

%d bloggers like this: