BW
Main Menu
Home
Contact Bill
lynda.com FAQ
Facebook
Linked In
Twitter
AMTP
Bio
Hire Bill
Bill’s Music
BW Whois
Boulder Pledge
UBE-Related
Blog
CGI Scripts
CMS Project
Music DB
Creative HTML
The CGI Book
Perl Book
BillyDos
Privacy
The End
· Sponsors ·
lynda.com video tutorials
amazon.com
Rheumatoid Arthritis
Site Design: Bill Weinman

CGI Scripts by Bill Weinman

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:

Ezz beeg trouble for moose and squirrel...

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 <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;

# bw's patent-pending VIRTUAL_DOCUMENT_ROOT kludge:
$ENV{DOCUMENT_ROOT} = $ENV{VIRTUAL_DOCUMENT_ROOT} if $ENV{VIRTUAL_DOCUMENT_ROOT};

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

print "content-type: text/html\n\n"
  if $ENV{GATEWAY_INTERFACE} and $ENV{GATEWAY_INTERFACE} eq 'CGI/1.1';

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>;
$fh->close;

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

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;
exit;
}


To err is human. To moo, bovine.
At BHG Worldwide Headquarters it is now quarter past four, on Friday, 26 May 2017.
·/.