2011/08/26

Yes I love(d) PERL

So being a lover and not generally a hater... although some may disagree with that statement when it comes to me and closed source/lock in vendors, I really loved PERL in the day. 
I haven't had much chance to wave my fingers at a keyboard in the direction of the first language that I truly attempted OO with . So when I stumbled across Moose, I was truly fascinated by it. After the fascination wore off, I realised I can't read PERL to save my life at the moment and I would really need to start from scratch.
It brings back fond memories of writing a full OO language in PERL, circa 1998, and I was very proud if it. I also remember looking at the code about 6 years later and scratching my head as to what I wrote ;)
That and my terrible short term memory should not dissuade people from still utilising PERL for what it is really worth: Speed, and lots of pre-written modules!!!


So I give you an example of Moose to look at and see if you can read it. I must admit its rather nice and almost tempts me to write a PERL app just for the kick of it again, and to see how good Moose really is. Apologies to the Moose community for plagiarising your code snippet below, but if you want to see the full article go to http://search.cpan.org/~flora/Moose-2.0204/lib/Moose/Cookbook/Basics/Recipe1.pod and have a squizz at the explanations. Seriouslly if PERL 6 is anything like Moose, its gonna ROCK!!!


package Point;
  use Moose;

  has 'x' => (isa => 'Int', is => 'rw', required => 1);
  has 'y' => (isa => 'Int', is => 'rw', required => 1);

  sub clear {
      my $self = shift;
      $self->x(0);
      $self->y(0);
  }

  package Point3D;
  use Moose;

  extends 'Point';

  has 'z' => (isa => 'Int', is => 'rw', required => 1);

  after 'clear' => sub {
      my $self = shift;
      $self->z(0);
  };

  package main;

  # hash or hashrefs are ok for the constructor
  my $point1 = Point->new(x => 5, y => 7);
  my $point2 = Point->new({x => 5, y => 7});

  my $point3d = Point3D->new(x => 5, y => 42, z => -5);

No comments:

Post a Comment