-------------%< simpleclass.pm %<--------------
class simpleclass;
has $.message = "Hello";
method greet ( $greetee? = "World" )
{
say $.message~" "~$greetee;
}
-------------%< simpleclass.pm %<--------------
and
----------%< 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 %<-----------
Unfortunately, that wasn't to be, as it failed:
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)
C:\perl6\perl6code>
Scratch Scratch..
Its either my understanding or the system, both of which are still
in development, so I'll try doing the same a different way:
-------------%< simpleclass.pm %<--------------
class simpleclass is rw;
has $.message;
submethod BUILD ( $.message? = "Hello" ){};
method greet ( $greetee? = "World" )
{
say $.message~" "~$greetee;
}
-------------%< 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,
the 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.
And breathe.
Thats enough for today. More later.
First Attempts at Object Orientation in Perl 6 ( using pugs 0 Comments More | Login | Reply /