Tag Archives: perl

Objects in Perl (part 2,352)

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 the London.pm Technical Meeting, 20th February 2009. MooseX::Declare is yet another way, but check this out…

    use MooseX::Declare;

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

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

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

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

        before withdraw (Num $amount) {
            my $overdraft_amount = $amount - $self->balance();
            if ( $self->overdraft_account && $overdraft_amount > 0 ) {
                $self->overdraft_account->withdraw($overdraft_amount);
                $self->deposit($overdraft_amount);
            }
        }
    }

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’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 `@_` 🙂

Links

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

Review: London Perl Workshop

Spent an interesting morning in the company of many Perl mongers at the London Perl Workshop. Although I don’t use Perl as my primary language any more, it was was interesting to learn about the current movements of its London-based community. The talks I attended were:

  • The Complete History of the Perle Mongers of Olde London Towne – Dave Cross
  • DBIx::Class for (advanced) beginners‎ – Leo Lapworth
  • closures for fun and maybe profit‎ – David Cantrell
  • Catalyst, DBIx::Class and PostgreSQL -‎ Matt Trout
  • ‎Introduction to Moose – Mike Whitaker

In particular, DBIx::Class caught my eye as a nice way of abstracting database details behind more friendly object methods. Although, as the list of contributors shows, this is a highly complex problem to solve and achieving a “one size fits all” solution may be unrealistic.

Matt Trout’s talk was a little disappointing. Although I admired his passion and enthusiasm, there was far too much crammed in the 40 minute talk. Just concentrating on one aspect of the development process would been more useful. However, saying that, he did enough to convince me that I need to revisit PostgreSQL at some point in the future.

My colleague Mike Whitaker talked about the “postmodern object system”, Moose. There was lots of questions by the end of the talk, which was a good sign that the introduction had achieved its aim.

Overall, a good experience and a reminder that the Perl community is very much alive and well.

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn

London Perl Workshop – 29th November

The London Perl Workshop is a free one-day conference in central London, UK. It will be held on Saturday the 29th of November, 2008 at Westminster University’s New Cavendish Campus

I’ll be attending, and so will my friend and fellow Yahoo Mike Whitaker, who will be presenting some talks on Moose and Enlightened Perl.

Glen Scott

I’m a freelance software developer with 18 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork. I am the Managing Director of Yellow Square Development.

More Posts

Follow Me:
TwitterFacebookLinkedIn