Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
From fish, loaves and apples to money, plastic cards and BACS (Perl references explained)

The EuroMoney'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:
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:

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");
  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) {
  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
C207 - C and C based languages - Pointers and references
  [4128] Allocating memory dynamically in a static language like C - (2013-06-30)
  [3386] Adding the pieces together to make a complete language - C - (2011-08-11)
  [3242] How to return 2 values from a function (C++ and C) - more uses of pointers - (2011-04-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [3121] New year, new C Course - (2011-01-05)
  [3004] Increment operators for counting - Perl, PHP, C and others - (2010-10-18)
  [2670] Pointers to Pointers to Pointers - what is the point? - (2010-03-10)
  [2572] The what and why of C pointers - (2010-01-13)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [1497] Training Season Starts again! - (2008-01-07)
  [1478] Some new C programming examples - files, structs, unions etc - (2007-12-19)
  [1155] Pointers in C - (2007-04-19)

P217 - Perl - More than Simple Lists and Hashes!
  [3906] Taking the lead, not the dog, for a walk. - (2012-10-28)
  [3577] How to do multidimensional arrays (or rather lists and hashes) in Perl - (2012-01-14)
  [3444] Take the dog on a lead - do not carry her. Perl references. - (2011-09-17)
  [3406] Not multidimentional arrays - but lists of lists. Much more flexible. Perl! - (2011-08-26)
  [3118] Arrays of arrays - or 2D arrays. How to program tables. - (2011-01-02)
  [3105] Adventure with references to lists and lists of references - (2010-12-26)
  [3072] Finding elements common to many lists / arrays - (2010-11-26)
  [3007] Setting up a matrix of data (2D array) for processing in your program - (2010-10-21)
  [2996] Copying - duplicating data, or just adding a name? Perl and Python compared - (2010-10-12)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [2840] Just pass a pointer - do not duplicate the data - (2010-06-30)
  [2241] Perl references - $$var and \$var notations - (2009-06-15)
  [1514] Autovivification - the magic appearance of variables in Perl - (2008-01-21)
  [293] Course follow-ups - (2005-04-27)
  [43] Hash of lists in Perl - (2004-09-09)

Y106 - Object Oriented Python
  [4129] Simple OO demonstration in C++, comparison to Python - (2013-07-01)
  [4028] Really Simple Class and Inheritance example in Python - (2013-03-04)
  [4021] Spike solution, refactored and reusable, Python - Example - (2013-02-28)
  [3947] this or self - what are they, and what is the difference? (Python) - (2012-12-08)
  [3878] From Structured to Object Oriented Programming. - (2012-10-02)
  [3673] Object oriented or structured - a comparison in Python. Also writing clean regular expressions - (2012-03-26)
  [3436] Moving from scripting to Object Orientation in Python - (2011-09-13)
  [3085] Object Oriented Programming for Structured Programmers - conversion training - (2010-12-14)
  [2604] Tips for writing a test program (Ruby / Python / Java) - (2010-01-29)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2017] Python - a truly dynamic language - (2009-01-30)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [1348] Screw it or Glue it? Access to Object variables - a warning - (2007-09-12)
  [1306] Python class rattling around - (2007-08-16)
  [900] Python - function v method - (2006-10-20)
  [834] Python makes University Challenge - (2006-08-15)
  [477] Class, static and unbound variables - (2005-10-25)

Back to
Perl - making best use of the flexibility, but also using good coding standards
Previous and next
Horse's mouth home
Forward to
$ is atomic and % and @ are molecular - Perl
Some other Articles
Last chance this summer - Swindon and North Wiltshire to Weymouth by through train
That spec is a kingfisher ...
Open Source Training Schedule - learn a programming language - in Autumn 2011 or 2012
$ is atomic and % and @ are molecular - Perl
From 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 standards
Does a for loop evaluate its end condition once, or on every iteration?
Tables as Objects in Lua - a gentle introduction to data driven programming
Parallel but not really parallel. Moving game characters. Coroutines in Lua.
The difference between lists and strings - Tcl
4398 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, 87, 88 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., 2015: 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/3399_Fro ... ined-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat