Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
(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, 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)


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
4759 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, 89, 90, 91, 92, 93, 94, 95, 96 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., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/738_-Per ... they-.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb