<?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; OOP</title>
	<atom:link href="http://transfixedbutnotdead.com/tag/oop/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; OOP</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>Calling methods with array variable not allowed!</title>
		<link>http://transfixedbutnotdead.com/2009/08/13/calling-methods-with-array-variable-not-allowed/</link>
		<comments>http://transfixedbutnotdead.com/2009/08/13/calling-methods-with-array-variable-not-allowed/#comments</comments>
		<pubDate>Thu, 13 Aug 2009 14:40:32 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://transfixedbutnotdead.com/?p=525</guid>
		<description><![CDATA[File this under&#8230;. Didn&#8217;t you know that you can&#8217;t do that! Here&#8217;s an example of something I tried the other day and was surprised to find didn&#8217;t work: Bit surprising that? The same also happens for a hash variable. I assume there is a very good reason why the perl parser won&#8217;t compile this. If [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=525&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>File this under&#8230;.</p>
<blockquote><p>Didn&#8217;t you know that you can&#8217;t do that!</p></blockquote>
<p>Here&#8217;s an example of something I tried the other day and was surprised to find didn&#8217;t work:<br />
<pre class="brush: perl;">
{
    package Baz;
    use Moose;
    
    sub a  { 'A'  }
    sub aa { 'AA' }
}

my $baz = Baz-&gt;new;
my @methods = qw/a aa/;

# OK just to show u that I can call method with normal $a variable
my $a = $methods[0];
say $baz-&gt;$a;    # 'a'

# but when I use an array variable instead it wont compile
say $baz-&gt;$methods[1];    #! syntax error .... near &quot;$methods[&quot;
</pre></p>
<p>Bit surprising that?    The same also happens for a hash variable.</p>
<p>I assume there is a very good reason why the perl parser won&#8217;t compile this.   </p>
<p>If you want to avoid having to copy into scalar beforehand (as in $a in above) then one (perhaps only?) solution is:</p>
<p><pre class="brush: perl;">
say $baz-&gt;${ \$methods[1] };    # 'AA'
</pre></p>
<p>/I3az/</p>
<p>PS.  Only thing I could find on this was this Perlmonks post:</p>
<ul>
<li><a href="http://perlmonks.org/?node_id=512124">calling methods using array variables (Perlmonks)</a>
</ul>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/525/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/525/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/525/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=525&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2009/08/13/calling-methods-with-array-variable-not-allowed/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>Doodling with Moose (part 1)</title>
		<link>http://transfixedbutnotdead.com/2008/03/12/doodling-with-moose-part-1/</link>
		<comments>http://transfixedbutnotdead.com/2008/03/12/doodling-with-moose-part-1/#comments</comments>
		<pubDate>Wed, 12 Mar 2008 15:12:17 +0000</pubDate>
		<dc:creator>draegtun</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://draegtun.wordpress.com/?p=24</guid>
		<description><![CDATA[While perusing my RSS I came across Doodle in an article at Ruby Inside Looks good hey. But it got even more interesting when I saw how the class is defined&#8230; This looks a lot like Moose&#8230; a Perl Object system. Anyway been meaning to get into Moose for ages now and so far only [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=24&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>While perusing my RSS I came across <a href="http://doodle.rubyforge.org/">Doodle</a> in an article at <a href="http://www.rubyinside.com/doodle-a-new-way-to-build-and-define-ruby-classes-795.html">Ruby Inside</a> </p>
<p><pre class="brush: ruby;">
event = Event &quot;Festival&quot; do
  date '2008-04-01'
  place &quot;The muddy field&quot;
  place &quot;Beer tent&quot; do
    event &quot;Drinking&quot;
  end
end
</pre></p>
<p>Looks good hey.  But it got even more interesting when I saw how the class is defined&#8230;</p>
<p><pre class="brush: ruby;">
require 'rubygems'
require 'date'
require 'pp'
require 'doodle'

class Location  String
  has :events, :init =&gt; [], :collect =&gt; :Event
end

class Event
  # or if you want to inherit from another class
  include Doodle::Helper
  include Doodle::Factory

  has :name, :kind =&gt; String
  has :date do
    kind Date
    default { Date.today }
    must 'be &gt;= today' do |value|
      value &gt;= Date.today
    end
    from String do |s|
      Date.parse(s)
    end
  end
  has :locations, :init =&gt; [], :collect =&gt; {:place =&gt; &quot;Location&quot;}
end
</pre></p>
<p>This looks a lot like <a href="http://www.iinteractive.com/moose/">Moose</a>&#8230;  a Perl Object system.<br />
<br />
Anyway been meaning to get into Moose for ages now and so far only used it to subclass an old library I had.<br />
So as a good way of getting me feet dirty I went ahead and done the above Doodle stuff in Moose&#8230;.</p>
<p><pre class="brush: perl;">
my $event = Event-&gt;new( 
    name  =&gt; 'Festival',
    date  =&gt; '2008-04-01', 
    locations =&gt; [ 
      { place=&gt;'The muddy field' }, 
      { place=&gt;'Beer Tent',events=&gt;['Drinking'] } 
    ],
);
</pre></p>
<p>I&#8217;m not sure if the Doodle example is a bit contrived but I decided to replicate how the objects worked in exactly same way.  However I slightly amended the contructor because it was  i) simpler to do this way in Perl!  &amp;   ii) Probably more accurate as an interface spec!!   (changes are&#8230; locations &amp; events are clearly defined as a list of things). </p>
<p><pre class="brush: perl;">
package Location;
use Moose;

has 'name'  =&gt; ( is =&gt; 'rw', isa =&gt; 'Str' );
has 'event' =&gt; ( is =&gt; 'rw', isa =&gt; 'ArrayRef[Object]');


package Event;
use Moose;
use Moose::Util::TypeConstraints;
require DateTime;

subtype 'DateTime'
    =&gt; as 'Object'
    =&gt; where { $_-&gt;isa( 'DateTime' ) };

subtype 'EventDate'
    =&gt; as 'DateTime'
    =&gt; where { $_ &gt;= DateTime-&gt;now }
    =&gt; message { 'date must be &gt;= today' };

subtype 'Places'
    =&gt; as 'ArrayRef[Object]';
            
coerce 'EventDate'
    =&gt; from 'Str'
    =&gt; via { 
        # below should be DateManip (some strange formatting bug in syntaxhighlighter!)
        require DateTime::Format::dateManip;
        DateTime::Format::dateManip-&gt;parse_datetime( $_ );
    };

coerce 'Places'
    =&gt; from 'ArrayRef[HashRef]'
    =&gt; via {
        my $array_ref = [];
        
        # Go thru all locations in Places
        for my $location ( @{ $_ } ) {
            # events assigned to this location
            my $events_ref = [];
            @$events_ref = map { Event-&gt;new( name =&gt; $_ )  } @{ $location-&gt;{events} }
                 if exists $location-&gt;{events};
                    
            push @$array_ref, 
                Location-&gt;new( name =&gt; $location-&gt;{place}, event =&gt; $events_ref ) }
        
        return $array_ref;
    };
    
has 'name'  =&gt; ( is =&gt; 'rw', isa =&gt; 'Str' );

has 'date'  =&gt; (
   is          =&gt; 'rw', 
   isa        =&gt; 'EventDate', 
   coerce   =&gt; 1, 
   default  =&gt; sub { DateTime-&gt;now } );
                 
has 'locations' =&gt; ( 
   is         =&gt; 'rw', 
   isa       =&gt; 'Places', 
   coerce  =&gt; 1 );
                     
</pre></p>
<p>And there we go.   The Moose example produced twice as many lines!   However this isn&#8217;t the fault of Moose but mainly because I had to manually format stuff due to issues with the WordPress syntax highlighter with the Perl code ;-(    </p>
<p>More in part 2.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/draegtun.wordpress.com/24/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/draegtun.wordpress.com/24/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/draegtun.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/draegtun.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/draegtun.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/draegtun.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/draegtun.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/draegtun.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/draegtun.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/draegtun.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/draegtun.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/draegtun.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/draegtun.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/draegtun.wordpress.com/24/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/draegtun.wordpress.com/24/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/draegtun.wordpress.com/24/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=transfixedbutnotdead.com&amp;blog=351142&amp;post=24&amp;subd=draegtun&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://transfixedbutnotdead.com/2008/03/12/doodling-with-moose-part-1/feed/</wfw:commentRss>
		<slash:comments>11</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>
