Skip to content

Moose fairy dust: Now with diagrams!

June 28, 2009

Moritz Lenz mentions in his excellent 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:


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


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

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 ๐Ÿ˜‰

9 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.


  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 ?


  3. June 29, 2009 8:16 am

    Thank you eiro.

    The syntax highlighting comes with free WordPress account. See here for more info:

    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:

    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.


    • 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 ๐Ÿ˜‰


  4. ravenhall permalink
    November 5, 2015 5:25 pm

    Auto-generating class diagrams would really be a useful way to visually show the power of Moose metaprogramming.

  5. May 4, 2023 6:41 pm

    Hi great reading your bloog


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

Leave a Reply to eiro Cancel reply

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

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

Facebook photo

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

Connecting to %s

%d bloggers like this: