Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Classes and object - first steps in Perl 6

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)


Back to
Using Perl 6 to analyse and report on data
Previous and next
or
Horse's mouth home
Forward to
Introspecion in Perl 6
Some other Articles
Classes and object - first steps in Perl 6
Happy New Year. Our customers are our ambassadors
Training course and hotel room prices - 2016
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.info/mouth/4607_Cla ... erl-6.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb