Main Content

From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)

Archive - Originally posted on "The Horse's Mouth" - 2011-08-20 08:37:49 - Graham Ellis

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.