<?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; roles</title>
	<atom:link href="http://transfixedbutnotdead.com/tag/roles/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>Tue, 08 May 2012 09:57:48 +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; roles</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>Changing MooseX::SingletonMethod (a little bit!)</title>
		<link>http://transfixedbutnotdead.com/2009/07/31/changing-moosexsingletonmethod-a-little-bit/</link>
		<comments>http://transfixedbutnotdead.com/2009/07/31/changing-moosexsingletonmethod-a-little-bit/#comments</comments>
		<pubDate>Fri, 31 Jul 2009 21:00:23 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[CPAN]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=501</guid>
		<description><![CDATA[It never quite sat comfortably with me that MooseX::SingletonMethod name didn&#8217;t convey the fact it was a Moose role and not a Moose class module. Other MooseX modules on CPAN showed there was no requirement to be succinct in this distinction. Still having posted my last little ditty on having role attached to Moose I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=501&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>It never quite sat comfortably with me that MooseX::SingletonMethod name didn&#8217;t convey the fact it was a Moose role and not a Moose class module.  Other MooseX modules on CPAN showed there was no requirement to be succinct in this distinction. </p>
<p>Still having posted my last little ditty on <a href="http://transfixedbutnotdead.com/2009/07/25/moose-with-role-already-attached/">having role attached to Moose</a> I thought I make some slight amendments to MooseX::SingletonMethod module to pull this slightly annoying thorn from my paw.</p>
<p>Version 0.02 will soon be on CPAN and now works either of these ways:</p>
<p><pre class="brush: perl;">
package Baz;
use MooseX::SingletonMethod;
...
no MooseX::SingletonMethod;
1;
</pre></p>
<p>or as a role:<br />
<pre class="brush: perl;">
package Baz;
use Moose;
with 'MooseX::SingletonMethod::Role';
...
no Moose;
1;
</pre></p>
<p>Hate to change API but MX::SM is only a a few weeks old so I&#8217;m hopeful that the hordes of users that use this module will understand <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/501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/501/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/501/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/501/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/501/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/501/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/501/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/501/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=501&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/07/31/changing-moosexsingletonmethod-a-little-bit/feed/</wfw:commentRss>
		<slash:comments>0</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&#038;blog=351142&#038;post=450&#038;subd=draegtun&#038;ref=&#038;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&#038;blog=351142&#038;post=450&#038;subd=draegtun&#038;ref=&#038;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&#038;blog=351142&#038;post=431&#038;subd=draegtun&#038;ref=&#038;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&#038;blog=351142&#038;post=431&#038;subd=draegtun&#038;ref=&#038;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>Using Moose Roles to create Singleton Methods</title>
		<link>http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/</link>
		<comments>http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/#comments</comments>
		<pubDate>Wed, 03 Jun 2009 14:14:09 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[singleton methods]]></category>
		<category><![CDATA[smalltalk]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=388</guid>
		<description><![CDATA[Randal Schwartz recently posted this bit of Smalltalk code on his Methods and Messages blog p := 4 @ 3. p changeClassTo: (p class copy superclass: p class). p class methodDictionary at: #negated put: (p class methodDictionary at: #transpose). p negated My exposure to Smalltalk is limited to a quick look at Seaside web framework [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=388&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Randal Schwartz recently posted this bit of <a href="http://methodsandmessages.vox.com/library/post/why-i-like-smalltalk-lots-of-reflection.html">Smalltalk code</a> on his <a href="http://methodsandmessages.vox.com/">Methods and Messages blog</a></p>
<p><code>p := 4 @ 3.<br />
p changeClassTo: (p class copy superclass: p class).<br />
p class methodDictionary at: #negated put: (p class methodDictionary at: #transpose).<br />
p negated<br />
</code></p>
<p>My exposure to Smalltalk is limited to a quick look at <a href="http://seaside.st">Seaside web framework</a> which is a bit like learning Rails instead of starting with Ruby <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />   </p>
<p>I can just about grok what this code does.  Thankfully <a href="http://talklikeaduck.denhaven2.com">Rick DeNatale</a> provided a nice <a href="http://talklikeaduck.denhaven2.com/2009/05/30/singleton-methods-in-smalltalk-and-ruby">Ruby conversion</a></p>
<p>Here is Rick&#8217;s subset of Smalltalk&#8217;s Point which replicates what the first Smalltalk line &#8220;p := 4 @ 3.&#8221; does:</p>
<p><pre class="brush: ruby;">
class Point
  attr_accessor :X, :y    # :X should be lowercase but Wordpress keeps converting it to a smiley!

  def initialize(x, y)
    @x, @y = x, y
  end

  def negated
    self.class.new(-x, -y)
  end

  def transpose
    self.class.new(y, x)
  end

  def inspect
    &quot;#{@x} @ #{@y}&quot;
  end
end


p = Point.new(4,3)

p.negated          # =&gt; -4 @ -3
p.transpose        # =&gt; 3 @ 4
</pre></p>
<p>So <em>negated</em> &amp; <em>transpose</em> methods return new Point objects so all very straightforward so far.</p>
<p>In the Smalltalk code the object <em>negated</em> method gets amended to use the <em>transpose</em> method instead.<br />
You can do this in Ruby by opening up the objects method like so:</p>
<p><pre class="brush: ruby;">
def p.negated
  transpose
end

p.negated          # =&gt; 3 @ 4
p.transpose        # =&gt; 3 @ 4
</pre></p>
<p>Nice eh!  The Ruby world refer to this has a Singleton Method.  These are methods which are defined in the object and not the class.   </p>
<p><em>I highly recommend watching this <a href="http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4">excellent video talk</a> by Dave Thomas which provides excellent lucidity to how Ruby objects work.</em></p>
<p>Now how about Perl?   I suspect you can use some MOP meta magic to do same but did you know that <a href="http://search.cpan.org/dist/Moose/lib/Moose/Role.pm">Moose Roles</a> can be applied directly to objects?</p>
<p>Here&#8217;s is the Ruby Point code in Perl using classic Moose.<br />
<pre class="brush: perl;">
{
    package Point;
    use Moose;

    has x =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );
    has y =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );

    sub negated {
        my $self = shift;
        $self-&gt;new( x =&gt; -$self-&gt;x, y =&gt; -$self-&gt;y );
    }

    sub transpose {
        my $self = shift;
        $self-&gt;new( x =&gt; $self-&gt;y, y =&gt; $self-&gt;x );
    }

    sub inspect { say &quot;$_[0]-&gt;{x} \@ $_[0]-&gt;{y}&quot; }
    
    no Moose;
}


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
</pre></p>
<p>So all works same as the Ruby example.   Now if we create a role like so:</p>
<p><pre class="brush: perl;">
{
    package Negated;
    use Moose::Role;
    
    requires 'transpose';
    
    sub negated { 
        my $self = shift;
        $self-&gt;transpose;
    }
    
    no Moose::Role;
}
</pre></p>
<p>You can then &#8220;apply&#8221; it to any object like so:<br />
<pre class="brush: perl;">
Negated-&gt;meta-&gt;apply( $p );
$p-&gt;negated-&gt;inspect;     # =&gt; 3 @ 4
$p-&gt;transpose-&gt;inspect;   # =&gt; 3 @ 4
</pre></p>
<p>Lovely jubbly!   </p>
<p>And as we&#8217;ve gone down this Moose Role route we might as well wrap it all up like this:<br />
<pre class="brush: perl;">
{
    package Point;
    use Moose;

    with qw/DoesNegated DoesTranspose/;
    
    has x =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );
    has y =&gt; ( isa =&gt; 'Int', is =&gt; 'rw' );

    sub inspect { say &quot;$_[0]-&gt;{x} \@ $_[0]-&gt;{y}&quot; }
    
    no Moose;
}

{
    package DoesNegated;
    use Moose::Role;
        
    sub negated {
        my $self = shift;
        $self-&gt;new( x =&gt; -$self-&gt;x, y =&gt; -$self-&gt;y );
    }
    
    no Moose::Role;
}

{
    package DoesTranspose;
    use Moose::Role;
        
    sub transpose {
        my $self = shift;
        $self-&gt;new( x =&gt; $self-&gt;y, y =&gt; $self-&gt;x );
    }
    
    no Moose::Role;
}

my $p = Point-&gt;new( x =&gt; 4, y =&gt; 3 );

DoesTranspose-&gt;meta-&gt;apply( $p, alias =&gt; { transpose =&gt; 'negated' } );

$p-&gt;negated-&gt;inspect;     # =&gt; 3 @ 4
$p-&gt;transpose-&gt;inspect;   # =&gt; 3 @ 4
</pre></p>
<p>This time I used the &#8220;alias&#8221; option to rename the applied method on the Moose hoof <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>And u can keep applying roles to  objects at your hearts content adding or replacing methods<br />
as many times as u like.</p>
<p>/I3az/</p>
<p>Some references:</p>
<ul>
<li><a href="http://search.cpan.org/dist/Moose/lib/Moose/Cookbook/Roles/Recipe3.pod">Moose Cookbook Roles Recipe 3</a>
<li><a href="http://stackoverflow.com/questions/813266/what-to-do-with-an-object-that-should-no-longer-be-used-in-perl/817666#817666">my Stackoverflow answer about applying roles on objects</a>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/388/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/388/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/388/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&#038;blog=351142&#038;post=388&#038;subd=draegtun&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/06/03/using-moose-roles-to-create-singleton-methods/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
<enclosure url="http://scotland-on-rails.s3.amazonaws.com/2A04_DaveThomas-SOR.mp4" length="285514197" type="video/mp4" />
	
		<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>
