Apache2-Autocomplete
view release on metacpan or search on metacpan
lib/Apache2/Autocomplete.pm view on Meta::CPAN
our $VERSION = 0.12;
sub new {
my ($class, $r) = @_;
die qq{Please supply an Apache request object \$r}
unless (defined $r and ref($r) eq 'Apache2::RequestRec');
return $class if (ref($class) eq __PACKAGE__);
my $cgi;
eval {require CGI::Apache2::Wrapper;};
if (not $@) {
$cgi = CGI::Apache2::Wrapper->new($r);
}
else {
eval {require CGI;};
if (not $@) {
if ($CGI::VERSION < 2.93) {
$r->log_error("A minimal CGI.pm version of 2.93 is required");
return;
}
$cgi = CGI->new($r);
}
else {
$r->log_error("A suitable CGI object isn't available");
return;
}
}
my $self = {cgi => $cgi, r => $r};
bless $self, $class;
}
sub r { return shift->{r};}
sub run {
my ($self, $header_extra) = @_;
my $r = $self->r;
if (defined $header_extra) {
unless (ref($header_extra) eq 'HASH') {
$r->log_error("Extra headers must be a hash ref");
return;
}
}
$self->header($header_extra);
unless ($self->param('js')) {
$r->print($self->no_js);
return;
}
my ($query, $names, $values, $prefix) = $self->expand($self->query);
$r->print($self->output($query, $names, $values, $prefix));
}
sub header { shift->cgi->header( @_ ) }
sub no_js {
my $no_js = <<HTML;
<html>
<head>
<script>
function bodyLoad() {
if (parent == window) return;
var frameElement = this.frameElement;
parent.sendRPCDone(frameElement, "", new Array(), new Array(), new Array(""));
}
</script></head><body onload='bodyLoad();'></body></html>
HTML
return $no_js;
}
1;
__END__
=head1 NAME
Apache2::Autocomplete - Autocomplete service backend via mod_perl
=head1 SYNOPSIS
Given some form that using Google's autocomplete that receives
suggestions from I<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;
}
######################################################
=head1 DESCRIPTION
This module is a mod_perl2 interface to
L<JavaScript::Autocomplete::Backend>, which is a base
class for implementing an autocomplete service
for a form using the Google Suggest protocol.
See L<http://www.google.com/webhp?complete=1&hl=en>
for an illustration of Google Suggest in operation,
as well as
( run in 1.404 second using v1.01-cache-2.11-cpan-e1769b4cff6 )