Skip to content

Moose fairy dust: Now with diagrams!

June 28, 2009

Moritz Lenz mentions in his excellent Perlgeek.de blog that there is not enough pictures being used alongside posts in the Perl blogosphere.

Moritz is absolutely correct so I’ll make immediate amends by reproducing the core of my last post about how Moose Roles implemented singleton methods with some helpful diagrams.

So first here is the initial Animal class code again:

use MooseX::Declare;

class Animal {
    method walk { "unknown" }
    method wild { "wild?... I'm bliming livid!" }
}

my $baz = Animal->new;

Here is a diagram which shows the object ($baz) & each class with its methods:

fig1

From this we can see that:

  • $baz is the instantiated object of the Animal class.
  • The Animal class contains all the methods we defined (along with “new” & “meta”) and its superclass is Moose::Object.

NB. Moose::Object is the ultimate superclass which all Moose classes relate back to. In Moose::Object we can see all the base methods.

Next is the DoesHuman role and it being applied to the $baz object:

role DoesHuman {
    method walk { "when not drunk!" }
    method legs { 2 }
}

DoesHuman->meta->apply( $baz );

fig2

And finally this is how the class relationship now looks for $baz object after the above role was applied:
fig3

The anonymous class was created when the DoesHuman role was applied to $baz object and it now stands proudly at the front of the inheritance chain for $baz object.

Thus we have added singleton method(s) to an object by using Moose roles.

This is a clear example of a picture being definitely worth a thousand words ;-)

About these ads
7 Comments leave one →
  1. June 28, 2009 9:35 pm

    Very nice. Your two pictures is probably the best explanation yet of how applying roles to an instance work. Exactly as you said “This is a clear example of a picture being definitely worth a thousand words”

    • June 29, 2009 8:02 am

      Many thanks Stevan.

      I think Dave Thomas’s presentation on Ruby’s object model that I link to in very first post of this “series” deserves most of the credit because it inspired me to see how this all worked in Moose.

      /I3az/

  2. June 29, 2009 7:56 am

    this is helpfull and beautifull! can you tell me what are the tools used for highlighting code and drawing diagrams ?

    thanks

  3. June 29, 2009 8:16 am

    Thank you eiro.

    The syntax highlighting comes with free WordPress account. See here for more info: http://transfixedbutnotdead.com/2008/03/14/doodling-with-moose-intermission/

    Update: FAQ link in above is now dead :( Tut tut WordPress for link breakage! Anyway here is the new support page which replaced that FAQ: http://support.wordpress.com/code/

    NB. I’m hoping at some point the free WordPress account will add gist (Github) syntax highlighting. That one does come with proper perl syntax highlighting!

    For diagrams I used OmniGraffle which runs on the Mac. I’ve used for this for all my diagrams for last five or so years and highly recommend it.

    /I3az/

    • eiro permalink
      June 29, 2009 2:40 pm

      oh … you did it with your hands … i was still dreaming about moose introspection to generate diagrams automatically :)

      thanks for help

      • June 30, 2009 9:44 am

        You could look at AutoDia or UML::Class::Simple on CPAN to do this?

        I’ve tried UML::Class::Simple before… it doesn’t work (yet) with MooseX::Declare but it does work on classic Moose.

        For eg.

        Certainly a good option but for showing how applied roles work and making it pretty then something like OmniGraffle maybe best route. That is until someone produces a MooseX::UML module ;-)

        /I3az/

Trackbacks

  1. Moose Singleton Method: Now without roles! « transfixed but not dead!

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: