<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>transfixed but not dead! &#187; MooseX::Declare</title>
	<atom:link href="http://transfixedbutnotdead.com/tag/moosexdeclare/feed/" rel="self" type="application/rss+xml" />
	<link>http://transfixedbutnotdead.com</link>
	<description>my ramblings on life, work &#38; anything left in-between</description>
	<lastBuildDate>Fri, 03 Feb 2012 16:03:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='transfixedbutnotdead.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/0a317653027efb1ab2bf8adde3dcb067?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>transfixed but not dead! &#187; MooseX::Declare</title>
		<link>http://transfixedbutnotdead.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://transfixedbutnotdead.com/osd.xml" title="transfixed but not dead!" />
	<atom:link rel='hub' href='http://transfixedbutnotdead.com/?pushpress=hub'/>
		<item>
		<title>Anyone for metaprogramming?</title>
		<link>http://transfixedbutnotdead.com/2010/01/13/anyone_for_metaprogramming/</link>
		<comments>http://transfixedbutnotdead.com/2010/01/13/anyone_for_metaprogramming/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 14:41:43 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[MooseX::Declare]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=813</guid>
		<description><![CDATA[What is metaprogramming? Well this question caused a heated discussion on Hacker News yesterday. I personally would describe that metaprogramming provides the ability to: transform programs with programs and leave it at that! The article that stirred the metaprogramming pot was Metaprogramming: Ruby vs. Javascript. This actually is a good post and it helped fill [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=813&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>What is metaprogramming? Well this question caused a <a href="http://news.ycombinator.com/item?id=1047616">heated discussion</a> on <a href="http://news.ycombinator.com/news">Hacker News</a> yesterday.</p>
<p>I personally would describe that <a href="http://en.wikipedia.org/wiki/Metaprogramming">metaprogramming</a> provides the ability to:</p>
<blockquote><p>transform programs with programs</p></blockquote>
<p>and leave it at that!</p>
<p>The article that stirred the metaprogramming pot was <a href="http://fingernailsinoatmeal.com/post/292301859/metaprogramming-ruby-vs-javascript">Metaprogramming: Ruby vs. Javascript</a>. This actually is a good post and it helped fill in a few holes in my &#8220;fragile&#8221; Javascript knowledge.</p>
<p>Luckily I believe my Perl is a bit less fragile!, so I thought I would convert the metaprogramming examples into Perl &amp; <a href="http://moose.perl.org">Moose</a>.</p>
<p>So following the blogs examples, below defines the Ninja class in Moose. <a href="http://search.cpan.org/dist/MooseX-SingletonMethod/">MooseX::SingletonMethod</a> loads Moose with a bit of <a href="http://transfixedbutnotdead.com/2009/07/15/moosexsingletonmethod-now-on-cpan/">singleton method sugar</a>.</p>
<p><pre class="brush: perl;">
use Modern::Perl;

{
    package Ninja;
    use MooseX::SingletonMethod;
    use namespace::clean -except =&gt; 'meta';
    
    has name =&gt; ( is =&gt; 'rw', isa =&gt; 'Str' );
}

my $drew = Ninja-&gt;new( name =&gt; 'Drew' );
my $adam = Ninja-&gt;new( name =&gt; 'Adam' );
</pre><br />
<em>NB. namespace::clean stops the Moose sugar percolating into Ninja class (except meta&#8230; which is the Meta Object Protocol method&#8230; which handy for metaprogramming!).</em></p>
<p>Now we re-open the Ninja class and add <code>battle_cry</code> method like so:<br />
<pre class="brush: perl;">
{
    package Ninja;
    
    sub battle_cry {
        my $self = shift;
        say $self-&gt;name . ' says zing!!!'; 
    }
}

$drew-&gt;battle_cry;   # =&gt; Drew says zing!!!
$adam-&gt;battle_cry;   # =&gt; Adam says zing!!!
</pre></p>
<p>Now using MooseX::SingletonMethod we add <code>throw_star</code> singleton method into $drew object:</p>
<p><pre class="brush: perl;">
$drew-&gt;add_singleton_method( throw_star =&gt; sub {
    say &quot;throwing star&quot;;
});

$drew-&gt;throw_star;   # =&gt; throwing a star
</pre></p>
<p>To call method dynamically we do this:<br />
<pre class="brush: perl;">
$drew-&gt;${ \'battle_cry' };   # =&gt; Drew says zing!!!
</pre></p>
<p>Create <code>colour</code> method closing over $colour_name (ie. closure):<br />
<pre class="brush: perl;">
my $colour_name = 'black';

Ninja-&gt;meta-&gt;add_method( colour =&gt; sub {
    my $self = shift;
    say &quot;${ \$self-&gt;name }'s colour is $colour_name&quot;;
});

$drew-&gt;colour;    # =&gt; Drew's colour is black
$adam-&gt;colour;    # =&gt; Adam's colour is black
</pre></p>
<p>Finally&#8230; <em>&#8220;defining a method dynamically on an instance that closes over local scope and accesses the instance’s state&#8221;</em><br />
<pre class="brush: perl;">
my $sword_symbol = '********';

$drew-&gt;add_singleton_method( swing =&gt; sub {
    my ($self, $sound_effect) = @_;
    say &quot;${ \$self-&gt;name }: $sword_symbol $sound_effect&quot;;
});

$drew-&gt;swing( 'slash!!' );   # =&gt; Drew: ********* slash!!
</pre></p>
<p>Everything nicely works the same as the Ruby &amp; Javascript blog examples.</p>
<p>To round things off, below is the complete example but with <a href="http://search.cpan.org/dist/MooseX-Declare/">MooseX::Declare</a> sugar stirred into the pot.</p>
<p><pre class="brush: perl;">
use Modern::Perl;
use MooseX::Declare;

class Ninja with MooseX::SingletonMethod::Role is mutable {
    has name =&gt; ( is =&gt; 'rw', isa =&gt; 'Str' );
}

my $drew = Ninja-&gt;new( name =&gt; 'Drew' );
my $adam = Ninja-&gt;new( name =&gt; 'Adam' );

###########################################################

class Ninja is mutable {    
    method battle_cry {
        say $self-&gt;name . ' says zing!!!'; 
    }
}

$drew-&gt;battle_cry;   # =&gt; Drew says zing!!!
$adam-&gt;battle_cry;   # =&gt; Adam says zing!!!

###########################################################

$drew-&gt;add_singleton_method( throw_star =&gt; sub {
    say &quot;throwing star&quot;;
});

$drew-&gt;throw_star;   # =&gt; throwing a star

###########################################################

$drew-&gt;${ \'battle_cry' };   # =&gt; Drew says zing!!!

###########################################################

my $colour_name = 'black';

Ninja-&gt;meta-&gt;add_method( colour =&gt; sub {
    my $self = shift;
    say &quot;${ \$self-&gt;name }'s colour is $colour_name&quot;;
});

$drew-&gt;colour;    # =&gt; Drew's colour is black
$adam-&gt;colour;    # =&gt; Adam's colour is black

###########################################################

my $sword_symbol = '********';

$drew-&gt;add_singleton_method( swing =&gt; sub {
    my ($self, $sound_effect) = @_;
    say &quot;${ \$self-&gt;name }: $sword_symbol $sound_effect&quot;;
});

$drew-&gt;swing( 'slash!!' );   # =&gt; Drew: ********* slash!!
</pre></p>
<p>/I3az/</p>
<p>PS.  If you want Moose in Javascript then have a look at <a href="http://code.google.com/p/joose-js/">Joose</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/813/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/813/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/813/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/813/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/813/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/813/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/813/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=813&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2010/01/13/anyone_for_metaprogramming/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
		<item>
		<title>Moose Singleton Method:  Now without roles!</title>
		<link>http://transfixedbutnotdead.com/2009/07/07/moose-singleton-method-now-without-roles/</link>
		<comments>http://transfixedbutnotdead.com/2009/07/07/moose-singleton-method-now-without-roles/#comments</comments>
		<pubDate>Tue, 07 Jul 2009 15:49:38 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[MooseX::Declare]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=470</guid>
		<description><![CDATA[So instead of using roles wouldn&#8217;t it be nice if we could just do the following to an instantiated object: &#160; Well if we define our Animal class like so then we can! This add_singleton_method does all the dirty work that applying a role to an object did in previous post. To break this down: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=470&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So instead of using roles wouldn&#8217;t it be nice if we could just do the following to an instantiated object:</p>
<p><pre class="brush: perl;">
$baz-&gt;add_singleton_method( legs =&gt; sub { 2 } );
</pre></p>
<p>&nbsp;</p>
<p>Well if we define our Animal class like so then we can!</p>
<p><pre class="brush: perl;">
use MooseX::Declare;

class Animal {
    method walk { &quot;unknown&quot; }
    method wild { &quot;wild?... I'm bliming livid!&quot; }
    
    method add_singleton_method( $method_name, $method ) {        
        my $singleton = $self-&gt;meta-&gt;create_anon_class(
            superclasses =&gt; [ $self-&gt;meta-&gt;name ],
            methods      =&gt; { $method_name =&gt; $method },
        );
        $singleton-&gt;add_method( meta =&gt; sub { $singleton } );
        $singleton-&gt;rebless_instance( $self );
    }
}
</pre></p>
<p>This <i>add_singleton_method</i> does all the dirty work that applying a role to an object did in previous <a href="http://transfixedbutnotdead.com/2009/06/28/moose-fairy-dust-now-with-diagrams/">post</a>. </p>
<p>To break this down:</p>
<p><pre class="brush: perl;">
my $singleton = $self-&gt;meta-&gt;create_anon_class(
    superclasses =&gt; [ $self-&gt;meta-&gt;name ],
    methods      =&gt; { $method_name =&gt; $method },
);
</pre></p>
<p>This creates our anonymous class ($singleton).   The &#8220;superclasses&#8221; part makes it a subclass of Animal and the &#8220;methods&#8221; part is where we add predefined methods (which we provide as args in <i>add_singleton_methods</i>)</p>
<p><pre class="brush: perl;">
$singleton-&gt;add_method( meta =&gt; sub { $singleton } );
</pre></p>
<p>Above adds the all important <i>meta</i> method.   Without this then things can get a bit hairy!</p>
<p><pre class="brush: perl;">
$singleton-&gt;rebless_instance( $self );
</pre></p>
<p>This now reblesses the instance with this new anonymous class.</p>
<p>Now we can produce that singleton method like so:</p>
<p><pre class="brush: perl;">
my $baz = Animal-&gt;new;
$baz-&gt;add_singleton_method( legs =&gt; sub { 2 } );
say $baz-&gt;legs;   # =&gt; 2
</pre></p>
<p>And it all works in exactly same way as with role examples previously mentioned.</p>
<p>Now we can keep creating further anonymous subclasses by using <i>add_singleton_method</i> or you can just use Moose meta method to add more methods to current anonymous class:</p>
<p><pre class="brush: perl;">
$baz-&gt;meta-&gt;add_method( walk =&gt; sub { &quot;when not drunk!&quot; } );
</pre></p>
<p>&nbsp;</p>
<p>Now if we tidy up the mechanics of all this into a Moose role with some extra fancy API:</p>
<p><i>NB. Think I&#8217;m safe from the <a href="http://en.wikipedia.org/wiki/Trade_Descriptions_Act_1968">Trade Description Act</a> for my blog title over this role usage!)</i></p>
<p><pre class="brush: perl;">
package DoesSingletonMethod;
use Moose::Role;

our $singleton = sub {
    my $self    = shift;
    my $methods = shift || {};
    
    my $meta = $self-&gt;meta-&gt;create_anon_class(
        superclasses =&gt; [ $self-&gt;meta-&gt;name ],
        methods      =&gt; $methods,
    );
    
    $meta-&gt;add_method( meta =&gt; sub { $meta } );
    $meta-&gt;rebless_instance( $self );
};

sub become_singleton      { $_[0]-&gt;$singleton }

sub add_singleton_method  { $_[0]-&gt;$singleton({ $_[1] =&gt; $_[2] }) }

sub add_singleton_methods { 
    my $self = shift;
    $self-&gt;$singleton({ @_ });
}


no Moose::Role;
1;
</pre></p>
<p>This nows means we can do the following:</p>
<p><pre class="brush: perl;">
use MooseX::Declare;

class Animal with DoesSingletonMethod {
    method walk { &quot;unknown&quot; }
    method wild { &quot;wild?... I'm bliming livid!&quot; }
}

# one way to create singleton method....
my $baz = Animal-&gt;new;
$baz-&gt;become_singleton;                   # make $baz a singleton
$baz-&gt;meta-&gt;add_method( baz =&gt; 'baz!' );  # add method &quot;baz&quot; using meta

# and another.....
my $foo = Animal-&gt;new;
$foo-&gt;add_singleton_method( foo =&gt; sub { 'foo!' } );

# and finally multiple methods....
my $bar = Animal-&gt;new;
$bar-&gt;add_singleton_methods( 
    bar1 =&gt; sub { 'bar1!' }, 
    bar2 =&gt; sub { 'bar2!' },
);
</pre></p>
<p>Hmmm this is nice.  Perhaps I&#8217;ll wrap this up as MooseX::SingletonMethod and upload to CPAN <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/470/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/470/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/470/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=470&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/07/07/moose-singleton-method-now-without-roles/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
		<item>
		<title>Moose fairy dust: Now with diagrams!</title>
		<link>http://transfixedbutnotdead.com/2009/06/28/moose-fairy-dust-now-with-diagrams/</link>
		<comments>http://transfixedbutnotdead.com/2009/06/28/moose-fairy-dust-now-with-diagrams/#comments</comments>
		<pubDate>Sun, 28 Jun 2009 19:24:46 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[MooseX::Declare]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=450</guid>
		<description><![CDATA[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&#8217;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 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=450&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Moritz Lenz mentions in his excellent <a href="http://perlgeek.de/blog-en/misc/am-i-a-stone-man.html">Perlgeek.de blog</a> that there is not enough pictures being used alongside posts in the Perl blogosphere.</p>
<p>Moritz is absolutely correct so I&#8217;ll make immediate amends by reproducing the core of my last post about <a href="http://transfixedbutnotdead.com/2009/06/19/moose-fairy-dust/">how Moose Roles implemented singleton methods</a> with some helpful diagrams.</p>
<p>So first here is the initial Animal class code again:<br />
<pre class="brush: perl;">
use MooseX::Declare;

class Animal {
    method walk { &quot;unknown&quot; }
    method wild { &quot;wild?... I'm bliming livid!&quot; }
}

my $baz = Animal-&gt;new;
</pre></p>
<p>Here is a diagram which shows the object ($baz) &amp; each class with its methods:</p>
<p><a href="http://www.flickr.com/photos/draegtun/3668317009/" title="fig1 by draegtun, on Flickr"><img src="http://farm4.static.flickr.com/3604/3668317009_64060cbd5c_o.jpg" width="379" height="280" alt="fig1" /></a></p>
<p>From this we can see that:</p>
<ul>
<li>$baz is the instantiated object of the Animal class.
<li>The Animal class contains all the methods we defined (along with &#8220;new&#8221; &amp; &#8220;meta&#8221;) and its superclass is Moose::Object.
</ul>
<p>NB.  Moose::Object is the ultimate superclass which all Moose classes relate back to.  In Moose::Object we can see all the base methods.</p>
<p>Next is the DoesHuman role and it being applied to the $baz object:</p>
<p><pre class="brush: perl;">
role DoesHuman {
    method walk { &quot;when not drunk!&quot; }
    method legs { 2 }
}

DoesHuman-&gt;meta-&gt;apply( $baz );
</pre></p>
<p><a href="http://www.flickr.com/photos/draegtun/3668316549/" title="fig2 by draegtun, on Flickr"><img src="http://farm3.static.flickr.com/2452/3668316549_68de35e8a0_o.jpg" width="395" height="126" alt="fig2" /></a></p>
<p>And finally this is how the class relationship now looks for $baz object after the above role was applied:<br />
<a href="http://www.flickr.com/photos/draegtun/3669124144/" title="fig3 by draegtun, on Flickr"><img src="http://farm4.static.flickr.com/3637/3669124144_661a4ab547_o.jpg" width="392" height="393" alt="fig3" /></a></p>
<p>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.</p>
<p>Thus we have added singleton method(s) to an object by using Moose roles.  </p>
<p>This is a clear example of a picture being definitely worth a thousand words  <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/450/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/450/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/450/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=450&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/06/28/moose-fairy-dust-now-with-diagrams/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>

		<media:content url="http://farm4.static.flickr.com/3604/3668317009_64060cbd5c_o.jpg" medium="image">
			<media:title type="html">fig1</media:title>
		</media:content>

		<media:content url="http://farm3.static.flickr.com/2452/3668316549_68de35e8a0_o.jpg" medium="image">
			<media:title type="html">fig2</media:title>
		</media:content>

		<media:content url="http://farm4.static.flickr.com/3637/3669124144_661a4ab547_o.jpg" medium="image">
			<media:title type="html">fig3</media:title>
		</media:content>
	</item>
		<item>
		<title>Moose fairy dust</title>
		<link>http://transfixedbutnotdead.com/2009/06/19/moose-fairy-dust/</link>
		<comments>http://transfixedbutnotdead.com/2009/06/19/moose-fairy-dust/#comments</comments>
		<pubDate>Fri, 19 Jun 2009 19:13:53 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[MooseX::Declare]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=431</guid>
		<description><![CDATA[So following on from my last two posts (1)(2) about Moose Roles and Singleton Methods I thought it would be nice to know how Moose performs all this magic. So to help explain all this I&#8217;ll create a simple (contrived!) example: Now if we introspect this: We will see this: baz is an Animal and [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=431&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So following on from my last two posts (1)(2) about Moose Roles and Singleton Methods I thought it would be nice to know how Moose performs all this magic.</p>
<p>So to help explain all this I&#8217;ll create a simple (contrived!) example:</p>
<p><pre class="brush: perl;">
use MooseX::Declare;

class Animal {
    method walk { &quot;unknown&quot; }
    method wild { &quot;wild?... I'm bliming livid!&quot; }
}
</pre></p>
<p>Now if we introspect this:</p>
<p><pre class="brush: perl;">
my $baz = Animal-&gt;new;
say &quot;baz is an Animal and its &quot; . $baz-&gt;walk . &quot; if he can walk or not?&quot;;
say &quot;baz methods are: &quot;, join &quot; | &quot;, $baz-&gt;meta-&gt;get_method_list;
say &quot;baz ISA: &quot;, join &quot; | &quot;, $baz-&gt;meta-&gt;class_precedence_list;
</pre></p>
<p>We will see this:</p>
<blockquote><p>baz is an Animal and its unknown if he can walk or not?<br />
baz methods are: walk | new | wild | meta<br />
baz ISA: Animal | Moose::Object
</p></blockquote>
<p>Using <i>get_method_list</i> returns methods just in this class (running <i>get_all_method_names</i> would return all resolvable methods via inheritance).</p>
<p>And using <i>class_precedence_list</i> returns the class interitance chain starting with its own class <i>Animal</i>. NB. <i>Moose::Object</i> is the base class for all Moose objects.</p>
<p>So no surprises here.  So what happens when I sprinkle some fairy dust on $baz by applying a role to this object (and creating a Singleton Method):</p>
<p><pre class="brush: perl;">
role DoesHuman {
    method walk { &quot;when not drunk!&quot; }
    method legs { 2 }
}

DoesHuman-&gt;meta-&gt;apply( $baz );

say &quot;baz is now Human with &quot; . $baz-&gt;legs . &quot; legs and walks &quot; . $baz-&gt;walk;
say &quot;baz methods are: &quot;, join &quot; | &quot;, $baz-&gt;meta-&gt;get_method_list;
say &quot;baz ISA: &quot;, join &quot; | &quot;, $baz-&gt;meta-&gt;class_precedence_list
say &quot;baz says: &quot;, $baz-&gt;wild;
</pre></p>
<p>This produces:</p>
<blockquote><p>baz is now Human with 2 legs and walks when not drunk!<br />
baz methods are: legs | walk | meta<br />
baz ISA: Class::MOP::Class::__ANON__::SERIAL::2 | Animal | Moose::Object<br />
baz says: wild?&#8230; I&#8217;m bliming livid!
</p></blockquote>
<p>First lets look at the object methods. <i>legs</i> has been added and <i>walk</i> has also nicely been replaced.  So this does exactly what I asked it to do.</p>
<p>But hold on&#8230; where has <i>wild</i> gone from the method list?   And yet when I do $self-&gt;wild it works fine??</p>
<p>Well if you followed Dave Thomas video link I gave in first post then this is easily explained (even though he&#8217;s talking about Ruby!).  </p>
<p>If you look at the class inheritance you can see what happened. Here is my attempt to explain whats going on: </p>
<p>Applying <i>DoesHuman</i> role to $baz object has created new anonymous class called (in this instance) &#8220;Class::MOP::Class::__ANON__::SERIAL::2&#8243; and puts the <i>DoesHuman</i> role methods <i>legs</i> and <i>walk</i> into there and then makes this class a child of <i>Animal</i>.  $baz is then assigned (blessed) to this new anon class.</p>
<p>Or has Dave Thomas says:</p>
<blockquote><p>move one to the right and then up one</p></blockquote>
<p>But that only makes sense if you use same diagrams he had!</p>
<p>All very neat!  In a nutshell its created a unique anonymous class with the role methods which $baz uses instead of the </i>Animal</i> class.</p>
<p>And you can keep adding to this anon class chaining:</p>
<p><pre class="brush: perl;">
role DoesMale {
    method sex  { &quot;male&quot; }
    method wild { &quot;nice to meet u!&quot; }
}

DoesMale-&gt;meta-&gt;apply( $baz );

say &quot;baz is now a &quot; . $baz-&gt;sex;
say &quot;baz methods are: &quot;, join &quot; | &quot;, $baz-&gt;meta-&gt;get_method_list;
say &quot;baz ISA: &quot;, join &quot; | &quot;, $baz-&gt;meta-&gt;class_precedence_list;
say &quot;baz says: &quot;, $baz-&gt;wild;
</pre></p>
<blockquote><p>baz is now a male<br />
baz methods are: sex | wild | meta<br />
baz ISA: Class::MOP::Class::__ANON__::SERIAL::3 | Class::MOP::Class::__ANON__::SERIAL::2 | Animal | Moose::Object<br />
baz says: nice to meet u!
</p></blockquote>
<p>/I3az/</p>
<p>Some references:</p>
<ol>
<li><a href="http://transfixedbutnotdead.com/2009/06/10/roles-singleton-methods-moosexdeclare/">Roles, Singleton Methods &amp; MooseX::Declare</a>
<li><a href="http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/">Using Moose Roles to create Singleton Methods</a>
<li><a href="http://search.cpan.org/dist/Moose/lib/Moose/Manual/MOP.pod">Moose::Manual::MOP</a>
<li><a href="http://search.cpan.org/dist/Class-MOP/lib/Class/MOP/Class.pm">CLass::MOP::Class</a> or <a href="http://kobesearch.cpan.org/htdocs/Class-MOP/Class/MOP/Class.html">(kobesearch)</a>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/431/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/431/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/431/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=431&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/06/19/moose-fairy-dust/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
		<item>
		<title>Roles, Singleton Methods &amp; MooseX::Declare</title>
		<link>http://transfixedbutnotdead.com/2009/06/10/roles-singleton-methods-moosexdeclare/</link>
		<comments>http://transfixedbutnotdead.com/2009/06/10/roles-singleton-methods-moosexdeclare/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 19:55:29 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[MooseX::Declare]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=405</guid>
		<description><![CDATA[If you feel that the &#8220;classic&#8221; Moose code in my last post seems a bit chunky when compared to the Ruby example then have a look at MooseX::Declare MooseX::Declare cuts out the following boilerplate code from my previous Point example: Down to just this: Here is the first Point code &#38; usage example using Moosex::Declare: [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=405&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you feel that the &#8220;classic&#8221; Moose code in my <a href="http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/">last post</a> seems a bit chunky when compared to the Ruby example then have a look at <a href="http://search.cpan.org/dist/MooseX-Declare/">MooseX::Declare</a></p>
<p>MooseX::Declare cuts out the following boilerplate code from my previous Point example:<br />
<pre class="brush: perl;">
    {
        package Point;
        use Moose;
        ...
        no Moose;
        __PACKAGE__-&gt;meta-&gt;make_immutable;
    }
</pre></p>
<p>Down to just this:<br />
<pre class="brush: perl;">
use MooseX::Declare;
class Point {
    ...
}
</pre></p>
<p>Here is the first Point code &amp; usage example using Moosex::Declare:<br />
<pre class="brush: perl;">
use MooseX::Declare;
 
class Point {    
    has x =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );
    has y =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );
 
    method negated   { $self-&gt;new( x =&gt; -$self-&gt;x, y =&gt; -$self-&gt;y ) }
 
    method transpose { $self-&gt;new( x =&gt; $self-&gt;y,  y =&gt; $self-&gt;x  ) }
 
    method inspect   { say $self-&gt;x . ' @ ' . $self-&gt;y              }
}
 
my $p = Point-&gt;new( x =&gt; 4, y =&gt; 3 );
 
$p-&gt;negated-&gt;inspect;      # =&gt; -4 @ -3
$p-&gt;transpose-&gt;inspect;    # =&gt; 3 @ 4
 
role Negated {
    requires 'transpose';
    method negated { $self-&gt;transpose };
}
 
Negated-&gt;meta-&gt;apply( $p );
$p-&gt;negated-&gt;inspect;      # =&gt; 3 @ 4
$p-&gt;transpose-&gt;inspect;    # =&gt; 3 @ 4
</pre><br />
<a href="http://gist.github.com/126839">gist: 126839</a></p>
<p>Nice eh!   Here is the second example of code transformed by MooseX::Declare:</p>
<p><pre class="brush: perl;">
use MooseX::Declare;

role DoesNegated {
    method negated { $self-&gt;new( x =&gt; -$self-&gt;x, y =&gt; -$self-&gt;y ) }
}

role DoesTranspose {
    method transpose { $self-&gt;new( x =&gt; $self-&gt;y, y =&gt; $self-&gt;x ) }
}

class Point with DoesNegated with DoesTranspose {    
    has x =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );
    has y =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );

    method inspect { say $self-&gt;x . ' @ ' . $self-&gt;y }
}
</pre><br />
<a href="http://gist.github.com/126849">gist: 126849</a></p>
<p>Then like in previous post to apply a role to object is just:<br />
<pre class="brush: perl;">
DoesTranspose-&gt;meta-&gt;apply( $p, alias =&gt; { transpose =&gt; 'negated' } );
</pre></p>
<p>Now if that doesn&#8217;t make you dribble then I don&#8217;t know what will <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>/I3az/</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/405/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/405/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/405/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=405&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/06/10/roles-singleton-methods-moosexdeclare/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/29cb106071d163d703484e63839d89cb?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">draegtun</media:title>
		</media:content>
	</item>
	</channel>
</rss>
