Object::InsideOut Cheatsheet

Yanick on 2008-07-20T01:31:14

Everybody is raving about Moose these days, but I must say that I still prefer Object::InsideOut. One of the things I love about OIO is its extensive documentation (70 glorious pages worth of pod!). One of things that I find daunting about it is, well, finding back the attribute I'm looking for as I thumb through those seventy-something blasted pages.

So I made myself a cheatsheet. It's not complete yet, but it's already containing what most mortals would use on a daily basis.

(btw, I've used Scribus to generate the 3-fold. Although I really, really want to like that software, I must say that it makes me miss Quark XPress real bad. If anyone has any suggestion for an alternative Desktop Publishing tool, please feel free to let me know.)


Why?

autarch on 2008-07-20T09:55:41

Just curious, why do you prefer OIO over Moose?

Re:Why?

Yanick on 2008-07-20T15:31:19

As I said, it's merely a preference. There's nothing that I found so far that is a show-stopper or an overwhelming advantage for any of them. This being said...

The biggest plus of OIO, for me, is how inside-out objects ensure that colliding fields are not clobbered, but masked:

package Foo;
use Moose;
has 'x' => ( default => 'foo', is => 'rw' );

package Bar;
use Moose;
extends 'Foo';
has 'x' => ( +default => 'bar' );

package main;
my $obj = Bar->new;
say $obj->x;        # prints 'bar'
say $obj->Foo::x;   # prints 'bar'

package Foo;
use Object::InsideOut;
my @x : Field :Acc('x') :Default('foo');

package Bar;
use Object::InsideOut 'Foo';
my @x :Field :Acc('x') :Default('bar');

package main;
my $obj = Bar->new;
say $obj->x;        # prints 'bar'
say $obj->Foo::x;   # prints 'foo'

I know it's a little bit of a paranoid concern, and it creates a little bit more work (as you have to completly create two '@x's instead of overloading the one from Foo in Bar), but hey, it soothes the obsessive-compulsive in me. :-)

Another argument for OIO is that it claims to produces accessors that are faster than the ones of the usual blessed hash objects. Now, it must be said that I didn't benchmark that claim, so I can't vouch for it. Nor do I really need blinding speed for the stuff that I usually do. But just like the guy shopping for BBQs, I'm atavisticly drawn to the beast that boast the most BTUs. :-)

There is also the documentation. Moose's doc is getting good, but it's still a little bit scattered and hard to put together. OIO's, on the other hand, is very comprehensive. It's also a bit lenghty and hard to use a quick reference, but hopefully my cheatsheet is taking care of that problem.

And OIO can also play quite nicely with non-OIO objects. You can easily inherit of a blessed hash object (which could be a Moose object!), and a lot of the required magic will be done for you.

On the other hand, Moose does offer a couple of features that isn't in OIO (being able to slap 'before' and 'after' pieces of code around your parent class' methods, for example), but so far I didn't feel the need for them.

It does look good

jplindstrom on 2008-07-20T14:32:55

The first impression from the synopsis is that the syntax seems pretty comfortable.

And from what I could tell by browsing the docs it does deal with my biggest problem with inside-out-objects: debuggability.

With the dynamic typing in Perl, if I'm confused as to what I'm looking at it's essential to be able to dump the object data structure easily just to see what's what. I'm assuming (well, hoping) that's covered by the "Object Serialization" blurb.

Re:It does look good

Yanick on 2008-07-20T16:06:16

It is covered. And provided that your class is relatively plain vanilla, the dump() method will do what you want:

package Foo;
use Object::InsideOut;
my @x : Field :Acc('x') :Default('foo');
my @y : Field :Acc('y') :Default('bar');
my @z : Field :Acc('z') :Default('baz');

package main;
my $obj = Foo->new;

use Data::Dumper;
say Dumper $obj->dump;