Randy Kobes > Apache2-Autocomplete-0.12 > Apache2::Autocomplete

Download:
Apache2-Autocomplete-0.12.tar.gz

Dependencies

Annotate this POD

CPAN RT

Open  0
Report a bug
Module Version: 0.12   Source  

NAME ^

Apache2::Autocomplete - Autocomplete service backend via mod_perl

SYNOPSIS ^

Given some form that using Google's autocomplete that receives suggestions from http://localhost/complete/search:

  ######################################################
  # in httpd.conf
  PerlModule Apache2::MyAutoComplete
  <Location /complete/search>
     SetHandler perl-script
     PerlResponseHandler Apache2::MyAutoComplete
  </Location>
  ######################################################
  
  ###################################################### 
  # module file Apache2/MyAutoComplete.pm
  package Apache2::MyAutoComplete;
  use base qw(Apache2::Autocomplete);
  # use whatever else
  
  my @NAMES = qw(bob carol ted alice);
  sub expand {
    my ($self, $query) = @_;
    my $re = qr/^\Q$query\E/i;
    my @names = grep /$re/, @NAMES;
    my @values = map {"some description"} @names;
    (lc $query, \@names, \@values, [""]);
  }
  
  sub handler {
    my $r = shift;
    my $ac = __PACKAGE__->new($r);
    $ac->run();
    return Apache2::Const::OK;
  }
  ######################################################

DESCRIPTION ^

This module is a mod_perl2 interface to JavaScript::Autocomplete::Backend, which is a base class for implementing an autocomplete service for a form using the Google Suggest protocol. See http://www.google.com/webhp?complete=1&hl=en for an illustration of Google Suggest in operation, as well as http://serversideguy.blogspot.com/2004/12/google-suggest-dissected.html for a description of how the JavaScript code works.

As well as mod_perl2, this package requires JavaScript::Autocomplete::Backend, as well as a CGI.pm-compatible CGI module for supplying the param() and header() methods. If available, CGI::Apache2::Wrapper will be used, which is a minimal module that uses methods of mod_perl2 and Apache2::Request to provide these methods; if this is not available, CGI (version 2.93 or greater) will be used.

Operation of this service requires inclusion of the Autocomplete JavaScript code; a copy of this is included in this distribution, the latest version of which is available at http://www.google.com/ac.js.

Example Form ^

An example form for which autocompletion is desired has the the following structure:

 <html><head>
 <title>Search</title>
 </head>
 <body onload="document.f.query.focus();">
 <form action="/search_handler" name=f>
 <br>
 <input autocomplete="off" maxlength=2048 
   name="query" size="25" title="Search" value="">&nbsp;
 <input name="btnG" type="submit" value="Search">
 </form>
 </body>
 <SCRIPT src="/js/ac.js"></SCRIPT>
 <SCRIPT>
 InstallAC(document.f,document.f.query,document.f.btnG,"search","en");
 </SCRIPT></html>

The Autocomplete JavaScript code is made available as /js/ac.js.

The basic operation of the autocompletion is controlled by the JavaScript function InstallAC (available in ac.js). This is called with 5 arguments:

Apache Handler ^

The autocompletion handler is specified through an Apache configuration directive such as

  PerlModule Apache2::MyAutoComplete
  <Location /complete/search>
     SetHandler perl-script
     PerlResponseHandler Apache2::MyAutoComplete
  </Location>

Here, search is the value specified by the fourth argument to InstallAC, as discussed above - the complete part of the location is hard-coded in ac.js. The Perl module which handles requests for this location has the form

  # module file Apache2/MyAutoComplete.pm
  package Apache2::MyAutoComplete;
  use base qw(Apache2::Autocomplete);
  # use whatever else
  
  sub expand {
    my ($self, $query) = @_;
    # decide what completions to return, based on the $query
    (lc $query, $names, $values, [""]);
  }
  
  sub handler {
    my $r = shift;
    my $ac = __PACKAGE__->new($r);
    $ac->run();
    return Apache2::Const::OK;
  }

This must inherit from Apache2::Autocomplete. Within this handler must be an expand method:

  sub expand {
    my ($self, $query) = @_;
    # decide what completions to return, based on the $query
    (lc $query, $names, $values, [""]);
  }

which is to return a list of 4 elements to be used for autocompletion of the I$query> argument passed in from the form. This list has the following elements:

The Apache handler itself:

  sub handler {
    my $r = shift;
    my $ac = __PACKAGE__->new($r);
    $ac->run();
    return Apache2::Const::OK;
  }

creates the object and calls the run method on it, which returns the autocomplete results in the form of JavaScript code that the original form then uses to fill in the suggestions.

Methods ^

The following methods are available.

SEE ALSO ^

For a description of the JavaScript backend, see JavaScript::Autocomplete::Backend and http://serversideguy.blogspot.com/2004/12/google-suggest-dissected.html.

If using CGI is a concern due to the memory footprint, see CGI::Apache2::Wrapper for a minimal CGI.pm-compatible module that uses methods of mod_perl2 and Apache2::Request.

Development of this package takes place at http://cpan-search.svn.sourceforge.net/viewvc/cpan-search/Apache2-Autocomplete/.

SUPPORT ^

You can find documentation for this module with the perldoc command.

    perldoc Apache2::Autocomplete

You can also look for information at:

COPYRIGHT ^

The Perl software is copyright 2007 by Randy Kobes <r.kobes@uwinnipeg.ca>. Use and redistribution are under the same terms as Perl itself; see http://www.perl.com/pub/a/language/misc/Artistic.html. The JavaScript autocomplete code contained in ac.js of this distribution is Copyright 2004 and onwards by Google Inc.