<?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; singleton methods</title>
	<atom:link href="http://transfixedbutnotdead.com/tag/singleton-methods/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; singleton methods</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>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&amp;blog=351142&amp;post=501&amp;subd=draegtun&amp;ref=&amp;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&amp;blog=351142&amp;post=501&amp;subd=draegtun&amp;ref=&amp;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 with role already attached</title>
		<link>http://transfixedbutnotdead.com/2009/07/25/moose-with-role-already-attached/</link>
		<comments>http://transfixedbutnotdead.com/2009/07/25/moose-with-role-already-attached/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 13:30:44 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=493</guid>
		<description><![CDATA[OK so adding Singleton Method to you class is nice and easy with: &#160; But wouldn&#8217;t it be even nicer if you could replace the two lines with something like: &#160; Well you can by applying the role to the base object (Moose::Object) like so: &#160; And then we can create Moose class and use [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=493&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>OK so adding Singleton Method to you class is nice and easy with:</p>
<p><pre class="brush: perl;">
use Moose;
with 'MooseX::SingletonMethod';
</pre></p>
<p>&nbsp;</p>
<p>But wouldn&#8217;t it be even nicer if you could replace the two lines with something like:</p>
<p><pre class="brush: perl;">
use MyMoose;
</pre></p>
<p>&nbsp;</p>
<p>Well you can by applying the role to the base object (Moose::Object) like so:</p>
<p><pre class="brush: perl;">
package MyMoose;

use Moose ();
use Moose::Exporter;
use Moose::Util::MetaRole;

Moose::Exporter-&gt;setup_import_methods( also =&gt; 'Moose' );

sub init_meta {
    shift;
    my %options = @_;

    my $meta = Moose-&gt;init_meta( %options );

    Moose::Util::MetaRole::apply_base_class_roles(
        for_class =&gt; $options{ for_class },
        roles     =&gt; [ 'MooseX::SingletonMethod' ],  # &lt;= my roles
    );

    return $meta;
}

1;
</pre></p>
<p>&nbsp;</p>
<p>And then we can create Moose class and use it like so:</p>
<p><pre class="brush: perl;">
package Baz;
use MyMoose;
has 'name' =&gt; ( is =&gt; 'rw', isa =&gt; 'Str' );


package main;

my $baz = Baz-&gt;new( name =&gt; 'baz' );

$baz-&gt;add_singleton_method( hello =&gt; sub { 'hello ' . $_[0]-&gt;name } ); 

say $baz-&gt;hello;   # =&gt; 'hello baz'
</pre></p>
<p>&nbsp;</p>
<p>Of course you don&#8217;t need to stop with just adding MooseX::SingletonMethod role only&#8230;  add all your favourite roles in there!</p>
<p>For more info see <a href="http://search.cpan.org/dist/Moose/lib/Moose/Cookbook/Extending/Recipe2.pod">Moose::Cookbook::Extending::Recipe2</a></p>
<p>/I3az/</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/493/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/493/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/493/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=493&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/07/25/moose-with-role-already-attached/feed/</wfw:commentRss>
		<slash:comments>3</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>MooseX::SingletonMethod now on CPAN</title>
		<link>http://transfixedbutnotdead.com/2009/07/15/moosexsingletonmethod-now-on-cpan/</link>
		<comments>http://transfixedbutnotdead.com/2009/07/15/moosexsingletonmethod-now-on-cpan/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 18:09:28 +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[singleton methods]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=488</guid>
		<description><![CDATA[So has promised or was it threatened I&#8217;ve packaged and uploaded MooseX::SingletonMethod to CPAN. Here is the synopsis from the pod: MooseX is the namespace for adding extensions around Moose. Its the playground of future Moose features where modules get torn, beaten and bruised in a development rite of passage. It would one day be [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=488&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>So has promised or was it <a href="http://transfixedbutnotdead.com/2009/07/07/moose-singleton-method-now-without-roles/">threatened</a> I&#8217;ve packaged and uploaded <a href="http://search.cpan.org/dist/MooseX-SingletonMethod/">MooseX::SingletonMethod</a> to CPAN.</p>
<p>Here is the synopsis from the pod:</p>
<p><pre class="brush: perl;">
package Baz;
use Moose;
with 'MooseX::SingletonMethod::Role';
no Moose;

package main;
my $baz = Baz-&gt;new;
my $foo = Baz-&gt;new;

# add singleton method called &quot;baz&quot; just to $baz and not to Baz class
$baz-&gt;add_singleton_method( baz =&gt; sub { 'baz!' } ); 

say $baz-&gt;baz;   # =&gt; 'baz!'
say $foo-&gt;baz;   # ERROR: Can't locate object method &quot;baz&quot;....
</pre></p>
<p>MooseX is the namespace for adding extensions around Moose.  Its the playground of future Moose features where modules get torn, beaten and bruised in a development rite of passage.   </p>
<p>It would one day be nice to see MooseX::SingletonMethod features grow up and become part of standard Moose family <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>MooseX::SingletonMethod can also be found in these exotic areas of the web:</p>
<ul>
<li><a href="http://github.com/draegtun/MooseX-SingletonMethod/tree/master">on GitHub</a>
<li><a href="https://www.ohloh.net/p/MooseX-SingletonMethod">on Ohloh</a>
<li><a href="http://freshmeat.net/projects/moosexsingletonmethod"> and on FreshMeat</a>
</ul>
<p>/I3az/</p>
<p>&nbsp;</p>
<p>PS. While sticking my flag into the MooseX::SingletonMethod namespace I noticed the following module by <a href="http://search.cpan.org/~simon/">Simon Cozens</a>:</p>
<ul>
<li><a href="http://search.cpan.org/dist/Class-SingletonMethod/">Class-SingletonMethod</a>
</ul>
<p>I&#8217;m amazed this as passed me by especially has I first came to understand Singleton Methods from Simon&#8217;s excellent &#8220;Advanced Perl Programming&#8221; book.   Just shows you how deep and diverse the CPAN universe is!</p>
<p>Update: Code now uses MX::SM::Role</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/488/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/488/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/488/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=488&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/07/15/moosexsingletonmethod-now-on-cpan/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>
		<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&amp;blog=351142&amp;post=388&amp;subd=draegtun&amp;ref=&amp;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&amp;blog=351142&amp;post=388&amp;subd=draegtun&amp;ref=&amp;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>
