Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

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". Happy Train of PassengersYou can add day trippers to a train and get a busier train (and add too many and you'll get an overcrowded unhappy train, add too few and you'll get an unjustified service). But you can't sensibly do certain addion operations. You can't add the colour purple to a DNS enquiry on port 53; it's a meaningless concept.

Perl's "+" operator works by default on numeric values - be they integers or floats. And you can redefine it on any of your own objects ("packages") if you wish, using the overload pragma. There's an example from last week's Perl for larger projects course - source code [here].

In the example (the source code is commented), I have shown an example of how to overload the "+" operator on an object of type box. Adding boxes together stacks them, so you get the greater of the two width and depth values, and the sum of the two heights, in a new box. But you have far more flexibiity than that - if I had wished, I could have returned an object of a different type, or added something other than a box to a box. Indeed, I've shown a multiplication example where I use a stack of identical boxes, and the second parameter in this case was a number.

When are two boxes "equal"? The most obvious answer is when all their attributes are the same, but it could be that you want something else. By overloading ==, I have redefined equality on boxes, considering them to be equal if they have the same volume.

And what do I get when I print out a variable that contains a box object? Without any instruction in the class, I'll get the fact that it's a box, the memory address at which it is held, and a note about the internal variable type that holds all the data about it. Overload the "" operator though, and you can change fromn the default to something which usefully described the object. Again, there's a commented example in the program that illustrates this article.

There's a further article [here], and a shorter source example showing just addition [here].

(written 2011-08-02, updated 2011-08-03)

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

P219 - Perl - Libraries and Resources
  [3101] The week before Christmas - (2010-12-23)
  [3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
  [2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
  [2234] Loading external code into Perl from a nonstandard directory - (2009-06-12)
  [2229] Do not re-invent the wheel - use a Perl module - (2009-06-11)
  [1865] Debugging and Data::Dumper in Perl - (2008-11-02)
  [1863] About dieing and exiting in Perl - (2008-11-01)
  [1444] Using English can slow you right down! - (2007-11-25)
  [1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
  [1235] Outputting numbers as words - MySQL with Perl or PHP - (2007-06-17)
  [1219] Judging the quality of contributed Perl code - (2007-06-06)
  [760] Self help in Perl - (2006-06-14)
  [737] Coloured text in a terminal from Perl - (2006-05-29)
  [712] Why reinvent the wheel - (2006-05-06)
  [358] Use standard Perl modules - (2005-06-25)
  [357] Where do Perl modules load from - (2005-06-24)
  [112] Avoid the wheel being re-invented by using Perl modules - (2004-11-08)
  [86] Talk review - Idiomatic Perl, David Cross - (2004-10-12)


Back to
Kennet and Avon - Walk from Bedwyn to Pewsey. TransWilts day out.
Previous and next
or
Horse's mouth home
Forward to
New product - ensuring that supply matches demand
Some other Articles
Report - day trip from Swindon / Chippenham / Melksham to Weymouth
Melksham Jelly - An Occasional Office for Home Workers
Sorting data the way YOU want it sorted
New product - ensuring that supply matches demand
What do I mean when I add things in Perl?
Kennet and Avon - Walk from Bedwyn to Pewsey. TransWilts day out.
How to interact with a Perl program while it is processing data
Speeding up your Perl code
Another busy Week at Well House Manor ... pictures from the midweek
Wearing the new London uniform
4291 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 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., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/3377_Wha ... Perl-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat