Perl 6 has a full object and class implementation ... first simple example
[here]. Classes are defined with a
class keyword, instance variables with a
haskeyword, and methods with a
method keyword. There's no "public", "protected", "private" wording, but subsidiary sigils (twigils) ... an extra "." providing an automatic accessor, and an extra "!" making a variable visible only within the object - i.e. private.
Here's a class definition:
class trainservice {
has $.length;
has $.seatsPerCarriage;
has $.time;
has $.destination;
method passengers {
($.length * $.seatsPerCarriage * 1.4).floor;
}
}
It's all a very long way from defining classes and objects in Perl 5 - showing that Perl 6 really is a new language, but you can spot similarities with the block structure, with sigils, and with defaults like functions returning the result of the last operation. There's also a default constructor which
blesses a hash of the named parameters passed in (so under the bonnet, there's quite a bit more there from Perl 5.
Here's a test program to make use of that class:
our $south = trainservice.new(length=>3, seatsPerCarriage=>78, time=>"06:38", destination=>"Southampton");
our $north = trainservice.new(length=>2, seatsPerCarriage=>75, time=>"07:19", destination=>"Cheltenham Spa");
my @services = ($south, $north);
for @services -> $service {
say $service.time ~ " to " ~ $service.destination ~ " takes up to " ~ $service.passengers ~ " people";;
}
This is only a starter for what you can do with objects in Perl 6 - you 'll have interitance, static and factory methods, overloaded (with
multi) and overridden methods. You have multiple inheritance (which I hope you don't use too often) and
roles which are rather akin to interfaces allowing you to define abstract classes. As you can see, it's probably worth coming along and spending a day or two with us on Perl 6 training, even if you're familiar with Perl 5 ...
(written 2016-01-02)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P213 - Perl - Creating your own Classes [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
[246] When to bless a Perl variable - (2005-03-15)
[975] Answering ALL the delegate's Perl questions - (2006-12-09)
[983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
[1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
[1435] Object Oriented Programming in Perl - Course - (2007-11-18)
[1664] Example of OO in Perl - (2008-06-03)
[1864] Object Oriented Perl - First Steps - (2008-11-01)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2169] When should I use OO techniques? - (2009-05-11)
[2834] Teaching examples in Perl - third and final part - (2010-06-27)
[2877] Further more advanced Perl examples - (2010-07-19)
[2969] What does blessing a variable in Perl mean? - (2010-09-24)
[3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
[3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
[3833] Learning to use existing classes in Perl - (2012-08-10)
P600 - Perl 6 [582] DWIM and AWWO - (2006-01-30)
[2816] Intelligent Matching in Perl - (2010-06-18)
[4604] Perl - version 6 - official version launched at Christmas - (2016-01-02)
[4605] Hello World - in Perl 6 - (2016-01-02)
[4606] Using Perl 6 to analyse and report on data - (2016-01-02)
[4608] Introspecion in Perl 6 - (2016-01-02)
[4609] Mapping an array / list without a loop - how to do it in Perl 6 - (2016-01-03)
[4610] Sorting by key or by comparator - Perl 6 - (2016-01-03)
[4611] Hungarian, Camel, Snake and Kebab - variable naming conventions - (2016-01-03)
[4620] Perl 6 - a Practical Extraction and Reporting example! - (2016-01-11)
Some other Articles
Classes and object - first steps in Perl 6Happy New Year. Our customers are our ambassadorsTraining course and hotel room prices - 2016