Exercises, examples and other material relating to training module P218. This topic is presented on public courses
How do we define Inheritance and overloading in Perl? What inherits from where? What happens if a method is missing? How do we make variables from a module available in a calling module?
|Articles and tips on this subject||updated|
|4366||Changing what operators do on objects - a comparison across different programming languages|
In Object Oriented languages, you used named pieces of code known as methods to perform an operation on a variable - for example you might write
to take a list of (something) for a week and add a list relating to weekend onto the end of it.
Sometimes, the method ...
|4356||Object factories in C++, Python, PHP and Perl|
Raw materials are converted into a manufactured product in a factory or by a factory procedure, and the sort of product you can make depends on both the raw material that's supplied and on the capability of the factory.
And so it is with Object Oriented Computer Programming - supply raw material ...
|4096||Perl design patterns example|
For short programs, just a few lines of code in a single block do an excellent job. As the program size increases, some sort of structure such as named blocks of code (sub, def, function, proc, lambda etc) becomes logical. And as the code grows further, you should consider splitting out logic relating ...
|4098||Using object orientation for non-physical objects|
A table, a flower, a dog, a train and the second hand on a clock are all physical objects. And when teaching people about Object Oriented programming, using such things as examples is a real help in getting the concepts across an showing how they're implemented. But objects to further than that too - ...
|3941||Building an object based on another object in Perl|
When you create an object, you call a constructor method. In many languages, there's a special name for this method and the word new or init is often involved ... so in Perl it's a good convention to use new as well.
$first = new roundtable(1800,"Pine");
Where does Perl find the ...
|3581||Perl - calls to methods that use => - what do they mean?|
Have you seen Perl method calls that look like this?
$present = new box(-smell => "chocolate", noise => "silent");
and wondered "what is all this => stuff" and "what's the - sign for"?
That's a call that could be fairly typical of the sort you'll see in the documentation of any number of ...
|3377||What do I mean when I add things in Perl?|
Some things have a natural concept of addition. And of multiplication.
You can add numbers - 5 + 7 gives 12. You can add strings of text - "Hello " + "World" = "Hello World". You can add day trippers to a train and get a busier train (and add too many and you'll get an overcrowded unhappy train, ...
|3097||Making Perl class definitions more conventional and shorter|
Perl 5's object oriented capabilities are excellent - but often a little longwinded, and quite easy to get wrong. Moose sells itself as "postmodern object system for Perl 5 that takes the tedium out of writing object-oriented Perl"; it's available for download from the CPAN under same license as Perl ...
|3098||Learning Object Orientation in Perl through bananas and perhaps Moose|
Perl has the most incredbible range of OO facilities (though at times it's overfeatured and a bit old fashioned / tricky to use). So yesterday I wrote a whole series of examples ... and then went on to explore Moose - a module which adds extra keywords and lets you define your classes in a shorter ...
|2876||Different perl examples - some corners I rarely explore|
The private Perl course that I ran on Wednesday through Friday of last week was a little out of the ordinary as we were concentrating far more that usual on a wide variety of practices that may be found - either in legacy code or advanced recent code. Great fun for me, and plenty of new examples.
|2972||Some more advanced Perl examples from a recent course|
I ran an extra Perl for larger projects course, single company, at the tail end of last week and into the weekend (the only gap in my diary for a few weeks!) and - as is often the case on single-company courses - I wrote some new illustrative code to show specific subjects that came up in a different ...
|2427||Operator overloading - redefining addition and other Perl tricks|
What does addition actually mean? It means different things depending on what you're adding together, and in Perl you can redefine additions - and other operators - on objects of types which you define. This is known as operator overloading, and there's a complete code example here.
So ... how do ...
|2811||Igloos melt in the summer, but houses do not|
In Germany, a house will grow in value over the years (perhaps current economic climate excluded) but an Igloo will loose all of its value in summer (it will melt!).From last week's Perl course, where I briefly covered object orientation in Perl (the course briefing only asked me to cover it briefly), ...
|2717||The Multiple Inheritance Conundrum, interfaces and mixins|
Should an OO programming language support "multiple inheritance"? Let's define multiple inheritance first - starting from simple (single?) inheritance.
I don't want to have to define each type of thing ("class of object") from scratch, so I'll define once class as being based ...
|2651||Calculation within objects - early, last minute, or cached?|
When you create an object with the constructor, you may also set property [attribute] values within it ... or you may follow on with a series of method calls to set the initial values. Then when you pull back values / attributes / properties from your object you return those values, or the results ...
|1949||Nuclear Physics comes to our web site|
One of the major projects that we'll be undertaking in the near future is aimed at improving the findability of information on our site - you shouldn't need to know that you need a blog article dated 2nd Feb 2007 to find about the difference between a comparator and comparable in Java, nor to look up ...
|1819||Calling base class constructors|
In all object oriented languages, you have a facility called inheritance where you can define one type of thing ("class of object") based on another, and the newly defined class ("subclass" or "extended class") takes the initial ("base") class as it starting point.
In your code for your base class, ...
|1665||Factory method example - Perl|
Utility methods (factorys) are often used to create objects which may be of any of one of a series of different classes, depending on the data passed in. Have a look at this Perl program which reads in data from a log file and creates a list of web site visit objects:
|1664||Example of OO in Perl|
Although Perl 5 doesn't use the words "class", "method" or "object" (or any of the other common OO words), it never the less has a very good OO model indeed - here's the source code of an example, together with the result of running the sample. I'll then give you a few clues!
|227||Bellringing and Programming and Objects and Perl|
For the second time this year (and it's only the second month), I find that I have a keen bell-ringer on my programming course ... he's off each evening to meet up with fellow bell-ringers in church towers in Wiltshire, and having a really good time outside course hours as well as during the day. I understand ...
|1435||Object Oriented Programming in Perl - Course|
Perl Review (module P251) Subroutines in Perl (module P209) More than Simple Lists and Hashes! (module P217) Object Orientation: Individual Objects (module Q906) Creating your own Classes (module P213) Object Orientation: Composite Objects (module Q909) More Objects (module P218) Object Orientation: ...
|1320||Perl for Larger Projects - Object Oriented Perl|
Perl is a powerful language for short utility scripts - AND a powerful object oriented language too, which is great if you're going to be writing longer applications and / or a suite of programs with shared code.
We run a general course that introduced Perl for everyone and also a more advanced course ...
|1217||What are factory and singleton classes?|
Do you find some of the OO terminolgy baffling? Once you've learnt about constructors and methods, inheritance, overloading and polymorphism and statics, you might think you're there. Then someone mentions a "factory class" or a "singleton" ...
Fear not - factory and singleton classes are posh names ...
|930||-> , >= and => in Perl|
Yes, Perl is eclectic - lots of things it can do, taken from lots of places and so, yes, it does have all three of those operators!
-> Run the method named to the right on the object named on the left
>= Return 1 (a true value) if the expression to the left is numerically greater than the expression ...
|831||Comparison of Object Oriented Philosophy - Python, Java, C++, Perl|
There are two different philosophies that have been adopted by the authors of Object Oriented languages.
The first approach is to set the thing up in such a way that a programmer who uses someone else's code as the basis for his isn't going to be trusted to use that other person's code in a sensible ...
|531||Packages in packages in Perl|
In perl, all your variables (except "my" ones, which we'll leave for another day) are arranged into packages, and by default that's a package called "main". So if you write about a scalar called $jeremy, that's really $main::jeremy and if you write about a named block of code called &mary, that's ...
|592||NOT Gone phishing|
Early last week, I was giving a training course on Perl and talking a little bit about Perl objects - how all the internal logic that's associated with a particular type of data can be hidden within a source file so that the person writing the code to call it up only needs to understand what to put in, ...
|656||Think about your design even if you don't use full UML|
Even if you don't feel that your project is big enough to get involved with formal design methods, many of the lessons of UML and some informal design diagrams can help you get a clear view of what you're going to be doing be for you start, and can help you come up a good, thought out and reliable plan ...
|angler|| Program to demonstrate use of phisherman class|
|animal.pm|| A framework for a generic base class|
|basket|| Sample program - polymorphism in Perl OO|
|bne|| a standard base class?|
|cafe|| Test application - use of EXPORTER|
|cafe3|| cafe3 - offer a number of dishes|
|coin.pm|| Simple base class defining a coin|
|collect|| Application that uses commememorative coin class|
|commemorative.pm|| A class which uses simple inheritance|
|dish.pm|| package showing Exporter|
|fldo|| Set up obects via a utility factory (main prog)|
|horse.pm|| BEGIN END DESTROY and import special methods|
|li2|| the @INC list and the %INC hash|
|lookinc|| Display the @INC list|
|needbr|| require is runtime, use is compiletime|
|offspring.pm|| class offspring - an animal that need attention|
|oo6|| SUPER example|
|opolop|| Operator Overloading in Perl|
|overload_add.pl|| Operator (+) and to_string overloading, Perl|
|pet.pm|| a pet is an animal ... inheritance ...|
|phisherman.pm|| A simple class in Perl|
|pover|| overloading operators |
|ptest|| test harness for the program.pm class|
|rider|| Application that uses the horse.pm module|
|runner.pm|| Multiple classes in a single file|
|runtest|| Test code - for polymorphism / inheritance demo|
|tabby.pm|| calling the base class constructor in Perl|
|teacup.pm|| For use with require demo|
|test_class_1|| Test harness for class "animal"|
|test_class_2|| Test harness for the various subclasses of animal|
|tharness|| Perl program, calls 2 classes, polymorphism and interitance|
|tptest|| Test program for the trainingprogram.pm class|
|transport.pm|| Definition of a transport animal|
|ua|| Looking at data via Data::Dumper|
|visit.pm|| Set up obects via a utility factory (classes prog)|
A Generic Class.
Where to look for modules.
Inheritance and hierarchy.
The Base class.
The derived class.
Imports and exports.
Further notes on object orientation.
Extending our earlier generic example.
If you are looking for a complete course and not just a information on a single subject, visit our Listing and schedule
Well House Consultants specialise in training courses in
. We run
throughout the UK (and beyond for longer courses), and
at our training centre in Melksham, Wiltshire, England.
It's surprisingly cost effective to come on our public courses -
even if you live in a different
country or continent to us
We have a technical library of over 700 books on the subjects on which we teach.
These books are available for reference at our training centre.