16 my $sth= $dbh->prepare("SELECT archipelago,islandid,islandname
17 FROM islands WHERE islandname LIKE ?
18 UNION ALL SELECT DISTINCT archipelago,NULL,archipelago
19 FROM islands WHERE archipelago LIKE ?");
21 my (@results, $canontext);
22 my ($output, $output_wrong);
24 if ($format =~ 'json') {
25 $r->content_type($ctype or $format);
26 $output= sub { print to_json({
28 show => encode_entities($canontext),
30 $output_wrong= sub { print to_json({
32 show => encode_entities($_[0]),
36 foreach my $each (split m#[/|,]#, $string) {
37 $each =~ s/^\s*//; $each =~ s/\s*$//; $each =~ s/\s+/ /g;
38 next if !length $each;
40 my $nrows= $sth->execute($pat,$pat);
42 my $msg= sprintf $_[0], encode_entities($each);
43 $output_wrong->($msg);
46 my $results= $sth->fetchall_arrayref();
48 $err->('no island or arch matches "%s"');
49 } elsif (@$results > 3) {
51 } elsif (@$results > 1) {
52 my @m= map { $_->[2] } @$results;
53 $err->('ambiguous island or arch "%s", could be '.
56 push @results, $results->[0];
59 $canontext= join ' | ', map { encode_entities($_->[2]) } @results;