<?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; metaprogramming</title>
	<atom:link href="http://transfixedbutnotdead.com/tag/metaprogramming/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; metaprogramming</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>Perl6 metaprogramming update</title>
		<link>http://transfixedbutnotdead.com/2010/10/31/perl6-metaprogramming-update/</link>
		<comments>http://transfixedbutnotdead.com/2010/10/31/perl6-metaprogramming-update/#comments</comments>
		<pubDate>Sun, 31 Oct 2010 20:32:32 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl6]]></category>
		<category><![CDATA[rakudo]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=1103</guid>
		<description><![CDATA[This post is an update to my Anyone for Perl 6 metaprogramming? article and gist using Rakudo Star (October 2010 release). Now I expected one change to the code and one optional improvement that I could now make. However along with these I got two extra enforced changes. Re-opening a class First the expected change [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=1103&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>This post is an update to my <a href="http://transfixedbutnotdead.com/2010/01/14/anyone-for-perl-6-metaprogramming/">Anyone for Perl 6 metaprogramming?</a> article and <a href="http://gist.github.com/276591">gist</a> using <a href="http://rakudo.org/">Rakudo Star</a> (October 2010 release).</p>
<p>Now I expected one change to the code and one optional improvement that I could now make.  However along with these I got two extra enforced changes.</p>
<h1>Re-opening a class</h1>
<p>First the expected change to re-opening a class:<br />
<pre class="brush: perl;">
class Ninja is also { ... }

# is now...

augment class Ninja { ... }
</pre></p>
<p>However for this to work I needed to add the following:<br />
<pre class="brush: perl;">
use MONKEY_TYPING;
</pre></p>
<p>A pragmatic addition.  If you&#8217;re gonna shoot yourself in the foot then at least you have to prepare for it in advance <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>NB. Rakudo nicely gave me a fatal warning when I tried to re-open the class and politely told me that <code>use MONKEY_TYPING</code> was needed it I wanted to <code>augment class Ninja</code>.</p>
<h1>Applying a role to an object (instance)</h1>
<p>Now the next change was optional but it was a more succinct way to add a role to an object.  Previously I had to create a role and then apply it to object:<br />
<pre class="brush: perl;">
role ThrowStar {
  method throw_star { say &quot;throwing star&quot; }
}

$drew does ThrowStar;
</pre></p>
<p>Now with Rakudo Star I can apply an anonymous role directly:<br />
<pre class="brush: perl;">
$drew does role {
  method throw_star { say &quot;throwing star&quot; }
};
</pre></p>
<h1>Something I didn&#8217;t expect</h1>
<p>The final change to the code was something unexpected.  I got the following error:<br />
<code><br />
===SORRY!===<br />
Quoted method name requires parenthesized arguments at line 50, near ";     # =&gt;"<br />
</code></p>
<p>This was tied to this line of code:<br />
<pre class="brush: perl;">
$drew.'battle_cry';     # =&gt; Drew says zing!!!
</pre></p>
<p>I am not sure if this is a change to Perl6 spec but it now requires extra parenthesis:<br />
<pre class="brush: perl;">
$drew.'battle_cry'();     # =&gt; Drew says zing!!!
</pre></p>
<h1>&#8230; and finally</h1>
<p>My original post from beginning of the year was me just kicking the Rakudo tyres.  I&#8217;m now looking forward to taking it out for a proper test drive around the block <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   </p>
<p>Below is the complete revised code:</p>
<p><pre class="brush: perl;">
use v6;
use MONKEY_TYPING;

class Ninja {
    has Str $.name is rw;
}

my Ninja $drew .= new( name =&gt; 'Drew' );
my Ninja $adam .= new( name =&gt; 'Adam' );


###########################################################
# Reopen Ninja class (&quot;is also&quot; does the biz) 
# and add 'battle_cry' method

augment class Ninja {
    method battle_cry {
        say $.name ~ ' says zing!!!'; 
    }
}

$drew.battle_cry;   # =&gt; Drew says zing!!!
$adam.battle_cry;   # =&gt; Adam says zing!!!


###########################################################
# add 'throw_star' method to $drew object by 
# applying (&quot;does&quot;) role to it (Singleton method)

$drew does role {
    method throw_star { say &quot;throwing star&quot; }
};

$drew.throw_star;     # =&gt; throwing a star


###########################################################
# call method dynamically

$drew.'battle_cry'();     # =&gt; Drew says zing!!!


###########################################################
# add &quot;colour&quot; method closing over $colour_name (ie. closure):

my $colour_name = 'black';

augment class Ninja {
    method colour { say &quot;{$.name}'s colour is $colour_name&quot; }
}

$drew.colour;    # =&gt; Drew's colour is black
$adam.colour;    # =&gt; Adam's colour is black
 
 
###########################################################

# &quot;defining a method dynamically on an instance that closes 
# over local scope and accesses the instance’s state&quot;
#
# Opps - Class method slipped in while working it out.
# $drew.^add_method() does a singleton method.. nice!

my $sword_symbol = '********';

$drew.^add_method( 'swing', method ( Str $sound_effect ) { 
    say &quot;$.name: $sword_symbol $sound_effect&quot;;
} );

$drew.swing( 'slash!!' );
</pre></p>
<p>Happy halloween!</p>
<p>/I3az/</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/1103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/1103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/1103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/1103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/1103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/1103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/1103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/1103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/1103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/1103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/1103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/1103/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/1103/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/1103/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=1103&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2010/10/31/perl6-metaprogramming-update/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>Anyone for Perl 6 metaprogramming?</title>
		<link>http://transfixedbutnotdead.com/2010/01/14/anyone-for-perl-6-metaprogramming/</link>
		<comments>http://transfixedbutnotdead.com/2010/01/14/anyone-for-perl-6-metaprogramming/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 22:11:36 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[metaprogramming]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[perl6]]></category>
		<category><![CDATA[rakudo]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=823</guid>
		<description><![CDATA[My last post about the Metaprogramming: Ruby vs. Javascript blog post stirred a little thought in my head: How would it look in Perl 6? Well here goes, the complete example written in Perl 6 which runs on Rakudo, an implementation of the Perl 6 spec on the Parrot VM: Of all the examples (in [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=823&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>My <a href="http://transfixedbutnotdead.com/2010/01/13/anyone_for_metaprogramming/">last post</a> about the <a href="http://fingernailsinoatmeal.com/post/292301859/metaprogramming-ruby-vs-javascript">Metaprogramming: Ruby vs. Javascript</a> blog post stirred a little thought in my head:</p>
<blockquote><p>How would it look in Perl 6?</p></blockquote>
<p>Well here goes, the complete example written in <a href="http://en.wikipedia.org/wiki/Perl_6">Perl 6</a> which runs on <a href="http://www.rakudo.org/">Rakudo</a>, an implementation of the Perl 6 spec on the <a href="http://en.wikipedia.org/wiki/Parrot_virtual_machine">Parrot VM</a>:</p>
<p><pre class="brush: perl;">
use v6;

class Ninja {
    has Str $.name is rw;
}

my Ninja $drew .= new( name =&gt; 'Drew' );
my Ninja $adam .= new( name =&gt; 'Adam' );


###########################################################
# Reopen Ninja class (&quot;is also&quot; does the biz) 
# and add 'battle_cry' method

class Ninja is also {    
    method battle_cry {
        say $.name ~ ' says zing!!!'; 
    }
}

$drew.battle_cry;   # =&gt; Drew says zing!!!
$adam.battle_cry;   # =&gt; Adam says zing!!!


###########################################################
# add 'throw_star' method to $drew object by creating
# and applying (&quot;does&quot;) role to it (Singleton method)

role ThrowStar {
    method throw_star { say &quot;throwing star&quot; }
}

$drew does ThrowStar;
$drew.throw_star;     # =&gt; throwing a star


###########################################################
# call method dynamically: $obj.'method_name' or $obj.$method

$drew.'battle_cry';     # =&gt; Drew says zing!!!


###########################################################
# add &quot;colour&quot; method closing over $colour_name (ie. closure):

my $colour_name = 'black';

class Ninja is also {
    method colour { say &quot;{$.name}'s colour is {$colour_name} &quot; }
}

$drew.colour;    # =&gt; Drew's colour is black
$adam.colour;    # =&gt; Adam's colour is black


###########################################################

# &quot;defining a method dynamically on an instance that closes 
# over local scope and accesses the instance’s state&quot;

my $sword_symbol = '********';

$drew.^add_method( 'swing', method ( Str $sound_effect ) { 
    say &quot;{$.name}: {$sword_symbol} {$sound_effect}&quot;;
});

$drew.swing( 'slash!!' );    # =&gt; Drew: ********* slash!!
</pre></p>
<p>Of all the examples (in Perl 5 / Moose, Ruby, Javascript &amp; <a href="http://codeblog.dhananjaynene.com/2010/01/dynamically-adding-methods-with-metaprogramming-ruby-and-python/">Python</a>) I think this one is the most clean and intuitive.  Perl 6 has a good future if it keeps this up!</p>
<p>And it all went surprising smoothly.  There were a couple of bumps in my Perl 6 road but these are all connected to the flux between the Perl 6 spec, Rakudo and knowing what info you find on the web is still accurate (or not). </p>
<p>First bump was how to re-open a class.  Most Perl 6 documentation implied that classes were always open (unless <code>is final</code> was used).  This no longer seemed to be the case and it gave examples of <code>class A is augmented { ... }</code>.  However this didn&#8217;t work in Rakudo but eventually I came across <code>is also</code> which did.</p>
<p>I like the look of <code>class A is also { ... }</code>.  However <a href="http://perlgeek.de/en/">Moritz</a> on IRC #perl6 mentioned that the Perl 6 spec had changed so my example would need to be:<br />
<pre class="brush: perl;">
augment class Ninja {    
    method battle_cry {
        say $.name ~ ' says zing!!!'; 
    }
}
</pre></p>
<p>Rakudo is currently a bit behind Perl 6 spec here but above will replace <code>is also</code> shortly.  The change to <em>augment</em> makes sense because it avoids any confusion with roles in the class declaration.</p>
<p>The second bump was how to create a method dynamically.  Now being familiar with Moose I had expected the following to work:<br />
<pre class="brush: perl;">
Ninja.meta.add_method( 'swing', method ( Str $sound_effect ) { 
    say &quot;{$.name}: {$sword_symbol} {$sound_effect}&quot;;
});
</pre></p>
<p>And according to most Perl 6 docs <code>.meta</code> was the method to access the metaclass (MOP).  However <em>meta</em> produced a method not defined error <img src='http://s0.wp.com/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>Trawling the web further I did find that you could do:<br />
<pre class="brush: perl;">
Ninja.swing = method { say &quot;Changed to zing&quot; };
</pre></p>
<p>But this only allowed you to redefine a method, not create a new one.</p>
<p>Luckily IRC #perl6 came to the rescue again!  <a href="http://github.com/jnthn">jnthn</a> &amp; Moritz pointed me to <code>$object.^add_method( ... )</code> and the <a href="http://github.com/rakudo/rakudo/blob/ng/docs/metamodel.pod#">docs on the ng branch</a> of <a href="http://github.com/rakudo/rakudo">Rakudo</a> on Github.</p>
<p>Perl6 does have some beautiful syntax.  I love the <code>$drew does Throwstar</code> line.  Soon Rakudo will be able to do:<br />
<pre class="brush: perl;">
$drew does role {
    method throw_star { say &quot;throwing star&quot; }
};
</pre></p>
<p>So this, <em>augment</em> and probably lots more will be part of the <a href="http://github.com/rakudo/rakudo/tree/ng">ng branch</a> which will be merged with the Rakudo master in the not too distant future.</p>
<p>/I3az/</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/823/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/823/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=823&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2010/01/14/anyone-for-perl-6-metaprogramming/feed/</wfw:commentRss>
		<slash:comments>10</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>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>
	</channel>
</rss>
