Main Menu
Contact Bill FAQ
Linked In
Hire Bill
Bill’s Music
BW Whois
Boulder Pledge
CGI Scripts
CMS Project
Music DB
Creative HTML
The CGI Book
Perl Book
The End
· Sponsors · video tutorials
Rheumatoid Arthritis
Site Design: Bill Weinman

CGI Scripts by Bill Weinman


This script displays a random tagline from a text file. It should be run from a server-side include.

On an Apache server you put something like this in your page:

  <!--#include virtual="/path-to/bw-random.cgi?taglines.txt" -->

The result should be something like this:

Very funny, Scotty. Now beam down my clothes.

You need to supply your own file of taglines. Put one on each line, like this:

You're just jealous because the voices only talk to me.
Give pizza chants.
I didn't fight my way to the top of the food chain to be a vegetarian.
Very funny, Scotty. Now beam down my clothes.
2 + 2 = 5 for extremely large values of 2.
Made entirely from recycled electrons. 
No electrons were harmed in the making of this message. 
Ezz beeg trouble for moose and squirrel...
"My arm!", said Captain Hook offhandedly.
"I think not," said Descartes, and he promptly disappeared.
Thesaurus--An ancient reptile with a large vocabulary.

Here is the source code for bw-random.cgi. Copy/paste this code into your text editor, and make sure that the line

#!/usr/bin/perl -w
is the first line in the file.

#!/usr/bin/perl -w
# bw-random.cgi
# Display a random witicism from a text file.
# May be used as a CGI (via SSI) or from a command line.
# Copyright 1995-2010 William E. Weinman <>
# The author grants permission to use and modify this program as you like. 
# All other rights and ownership are reserved by the author. 

use strict;
use IO::File;

# bw's patent-pending VIRTUAL_DOCUMENT_ROOT kludge:

my $filename = shift || $ENV{QUERY_STRING} || "taglines.txt";
my $dr_filename = $ENV{DOCUMENT_ROOT} ?
  $ENV{DOCUMENT_ROOT} . $filename :

print "content-type: text/html\n\n"

my $fh;

if(-f $filename) {
  $fh = new IO::File("<$filename") or error("Cannot open $filename\n");
elsif(-f $dr_filename) {
  $fh = new IO::File("<$dr_filename") or error("Cannot open $dr_filename\n");
else {
  error("cannot find file $filename\n");
my @lines = <$fh>;

print cleanup($lines[int(rand(@lines))]);

sub cleanup
my $text = shift or return '';
chomp $text if $ENV{GATEWAY_INTERFACE};
$text =~ s/&/&amp;/g;
$text =~ s/"/&quot;/g;
$text =~ s/</&lt;/g;
$text =~ s/>/&gt;/g;
return $text;

sub error
print "Error: " . shift;

A few bits shy of a word.
At BHG Worldwide Headquarters it is now eleven past noon, on Wednesday, 24 July 2024.