From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)
Money's useful stuff.
Before money, transactions were undertake on a barter system; if I wanted a loaf of bread I might exchange it for a fish. If I was wanting to book passage on tthe stagecoach from the George in Melksham to London, I would have to take ia bushel of apples into my local travel agent, who would then find imself in the fruit business too. All pretty impractical for the efficient handling of a large volume of transactions.
By being only level "removed" and passing tokens or pointers around in some way, modern society uses money which allow a handle to goods or services, that can be re-assigned to other goods or services, to be used. Money is quiet efficient to pass around. But it's actually gone further; even coins in bulk are a handling issue, and we've move on to notes, and then from notes to cheques, credit and debit cards, and paperless banking through BACS.
There's a parallel in passing data around between blocks of code in most programming languages; rather than passing around all of the data (with the inefficiency of copying it), we can simply pass the address at which the data starts - or indeed the address of a whole collection of addresses if we want to pass in a whole number of items.
On the Perl course which I ran on Friday, I was passing generous amounts of data into
subs. `If I had simply passed in the data:
printmenu(%lunch);
or
printmenu(@lunch);
I would have been duplicating the data into the sub
and any changes that I made to it in the sub would have been lost on return, as they would be changes to a temportary copy. So instead I wrote:
printmenu(\%lunch);
and
printmenu(\@lunch);
The change is a subtle one in coding terms - an extra \ character, but it makes the most enormous difference. Passing in the address means that the data is not duplicated, and should I wish to do so I can alter it
and effect the original within my sub - in fact I did that to add items to the menu:
addmenu(\%lunch,"Nightcap","Cold Shower");
and
addmenu(\@lunch,"Nightcap","Cold Shower");
in my two examples.
Within the
sub, I can access values that are pointed to (referenced) with an extra $ character, so you'll see code like:
foreach $course(keys %$lead) {
and
foreach $course(@$lead) {
Full source examples
[here] passing a reference to a list and
[here] passing a reference to a hash.
These examples use pointers to pointers, allowing us to simulate multidimensional structures in Perl - lists of lists and hashes of lists. The structures are far more flexible that "arrays" ... I'll tell you how, quite briefly, towards the end of our
Perl Programming course and cover them in more detail - together with how they're extended to full object orientation - in our more advanced
Perl for larger projects course.
In Python, all data is held internally as references to objects, and as a result the passing of memory address pointers is done automaically behind the scenes for you.
In C and C++, you use & to mean "address of" and * to mean "contents of" in the body of your code. A variable declared with a * is one that holds an address and (in C++ only) a variable declared with an & is known as a reference - that's a sort of alias.
(written 2011-08-20)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
Y106 - Object Oriented Python [477] Class, static and unbound variables - (2005-10-25)
[834] Python makes University Challenge - (2006-08-15)
[900] Python - function v method - (2006-10-20)
[1306] Python class rattling around - (2007-08-16)
[1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
[1925] Introduction to Object Oriented Programming - (2008-12-06)
[2017] Python - a truly dynamic language - (2009-01-30)
[2169] When should I use OO techniques? - (2009-05-11)
[2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
[3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
[3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
[3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
[3878] From Structured to Object Oriented Programming. - (2012-10-02)
[3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
[4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
[4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
[4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
[4448] What is the difference between a function and a method? - (2015-03-04)
[4591] From single block to structure and object oriented programming - (2015-12-02)
[4650] Why populate object with values as you construct them? - (2016-02-18)
[4721] When to check an object type - Python isinstance example - (2016-11-03)
P217 - Perl - More than Simple Lists and Hashes! [43] Hash of lists in Perl - (2004-09-09)
[293] Course follow-ups - (2005-04-27)
[1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
[2241] Perl references - $$var and \$var notations - (2009-06-15)
[2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
[2877] Further more advanced Perl examples - (2010-07-19)
[2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
[3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3105] Adventure with references to lists and lists of references - (2010-12-26)
[3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
[3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
[3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
[3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
[3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
C207 - C and C based languages - Pointers and references [1155] Pointers in C - (2007-04-19)
[1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
[1497] Training Season Starts again! - (2008-01-07)
[2005] Variables and pointers and references - C and C++ - (2009-01-23)
[2572] The what and why of C pointers - (2010-01-13)
[2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
[3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
[3121] New year, new C Course - (2011-01-05)
[3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
[3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
[3386] Adding the pieces together to make a complete language - C - (2011-08-11)
[4128] Allocating memory dynamically in a static language like C - (2013-06-30)
[4560] Variables, Pointers and References - C and C++ - (2015-10-29)
Some other Articles
Last chance this summer - Swindon and North Wiltshire to Weymouth by through trainThat spec is a kingfisher ...Open Source Training Schedule - learn a programming language - in Autumn 2011 or 2012$ is atomic and % and @ are molecular - PerlFrom fish, loaves and apples to money, plastic cards and BACS (Perl references explained)Perl - making best use of the flexibility, but also using good coding standardsDoes a for loop evaluate its end condition once, or on every iteration?Tables as Objects in Lua - a gentle introduction to data driven programmingParallel but not really parallel. Moving game characters. Coroutines in Lua. The difference between lists and strings - Tcl