Moose fairy dust: Now with diagrams!
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:
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 ๐
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”
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/
this is helpfull and beautifull! can you tell me what are the tools used for highlighting code and drawing diagrams ?
thanks
Thank you eiro.
The syntax highlighting comes with free WordPress account. See here for more info: https://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/
oh … you did it with your hands … i was still dreaming about moose introspection to generate diagrams automatically ๐
thanks for help
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.
http://gist.github.com/138092

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/
Auto-generating class diagrams would really be a useful way to visually show the power of Moose metaprogramming.
Hi great reading your bloog