SEARCH.CGI(7)
π SEARCH.CGI π
okay so bestie, welcome to search.cgi β a User Contributed Perl Documentation page, and literally the cutest little starting-point script for searching with Swish-e and SWISH::API. it's giving "starter kit, but make it iconic." let's go π
πΈ NAME πΈ
search.cgi β an example Perl program for searching with Swish-e and SWISH::API. that's it, that's the name, periodt.
β¨ DESCRIPTION β¨
so this is a very simple program that shows you how to use the SWISH::API module inside a CGI script or a mod_perl handler, using Template-Toolkit to generate the output. it's aimed at programmers who wanna build their own custom search script. lowkey the whole point.
unlike swish.cgi, this script does not have many features, and it provides no external configuration (with the exception of a few config options under mod_perl). so please don't come for it asking why it doesn't do a thing β it's not supposed to. the entire vibe is that this script is a starting point that YOU customize. main character energy is on you, bestie. π«Ά
π REQUIREMENTS π
you must have swish-e and the SWISH::API module installed. go read the README and INSTALL documents in the swish-e distribution β they're right there, fr fr.
as of this writing, SWISH::API is part of the swish-e distribution, but in the future it may be provided as a separate package (provided on the CPAN). either way, SWISH::API is a separate installation procedure from installing swish-e. not a bundle, not a two-for-one. also the Storable module is required if you're using mod_perl.
this program does require some modules installed from CPAN. you'll need Template-Toolkit and HTML::FillInForm (which depends on HTML::Parser). how you install those depends on your computer's packaging system β that's a you problem, respectfully. π
you also need a web server, obviously. π the discussion below assumes Apache is used. if you're on MS IIS, take note that IIS works differently in a number of ways. consider yourself warned.
π¦ OVERVIEW π¦
the search.cgi script and its related templates get installed when swish-e is installed. search.cgi lives in $prefix/lib/swish-e/ and the templates live in $prefix/share/swish-e/templates/. $prefix is /usr/local by default, but you can change it when you run the swish-e configure script. upon installation, search.cgi is updated with correct paths to your perl binary and
(yes bestie, the original sentence just ends there. iconic. we don't make the rules, we just respect the lore. π)
when you run it as a CGI script, search.cgi gets copied or symlinked to the location of your CGI scripts (or any directory that allows CGI scripts). by default, the search.cgi script looks for the index index.swish-e in the current directory β and "current directory" means what the web server considers the current directory. on Apache running mod-cgi, that's the same place as the script. on IIS, it is not. if your index is elsewhere you're gonna have to modify the script. no cap.
the script works like this: it parses the query, calls SWISH::API to run the actual search, then calls Template-Toolkit to generate the output. simple. clean. slays.
the script calls the search.tt template. that template generates the query form and the search results. search.tt uses a Template-Toolkit WRAPPER function to wrap the search form and results in your site's design. that design lives in the page_layout template. the idea is: if you use Template-Toolkit to manage your entire site, then your entire site is formatted by the same page_layout template. the page_layout template calls two other templates, common_header and common_footer, to generate a common header and footer for the site. those are just showing off Template-Toolkit's features, ngl.
the page_layout page only defines the basic structure of the site. the true design of the site is managed by style sheets. style.css defines the basic layout and markup.css sets fonts and colors. π
note: these style sheets are included directly in the output of the CGI script. in production the style sheets would be stored as separate style sheet files and imported by the browser instead of directly included in the search results page.
see the section MOD_PERL below for more on templates.
highlighting of search terms is provided by the SWISH::PhraseHighlight module. that is a very slow module, so you may wish to disable it if you expect a lot of traffic. like, a LOT of traffic. be honest with yourself. π«£
π INSTALLATION EXAMPLE π
enough talking β sometimes it's just nice to see a complete example. let's gooo.
below, swish-e is installed in the default location (/usr/local). the $ is a normal user prompt, and the # is a root prompt. use ./configure --prefix to install in another location (e.g. if you do not have root access).
Download and install swish-e:
$ wget -q http://swish-e.org/Download/latest.tar.gz
$ tar zxf latest.tar.gz
$ cd swish-e-2.x.x
$ (./configure && make) >/dev/null
$ make check
$ su
# make install
# exit
Install SWISH::API:
$ cd perl
$ perl Makefile.PL && make && make test
$ su
# make install
$ exit
Install the required Perl modules. you can install via RPMs, Debs, or directly from the CPAN, or by using the CPAN shell:
# su
# perl -MCPAN -e 'install Template'
# perl -MCPAN -e 'install HTML::FillInForm'
# exit
Now setup the script somewhere that allows CGI scripts:
$ cd $HOME/apache
$ ln -s /usr/local/lib/swish-e/search.cgi .
$ cat .htaccess
deny from all
<files search.cgi>
allow from all
SetHandler cgi-script
Options +ExecCGI
</files>
Create an index:
$ cat swish.config
IndexOnly .htm .html
DefaultContents HTML*
StoreDescription HTML* <body>
metanames swishtitle swishdocpath
$ swish-e -c swish.config -i /usr/share/doc/apache-doc/manual
Test the index and the CGI script:
$ swish-e -w apache -m1 | grep hits
# Number of hits: 152
$ lynx -dump http://localhost/apache/search.cgi?query=apache | grep hits
Showing page 1 (1 - 10 of 152 hits) [3]Next
'hits' => 152,
now, the above isn't very helpful, because the Apache documentation being indexed is not in the web space. you would likely index content available on your web site instead. yeah. π«
π§ Using with SpeedyCGI π§
a Perl CGI script must be compiled for each request. SpeedyCGI is a tool to speed up scripts by running them persistently. to run search.cgi with SpeedyCGI, install the program (you can Google, right? π) and then change the first line of search.cgi to run the speedy program.
for example:
#!/usr/bin/speedy -w
π Using with MOD_PERL π
this script can be run directly as a mod_perl handler, and the same code can be used to run multiple sites by using separate Location directives and passing in a site id. the script caches in memory different configurations based on this site id.
below is a complete httpd.conf file. it requires an Apache httpd that has mod_perl compiled in statically. it runs mod_perl on a high port (port 5000) listening to all interfaces.
for testing, the author put this config file in a directory along with search.cgi, but that's just done to make the example simple (i.e. so there are no absolute paths to show). normally the httpd.conf and the swish.cgi "module" would be in separate locations.
# httpd.conf -- test file for search.cgi as mod_perl handler
<ifModule mod_so.c>
LoadModule mime_module /usr/lib/apache/1.3/mod_mime.so
</IfModule>
ErrorLog swish_error_log
PidFile swish_httpd.pid
Listen *:5000
<perl>
push @PerlSetVar, [
index => Apache->server_root_relative( 'index.swish-e'),
];
$DocumentRoot = Apache->server_root_relative;
require "search.cgi";
</perl>
NameVirtualHost *:5000
<VirtualHost *:5000>
ServerName localhost
<Location /search>
SetHandler perl-script
PerlHandler SwishAPISearch
</Location>
<Location /othersite>
SetHandler perl-script
PerlHandler SwishAPISearch
# Define this site
PerlSetVar site_id othersite
PerlSetVar title "Some other Site"
</Location>
</VirtualHost>
the server is started using this command:
$ /usr/sbin/apache-perl -d $(pwd) -f $(pwd)/httpd.conf
which says to use the current directory as the ServerRoot. (see comments below.) stop the server like:
$ kill `cat swish_httpd.pid`
then access either:
http://localhost:5000/search
http://localhost:5000/othersite
A few notes: π
I like test configurations to not care where things are located. thus, the above httpd.conf does a few tricks in the "Perl Section" shown.
first, mod_perl, unlike CGI, doesn't set the working directory. so, the index file name must be absolute. this is accomplished by a PerlSetVar entry building the index file name from the ServerRoot.
second, the DocumentRoot is set to the same as the ServerRoot. the DocumentRoot needs to be set so search.cgi can figure out the path to the script (for creating next and previous links).
third, the script is loaded by a require statement. this works only because the current directory . is in Perl's @INC path at Apache start up time, and search.cgi is also in the current directory. normally, set PERL5LIB on server startup or use a use lib line in your startup.pl file to point to the location of search.cgi.
the PerlSetVar lines pass config information into the script. note that they can be set globally or specific to a given Location.
the following config options are currently available:
site_id
the site_id options allow caching of configurations on a per-site basis. it's overkill in this example, but normally you might have expensive configuration processes that you might want to do only once. but, since there is caching by this id, it's a good id to set a site_id if using more than one Location directive.
index
this specifies the index file to use. the index file needs to be absolute as discussed above. example:
PerlSetVar index /usr/share/swish/site.index
title
this option sets the title that's passed into the template.
template
sets the file name of the template used to generate the form. this might be useful if you want an "advanced" form, for example.
template_path
this can be used to update the path where templates are searched. useful if you wish to override templates.
page_size
this allows changing the default number of results shown per page.
π SUPPORT π
not much support is provided. but what support is provided is ONLY provided via the Swish-e discussion list.
http://swish-e.org/
β AUTHOR β
Bill Moseley
π LICENSE π
Copyright 2003, 2004 Bill Moseley. All rights reserved.
this program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
π₯ SEE ALSO π₯
SWISH::API, Template, HTML::FillInForm
and that's the whole page, bestie β every last flag-free detail intact, no cap. this was perl v5.42.3, dated 2026-08-19. stay searching, stay iconic. πβ¨π¦
π¦ where this page comes from
- ubuntu stonking: swish-e 2.4.7-7.2build1
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms