After a few digs through the docs and synopsis I think I've got
my head around things enough to have a go at writing a simple
object.
The OO in perl 6 looks a lot more "finished" (for want of a better description) than perl 5, which was a little
bare, though extensible. Looking through the synopsis and some
websites I start to see hints of the thought that has
gone into the language - reading the synopsis documents a sense of understated elegance comes accross, classes for example, are to manage objects,
and code duplication management has been farmed out to roles, and after spending some time with java, with its single inheritance, and interfaces which
seem to force the use of cut and paste it seems like a breath of
fresh air. As I flick through the docs more and more, I see an
essence of perl that has been preserved - and promoted in the
design changes of perl 6 over perl 5 - the taking
of oft used lengthy constructs and the creation of a new function,
or squiggle to decrease the amount of typing to acheive a common
goal. Coupled with the rationalisation thats gone on I'm starting
to comprehend what an absolutely massive undertaking this is.
The vision powering perl 6, and the genius and drive going into
making it happen is quite inspiring. Even if I had the brains I
think I would have given up and gone down the beach quite some
time ago.
Anyway, onto the simple object - this time just a simple class
to display our "Hello World" message. Looking at the Synopsis
12, I thought the code below would work:
-------------%< simpleclass.pm %<--------------and
class simpleclass;
has $.message = "Hello";
method greet ( $greetee? = "World" ) { say $.message~" "~$greetee; }
-------------%< simpleclass.pm %<--------------
----------%< simpleclasscaller.pl %<-----------
use simpleclass;
my simpleclass $instance .= new;
$instance.greet(); $instance.greet("Planet erf"); $instance.message = "hi";
$instance.greet(); $instance.greet("Planet erf");
----------%< simpleclasscaller.pl %<-----------
C:\perl6\perl6code>..\pugs-win32\pugs.exe simpleclasscaller.pl pugs.exe: user error (*** unexpected "=" expecting trait, "handles", ";" or end of input at ./simpleclass.pm line 4, column 23 simpleclasscaller.pl line 1, column 1)Scratch Scratch..
C:\perl6\perl6code>
-------------%< simpleclass.pm %<--------------A quick run through, to save looking through Synopsis 12, and baring in mind that I may have made glaring ommisions or errors,
class simpleclass is rw;
has $.message;
submethod BUILD ( $.message? = "Hello" ){}; method greet ( $greetee? = "World" ) { say $.message~" "~$greetee; }
-------------%< simpleclass.pm %<--------------
class
line tells perl6 that the rest of the file contains the class definition( as it isn't followed by braces, which would imply that the class definition is contained within a block ) and the is rw
defines the all instance variables to be lvalues by default. The has
line gives each instance a publically accesable variable ( I think, though the docs say its an accessor ). There are
a couple of uses of default variables in the parameters ( the trailing question mark indicates the argument is optional , and the equals sets the default ), and this class uses submethod
to overide the default constructor - using implied magic, as any instance variable mentioned in arguments to the constructor automagically get copied into the instance.