Kevin Old wrote:

> Hello everyone,
>
> I've seen the word "callback" used in a few Changelogs for a few modules
> saying that a few callbacks were fixed in the module.  I can't find any
> documentation on what a callback is and am wondering if anyone can point
> me to some docs, or explain what it is?
>
> Thanks,
> Kevin

A callback is afuction that you define and then offer a referende to when
calling another function or configuting an object, usually a library.  This
allows the function to use your definition in carrying out its overall
process.

Think about sort.  That is the most practical frequently used callback,
probably.  The sort function takes the block offered and uses it in a
standardized sorting algorithm.  This gives us the flexibility to specify
how we wish any two values being sorted to be compared. Here is an example
from my current project:

sub create_received_date_index {
...
 foreach my $rec_date_value
   (sort {received_date_compare($a, $b)} keys %$rec_date_index) {
    my $index_row = column_index_line($rec_date_index, $rec_date_value);
    print OUT "$index_row\n";
  }
...
}


sub received_date_compare {
  my ($date_1, $date_2) = @_;

#  s/^\D*// for $date_1, $date_2;
  my ($week_day1, $month_abbrev_1, $day_1, $time_1, $year_1)
   = split / /, $date_1;
  my ($week_day2, $month_abbrev_2, $day_2, $time_2, $year_2)
   = split / /, $date_2;

  $date_1 = [$year_1, $month_abbrev_1, $day_1, $time_1];
  $date_2 = [$year_2, $month_abbrev_2, $day_2, $time_2];
  return compare_date($date_1, $date_2);
}

Since the data I am working with can sometimes be a bit funky, this gives me
a chance to massage it a bit.  Anytime you have very specific needs in how a
process is caried out, callbacks can be useful.

Another application is in GUI programming.  We provide interactivity by
providing callbacks to be executed when specified events occur.   For
instance, you might create a command button this way:

  my $advanced_search_button = $search_dialog->add('Button',
   -text => 'Advanced Search',
   -command => [\&launch_advanced_search, $search_dialog])->pack(
    -side => 'top', -anchor => 'se');

so that when a user click on the 'Advanced Search' button, the
launch_advanced_search subroutine is called with the $dialog refeence as an
argument.

HTH,

Joseph



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to