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))
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.

Example:
  $first = new roundtable(1800,"Pine");

Where does Perl find the new method?

Firstly, it will look for a sub called new in the roundtable package. If there isn't one, it will look in any packages which roundtable inherits from by looking in the list @ISA, and it will recursively explore all parent packages until it finds a new sub.

If no new sub is available, the same traversal will be applied looking for a sub called AUTOLOAD. And if that fails too, then an error / excpetion is thrown.

How does Perl run two new methods?

There are occasions when you'll want to construct an object and within its constrcutor call the constructor of its base class. This is the super call in Python, and you may be familiar with other ways of doing this in other languages - it's pretty nonstandard.

In Perl, you can call the constructor of the parent class, and the
re-bless the resulting reference into the subclass. Here's an example, where a roundtable is based on a table but with a single extra parameter.

table constructor:

  sub new {
    my ($type,$mat) = @_;
    my %self;
    $self{m} = $mat;
    bless \%self,$type;
    }


roundtable constructor which calls in table constructor:

  sub new {
    my ($type, $diam, $material) = @_;
    my $this = new table($material);
    $this->{h} = $diam;
    bless $this,$type;
    }


Complete, working example (also shows AUTOLOAD in use) [here] from last week's Perl Course.
(written 2012-12-03, updated 2012-12-08)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P218 - Perl - More Objects
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [246] When to bless a Perl variable - (2005-03-15)
  [531] Packages in packages in Perl - (2005-12-16)
  [588] Changing @INC - where Perl loads its modules - (2006-02-02)
  [592] NOT Gone phishing - (2006-02-05)
  [656] Think about your design even if you don't use full UML - (2006-03-24)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [930] -> , >= and => in Perl - (2006-11-18)
  [1217] What are factory and singleton classes? - (2007-06-04)
  [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)
  [1665] Factory method example - Perl - (2008-06-04)
  [1819] Calling base class constructors - (2008-10-03)
  [1949] Nuclear Physics comes to our web site - (2008-12-17)
  [2427] Operator overloading - redefining addition and other Perl tricks - (2009-09-27)
  [2651] Calculation within objects - early, last minute, or cached? - (2010-02-26)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2811] Igloos melt in the summer, but houses do not - (2010-06-15)
  [2876] Different perl examples - some corners I rarely explore - (2010-07-18)
  [2972] Some more advanced Perl examples from a recent course - (2010-09-27)
  [3097] Making Perl class definitions more conventional and shorter - (2010-12-20)
  [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
  [3377] What do I mean when I add things in Perl? - (2011-08-02)
  [3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
  [4096] Perl design patterns example - (2013-05-20)
  [4098] Using object orientation for non-physical objects - (2013-05-22)
  [4356] Object factories in C++, Python, PHP and Perl - (2014-12-19)
  [4366] Changing what operators do on objects - a comparison across different programming languages - (2014-12-26)


Back to
Run other processes from within your Perl program
Previous and next
or
Horse's mouth home
Forward to
Santa Trip - Melksham Railway Development Group, 2012
Some other Articles
vargs in Python - how to call a method with unknown number of parameters
Melksham Christmas Lights, 2012
With us for a year already!
Santa Trip - Melksham Railway Development Group, 2012
Building an object based on another object in Perl
Run other processes from within your Perl program
Lots of ways of doing the same thing in Perl - list iteration
A long overdue meeting - a steeping stone towards coordinate transport user inputs
Agenda for Wiltshire Link Travel groups meeting, Saturday 1st December 2012
Lisa is home today ...
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.net/mouth/3941_Bui ... -Perl.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb