I am trying to write auto-complete for my dictionary project hosted on SourceForge.
Unfortunately, Perl on SF does not have JSON module installed. How can I workaround this, without using module itself? As a model I took JQuery UI automcomplete guide on jensbits.
Here is Perl code that is supposed to query text and send to the auto-complete module
#!/usr/bin/perl
use strict;
use CGI;
use DBI;
use JSON;
# HTTP HEADER
print "Content-type: application/json; charset=iso-8859-1\n\n";
my $dbh = DBI->connect("DBI:mysql:database;mysql_read_default_file=/path/.my.cnf");
$dbh->do("set character set utf8");
$dbh->do("set names utf8");
my ($p, $sth, @query_output);
$p = new CGI;
my $term = $p->param('term');
$sth = $dbh->prepare(qq{SELECT trim(both char(13) FROM article) AS value, definition FROM dict WHERE article like ?;});
$sth->execute('%'.$term.'%');
# LOOP THROUGH RESULTS
while ( my $row = $sth->fetchrow_hashref ){
push @query_output, $row;
}
#CLOSE THE DATABASE CONNECTION
$dbh->disconnect()
# JSON OUTPUT
print JSON::to_json(\@query_output);
As far as I know,
JSONpackage doesn’t need to be installed in system.You can just download latest JSON distribution, unpack, upload it to your hosting and let Perl know, where to grab the code: just add
before
If you don’t know absolute path or want to make the code more portable, you can use FindBin package, to find JSON distro dir relative to your binary:
I hope this solves your problem.