Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
(Perl) Callbacks - what are they?

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 16:06:22)

 
Associated topics are indexed under
P211 - Perl - Hashes
P307 - Perl/Tk

Back to
Coloured text in a terminal from Perl
Previous and next
or
Horse's mouth home
Forward to
The eye
Some other Articles
A visit from the solicitor
Last week - picture of the Perl course
Finishing up in Dhahran
The eye
(Perl) Callbacks - what are they?
Coloured text in a terminal from Perl
New Tape Librarian
Boys will be boys, saved by Ubuntu
Keeping customers informed by email
Perl for Systems Admin - suid scripts
2259 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 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).

© WELL HOUSE CONSULTANTS LTD., 2009: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 707126 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho