Moose fairy dust: Now with diagrams!

2009 June 28

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 ;-)

7 Responses leave one →
  1. 2009 June 28

    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”

    • 2009 June 29

      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. 2009 June 29

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

    thanks

  3. 2009 June 29

    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/

    • 2009 June 29
      eiro permalink

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

      thanks for help

Trackbacks & Pingbacks

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

Leave a Reply

Note: You can use basic XHTML in your comments. Your email address will never be published.

Subscribe to this comment feed via RSS