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 standard modules on the CPAN (Comprehensive Perl Archive Network), and it's an API (Application / Programmer Interface) that's designed to be robust, easy to follow, and doesn't add a heavy footprint of variables and names into your program.
Internally, the code:
$present = new box(-smell => "chocolate", noise => "silent");
is the same as:
$present = box::new("box", "-smell", "chocolate", "noise", "silent");
- in other words, run the
new sub in the
box package, passing in five parameters. The first parameter appears to be redundant (and indeed it is until we start using inheritance), and the other parameters are pairs of attribute names and values.
•
=> is a synonym for
,
• We can leave the quotes off the attribute names if they are single words before the
=>
• The extra
- sign ensures that the property name is never going to be a Perl keyword, and the class needs to strip it off if present.
There's a full example, showing this call (and the class / package that implements it)
[here] from last week's course. The example is a generic base class with a common setter that handles the various standard ways of passing options (i.e. as above, and also as a reference to a hash).
There's a further example - showing both syntaxes of the constructor and also accessor methods -
[here], and the simple class that implements it
[here].
Topic covered on our
Perl for Larger Projects course and also as appropriate / requested on tailored private courses.
(written 2012-01-16, updated 2012-01-28)
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)
[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
The changing Hotel and Bed and Breakfast business in MelkshamQR codes - graphics images that provide quick phone linksExpanding a list of parameters in Tcl - {*} and evalTcl collections - lists, dicts and arrayPerl - calls to methods that use => - what do they mean?Melksham Area - buses, trains, cycles, and roads. Meeting, 17.1.2012Transport in Cambridgeshire - seen by an outside observer. What can Wiltshire learn?A busy start to 2012How to do multidimensional arrays (or rather lists and hashes) in PerlTcl - apparently odd behaviour of string trimleft