The business view on J2EE versus [insert shiny technology here] Amazon launches new search site: A9
Apr 16

Perl 6 OO: Not as much of a hack ;)

Perl, Tech Add comments

Larry Wall has come out with Apocalypse 12 which details how you will define and use OO in Perl 6.

This is great to see, especially after also being up on the discussions for Groovy on how to handle certain things (e.g. properties).

What does a Perl 6 simple class / use look like now?

class Point {
has $.x;
has $.y is rw;

method clear () { $.x = 0; $.y = 0; }
}

my $point = Point.new(x => 2, y => 3);

$a = $point.y; # okay
$point.y = 42; # okay

$b = $point.x; # okay
$point.x = -1; # illegal, default is read-only

$point.clear; # reset to 0,0

Thank god for the keywords of class (and method). Finally it doesn’t feel like OO is bolted on (even though Larry thinks it was bolted through).

It is interesting that there is no constructor, yet “my $point = Point.new(x => 2, y => 3);” still does the right thing.

And moving from “->” to “.” is nice too.

You may wonder what the “$.x” is all about. This is an interesting use… if you said “$:x” that would make it private! Sneaky huh. You can make private classes via “class :MyPrivateClass {…}”, and private methods via “method :think (Brain $self: $thought)”

Another interesting thing is the part of “roles”.

E.g.

class Dog {
is Mammal;
does Pet;
does Servant;
does Best::Friend[Man];
does Drool;

}

I also enjoyed seeing this Groovy/Ruby/…-ish looking code:

@thumbs.each { .twiddle }

Whatever you think of this document, anyone can see that a LOT of thought has been put into this (as is always the case with Larry), and there are many things in which we can learn from (are you reading this Groovy guys?).

Great stuff. Hopefully we will see Perl 6 in the next 5 years ;)

One Response to “Perl 6 OO: Not as much of a hack ;)”

  1. laser hair removal philadelphia Says:

    Its very interested information

Leave a Reply

Spam is a pain, I am sorry to have to do this to you, but can you answer the question below?

Q: Type in the word 'ajax'