Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

lilstevey (4628)

lilstevey
  (email not shown publicly)

Consumes:
  • Heinekin
  • Kro
  • Addlestones
  • Old Rosie
  • Thatchers
  • Strongbow
  • Erdinger
  • Guiness
  • Budvar
  • Leffe ( usually blond )
  • Various Real Ales when in premises suitable

Journal of lilstevey (4628)

Saturday September 09, 2006
04:39 PM

First Attempts at Object Orientation in Perl 6 ( using pugs

[ #30943 ]
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 %<--------------

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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.