bw-random
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:
Made entirely from recycled electrons.
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 2003 William E. Weinman <http://bw.org/>
#
# 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;
# let's find the filename
my $filename = shift || $ENV{QUERY_STRING} || "taglines.txt";
my $dr_filename = $ENV{DOCUMENT_ROOT} ?
$ENV{DOCUMENT_ROOT} . $filename :
$filename;
# da header.
print "content-type: text/html\n\n"
if $ENV{GATEWAY_INTERFACE} and $ENV{GATEWAY_INTERFACE} eq 'CGI/1.1';
# can we find the file?
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");
}
# corral 'em in an array
my @lines = <$fh>;
# print a random element from the array
print cleanup($lines[int(rand(@lines))]);
exit;
# make it HTML-safe
sub cleanup
{
my $text = shift or return '';
chomp $text if $ENV{GATEWAY_INTERFACE};
$text =~ s/&/&/g;
$text =~ s/"/"/g;
$text =~ s/</</g;
$text =~ s/>/>/g;
return $text;
}
# bad server!
sub error
{
print "Error: " . shift;
exit;
}
|