<?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/"
	>

<channel>
	<title>Glen Scott - Keeping it simple &#187; perl</title>
	<atom:link href="http://www.glenscott.co.uk/blog/category/perl/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.glenscott.co.uk/blog</link>
	<description>Thoughts on software development and other geeky pursuits.</description>
	<lastBuildDate>Fri, 10 Feb 2012 17:23:07 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Objects in Perl (part 2,352)</title>
		<link>http://www.glenscott.co.uk/blog/2009/02/20/objects-in-perl-part-2352/</link>
		<comments>http://www.glenscott.co.uk/blog/2009/02/20/objects-in-perl-part-2352/#comments</comments>
		<pubDate>Fri, 20 Feb 2009 12:33:03 +0000</pubDate>
		<dc:creator>Glen</dc:creator>
				<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[moose]]></category>
		<category><![CDATA[oop]]></category>

		<guid isPermaLink="false">http://www.glenscott.co.uk/?p=340</guid>
		<description><![CDATA[One of my issues with Perl has always been its implementation of OOP. Or, to be precise, the multitude of ways that a class can be declared (blessed hash, inside-out, Class::Std etc). I would argue that this is a case where TMTOWTDI is not advantageous. Enter PiersÂ Cawley and his _Moose for Ruby Programmers_ talk at [...]]]></description>
			<content:encoded><![CDATA[<p>One of my issues with Perl has always been its implementation of OOP.  Or, to be precise, the multitude of ways that a class can be declared (blessed hash, <a href="http://search.cpan.org/dist/Object-InsideOut/lib/Object/InsideOut.pod">inside-out</a>, <a href="http://search.cpan.org/~dmuey/Class-Std-0.0.9/lib/Class/Std.pm">Class::Std</a> etc).  I would argue that this is a case where <a href="http://en.wikipedia.org/wiki/There_is_more_than_one_way_to_do_it">TMTOWTDI</a> is not advantageous.  Enter PiersÂ Cawley and his _Moose for Ruby Programmers_ talk at the London.pm Technical Meeting, 20th February 2009.  MooseX::Declare is yet another way, but check this out&#8230;</p>
<pre name="code" class="perl">
    use MooseX::Declare;

    class BankAccount {
        has 'balance' =&gt; ( isa =&gt; 'Num', is =&gt; 'rw', default =&gt; 0 );

        method deposit (Num $amount) {
            $self-&gt;balance( $self-&gt;balance + $amount );
        }

        method withdraw (Num $amount) {
            my $current_balance = $self-&gt;balance();
            ( $current_balance &gt;= $amount )
                || confess "Account overdrawn";
            $self-&gt;balance( $current_balance - $amount );
        }
    }

    class CheckingAccount extends BankAccount {
        has 'overdraft_account' =&gt; ( isa =&gt; 'BankAccount', is =&gt; 'rw' );

        before withdraw (Num $amount) {
            my $overdraft_amount = $amount - $self-&gt;balance();
            if ( $self-&gt;overdraft_account &#038;&#038; $overdraft_amount > 0 ) {
                $self-&gt;overdraft_account-&gt;withdraw($overdraft_amount);
                $self-&gt;deposit($overdraft_amount);
            }
        }
    }
</pre>
<p>Firstly, yes, that is indeed Perl.  Secondly, wow:  it actually _looks_ like a class definition.  And this was the big win for me, as anyone coming from a Java or PHP background will find it trivial to understand what&#8217;s going on here.  The main point is that by using MooseX::Declare, you are moving towards a more declarative programming style describing _what_ the program should do rather than _how_ it should achieve it.  So, no more unrolling `@_` <img src='http://www.glenscott.co.uk/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Links</h3>
<ul>
<li><a href="http://search.cpan.org/~flora/MooseX-Declare-0.03/lib/MooseX/Declare.pm">MooseX::Declare on CPAN</a></li>
<li><a href="http://www.bofh.org.uk/">Piers Cawley&#8217;s blog &#8211; Just A Summary</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.glenscott.co.uk/blog/2009/02/20/objects-in-perl-part-2352/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

