In most programming applications, you'll write code that calls system functions or subroutines - for example, you'll write a program that reads data from a file (via a system call), splits the data into an array (perhaps via a further system call), and prints it out (through system calls).
On a few occasions - for example, when writing a GUI event handler or a use defined sort routine, you'll break the pattern. You'll call a system routine which will itself call back to a named piece of code that you have written. This concept isn't always easy for newcomers to grasp, and on yesterday's Perl course I wrote an extra training example to show what's really going on ...
In Perl, you sort using the sort function. Easy. But if you want to sort in a different way (perhaps sorting by the length of a string rather than alphabetically), you pass sort the name of a function that you've written which takes two input variables - globals $a and $b - and returns a negative, zero, or positive result if $a is less than, equal to, or greater than $b. In other words, the sort function that's Perl provided does the difficult bit of managing what needs to be compared with what, swapping items over as necessary, and minimising the number of comparisons you make; you're left with writing a simple piece of code that just ranks two records against each other.
Like to see what I mean?
I wrote a demo - a piece of code in Perl that sorts a list of hash keys based on the number of characters in the value held in the hash. The sort call was:
@names = sort bylength (keys(%hols));
I then re-wrote that line as:
@names = wingit ("bylength",keys(%hols));
and provided an extra piece of code called wingit that provided alternative functionality to Perl's sort, and calls bylength internally. Of course, this is purely illustrative and Perl's built-in sort will be much more efficient.
Both pieces of code work - full source code here
. (written 2006-05-30, updated 2006-06-05)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesP211 - Perl - Hashes 
Finding all the unique lines in a file, using Python or Perl - (2012-03-20) 
Why would you want to use a Perl hash? - (2011-09-20) 
$ is atomic and % and @ are molecular - Perl - (2011-08-20) 
Buckets - (2010-12-26) 
Finding elements common to many lists / arrays - (2010-11-26) 
Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11) 
Sorting - naturally, or into a different order - (2010-08-14) 
Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11) 
Perl - the duplicate key problem explained, and solutions offered - (2010-06-28) 
Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27) 
Out of memory during array extend - Perl - (2008-12-02) 
A few of my favourite things - (2008-10-26) 
Perl - Subs, Chop v Chomp, => v , - (2008-10-08) 
Environment variables in Perl / use Env - (2008-07-11) 
Stable sorting - Tcl, Perl and others - (2007-09-06) 
Perl - a list or a hash? - (2006-12-06) 
-> , >= and => in Perl - (2006-11-18) 
What is a callback? - (2005-07-22) 
Conventional restraints removed - (2005-03-09)P307 - Perl/Tk 
Expect in Perl - a short explanation and a practical example - (2010-10-22) 
Tk locks up - 100% c.p.u. on a simple program (Tcl, Perl, Python) - (2007-09-09) 
Callbacks - a more complex code sandwich - (2007-08-19) 
Perl/Tk real time display - (2006-02-10) 
The magic of -textvariable - (2006-02-08) 
Add a friendly front end with Tk - (2006-02-08)
Some other Articles
A visit from the solicitorLast week - picture of the Perl courseFinishing up in DhahranThe eye(Perl) Callbacks - what are they?Coloured text in a terminal from PerlNew Tape LibrarianBoys will be boys, saved by UbuntuKeeping customers informed by emailPerl for Systems Admin - suid scripts