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 articles
P307 - Perl/Tk [595] Add a friendly front end with Tk - (2006-02-08)
[596] The magic of -textvariable - (2006-02-08)
[599] Perl/Tk real time display - (2006-02-10)
[1310] Callbacks - a more complex code sandwich - (2007-08-19)
[1340] Tk locks up - 100% c.p.u. on a simple program (Tcl, Perl, Python) - (2007-09-09)
[3009] Expect in Perl - a short explanation and a practical example - (2010-10-22)
P211 - Perl - Hashes [240] Conventional restraints removed - (2005-03-09)
[386] What is a callback? - (2005-07-22)
[930] -> , >= and => in Perl - (2006-11-18)
[968] Perl - a list or a hash? - (2006-12-06)
[1334] Stable sorting - Tcl, Perl and others - (2007-09-06)
[1705] Environment variables in Perl / use Env - (2008-07-11)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1856] A few of my favourite things - (2008-10-26)
[1917] Out of memory during array extend - Perl - (2008-12-02)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2836] Perl - the duplicate key problem explained, and solutions offered - (2010-06-28)
[2915] Looking up a value by key - associative arrays / Hashes / Dictionaries - (2010-08-11)
[2920] Sorting - naturally, or into a different order - (2010-08-14)
[3042] Least Common Ancestor - what is it, and a Least Common Ancestor algorithm implemented in Perl - (2010-11-11)
[3072] Finding elements common to many lists / arrays - (2010-11-26)
[3106] Buckets - (2010-12-26)
[3400] $ is atomic and % and @ are molecular - Perl - (2011-08-20)
[3451] Why would you want to use a Perl hash? - (2011-09-20)
[3662] Finding all the unique lines in a file, using Python or Perl - (2012-03-20)
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