Take the dog on a lead - do not carry her. Perl references.
Archive - Originally posted on "The Horse's Mouth" - 2011-09-17 17:29:39 - Graham Ellis
And ... in a similar vein ... when you're passing some data into a named block of code (a.k.a. sub, subroutine, function, procedure, command, method, macro) when programming, it's much more efficient to pass in a pointeri (a.k.a. an address or a reference)
When you pass in a pointer, you're saving yourself having to duplicate the data. That can be a serious saving if you're working with large or huge data sets. And you're also providing a powerful (but dangerous if misused) capability of altering the original data within the code that you've called.
On yesterday's Perl course, I wrote a fresh example that compares copying a list into a Perl sub, and passing in the address. The difference is just a \ !
Copying into the sub:
addon("Ibiza",@places);
Passing a reference into the sub:
anotheradd("Magaluf",\@places);
Full source code of the demo [here], including the source of the subs which must (of course) be coded to accept the data type that's being passed to them.
Perl's references are also an excellent way of setting up collections of collections - a very flexible and space-saving alternative to the multidimensional arrays of other languages. New example of those - [here].