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 you do it? Within your class, call up the overload program to redirect the operator that you're overloading to a sub of your choice. And provide that sub - with two parameters passed in through @_ for the two objects being added. Within the sub you should check that you really do have objects (my example is kept simple and does not have this check) and if you do, construct a new object that's a combination of the inputs.
I've chosen - for my example - to stack boxes which have an "x" and a "y" dimension; the new combined box when you add two of them together has an "x" which is the sum of the two "x"es, and a "y" which is the greater of the two "y"s.
Operators can be overloaded in Python too (
example) and in C++ (
example), but not in PHP, which is a loosely typed language.
Illustation ... I might look for a clever title like "adding pieces to a jigsaw puzzle" ... but actually these are delegates on a private Perl course at our training centre and hotel, relaxing during the evening in the old fashioned way by doing a jigsaw.
Our courses provide not only the lectures and practical sessions, but also a chance to get to know your colleagues better, and to exchange ideas with other delegates learning the same subject. (written 2009-09-27, updated 2010-06-20)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P303 - Perl - Miscellany [2219] Configuring httpd, or Tomcat, to run CGI scripts in Perl - (2009-06-05)
[2876] Different perl examples - some corners I rarely explore - (2010-07-18)
[2877] Further more advanced Perl examples - (2010-07-19)
[3012] Exception handling in Perl - using eval - (2010-10-23)
[3159] Returning multiple values from a function call in various languages - a comparison - (2011-02-06)
P219 - Perl - Libraries and Resources [86] Talk review - Idiomatic Perl, David Cross - (2004-10-12)
[112] Avoid the wheel being re-invented by using Perl modules - (2004-11-08)
[357] Where do Perl modules load from - (2005-06-24)
[358] Use standard Perl modules - (2005-06-25)
[712] Why reinvent the wheel - (2006-05-06)
[737] Coloured text in a terminal from Perl - (2006-05-29)
[760] Self help in Perl - (2006-06-14)
[1219] Judging the quality of contributed Perl code - (2007-06-06)
[1235] Outputting numbers as words - MySQL with Perl or PHP - (2007-06-17)
[1391] Ordnance Survey Grid Reference to Latitude / Longitude - (2007-10-14)
[1444] Using English can slow you right down! - (2007-11-25)
[1863] About dieing and exiting in Perl - (2008-11-01)
[1865] Debugging and Data::Dumper in Perl - (2008-11-02)
[2229] Do not re-invent the wheel - use a Perl module - (2009-06-11)
[2234] Loading external code into Perl from a nonstandard directory - (2009-06-12)
[2931] Syncronise - software, trains, and buses. Please! - (2010-08-22)
[3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
[3101] The week before Christmas - (2010-12-23)
[3377] What do I mean when I add things in Perl? - (2011-08-02)
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)
[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)
[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)
[3581] Perl - calls to methods that use => - what do they mean? - (2012-01-16)
[3941] Building an object based on another object in Perl - (2012-12-03)
[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)
Some other Articles
Moving busstop!Not just a PHP program - a good web applicationTcl scripts / processes on a web server via CGIDiluting HistoryOperator overloading - redefining addition and other Perl tricksWhich version of MySQL am I running?Weekend and Christmas Promotion - Well House Manor Hotel, MelkshamA Winter Weekend Special at Well House ManorWhat is a JVM, a JRE, a JDK - components of the core Java EnvironmentLooking inside Java classes - javap and javadoc