![]() |
America's Town Square |
A Free Link to Your Random Widgets,
What is a Random Widget?,
Examples applications,
Server Side Includes,
Storing Your Widgets,
Widget Files,
Widget Arrays,
A Perl for Every Page,
Bring it all Together,
Download widgets.pl now,
Epilogue
If you decide to use random widgets, please let us know where and how and
we'll include a description of your use and a link to your page.
Widgets, widgets, everyone loves random widgets...well at least most people like widgets. A widget might be almost anything. The basic idea is to select an item a random and have it display somewhere in your HTML document.
Use a widget generator to insert a random thought for the day in your Web home page. Or, insert a tip for the current session. You can get really creative and change the look and feel of your page every time some enter your inner sanctum by selecting from a group of body tags.
No matter what kind of random widget you use, the underlying concept is the same. Generate a random number and select a widget corresponding to the number. First a word of caution.
Before you get carried away with creativity, check with your Internet
Service Provider as see if they allow cgi scripts in general and server
executable side includes specifically. At the heart of random Widgets is
a line in your HTML document looking something like the following.:
<!--#exec cgi="/cgi-bin/thoughts.pl"-->The America's Town Square home page has two of these widget generators, one for tips and one for a "Not so serious thought." You would insert the following at the point you want the random thought:
</FONT><FONT SIZE=6 COLOR=YELLOW>On a not so serious note...</FONT> <FONT SIZE=+1> <PRE> <!--#exec cgi="/cgi-bin/thoughts.pl"--> </PRE></FONT>The PRE tags are to preserve the line breaks in the text file holding the thoughts. The path to your Perl script may need modification based upon your provider's configuration.
The use of SSI put additional demands on a Web server. When a server is SSI enabled, there is extra processing required to parse the served document. Many servers can be configured to parse only those HTML document containing the extension "shtml". Again, check with your Internet Service Provider to determine if they require the shtml extension on documents contain server side includes.
There are two ways of storing your group of widgets:
Widget files take the form of a group of widgets separated by some type of
delimiter. I delimiter is a character, or group of characters, that would
NEVER appear within the list of widgets. Let's take a look at a portion of
the file used to generate the "Not so serious thought" and the ATS home
page.
Computers in the future may weigh no more than 1.5 tons. --Popular Mechanics magazine, 1949. (1) Clothes make the man. Naked people have little or no influence on society. -- Mark Twain (1) The gene pool could use a little chlorine. (1) It isn't necessary to be rich and famous to be happy.... It's only necessary to be rich. --Alan Alda (1)It this case the delimiter is "(1)" (without the quotes.) Adding is new thought -- or any other widget -- is simple:
The widgets can be any size. If you actually display the widgets between the PRE tags, the individual line formatting will be preserved. You can use any delimiter you prefer, just make sure they are all exactly the same and additionally, make sure the delimiter never appears within the body of a widget.
As mentioned above, storing widgets in an array in within script itself is
very efficient since the script does not have to read an external file.
In programming parlance, an array is a grouping of similar "things," in
this case widgets.
A creative use of the widget concept is to store a small group of body tags in an array. When executed by your HTML document the script produce a different look and feel for the same page. The effect can be quite impressive. Be prepared to answer "how did you do that" questions.
In the following code snippet, the array @body is loaded with the various body tags. In Perl, a single array element is referenced as a scalar variable with a subscript enclosed between the "[]" pair. The first element in an array is designated [0]. (We computer types don't waste numbers.) The first element of array @body would therefore be, $body[0], the second $body[1], and so on. Here is a set of four different body tags. The third tag is just the plain old body tag.
$body[0] = # Blue from main index page "<BODY BGCOLOR=\"01017E\" TEXT=\"FFFFFF\" VLINK=\"FF0000\" LINK=\"FFFF00\">"; $body[1] = # Red from what's hot "<BODY BGCOLOR=\"8B0518\" TEXT=\"FFFFFF\" LINK=\"FFFF55\" VLINK=\"0000FF\">"; $body[2] = "<BODY>"; # Default colors $body[3] = # Teal background "<BODY BGCOLOR=\"007070\" TEXT=\"FFFFFF\" LINK=\"FF0000\" VLINK=\"FFFF00\">";Make sure your choice of text and link colors work together. If you simply select a background color at random you run the danger of selecting a black background using black text. Makes the page difficult to read.
An example of changing background colors can be found on the LBI & Ocean County, NJ Online home page. There are two other random Widgets on the page, an entry from the guest book and one the URL hall of fame.
Let's look at little snippets of Perl code. I'm making the assumption your
host server is using Unix. If not, check with one of your local gurus for
tweaking. If you're still with me at the end of this tutorial, you can
download the entire script.
The first line in any Perl script must let Unix know this is a Perl script, not a shell script. It contains the path to Perl on your system and my be different than what is show below. You can check your path to Perl using the Unix "which" command. At the Unix prompt, enter:
which perl
Enter the following line starting with the "#!" and your path to Perl.
#!/usr/bin/perl
The script next informs the server what is about to be sent.
print "Content-type: text/plain\n\n";
Next, some system constants.
# Script variables, change if assumptions are wrong. $WidgetDelimiter = "\\(1\\)"; # Assumes widgets are separated by (1) $WidgetExtension = ".txt"; # Assumes widget file extension of txt $WidgetPath = "data/"; # Assumes path data under the script pathThe above constants assume your widgets are delimited by (1), the widget file has an extension of "txt" (lower case) and the widget file is in a directory named data which is a subdirectory of the directory holding the script. You cannot have the text file in a directory where the server expects to find executable cgi scripts.
I'm now going to get cute. I like to have my scripts serve multiple masters. The widgets.pl script will word for different HTML documents. If an HTML document is name tips.html, the script will look for a widget file name tips.txt. If the HTML file is thoughts.html, the script will look for thoughts.txt. There are several advantages to doing this. If you want to make a change to the script, you only have to do it in one place.
The next few lines accomplish the widget naming task. If you don't want to use this extraordinarily creative scheme, simply comment (#) out the first group of statements and hard code you file name in the last statement.
$WidgetFile = $ENV{'DOCUMENT_URI'}; # Get name of calling HTML file
$_ = $WidgetFile; # Set up for regular expression
$WidgetFile =~ m!.*/(.*)\.!; # Put extensionless file name in $1
# Add "data/" to path and ".txt" to file name
$WidgetFile = "$WidgetPath$1$WidgetExtension";
The next group of instructions open the widget file, transfer it to
the @widget array (each line is an element), and then put it all into
one big scalar variable ($bigdata).
open(WIDGET, "$WidgetFile") || # Open the file die "Cannot open $WidgetFile\n"; # Bummer if you get here @widget =Now, place each individual widget into an element of array @widget and count the number of elements.; # Read up file into array $bigdata = join("",@widget); # Transfer file to scalar variable
@widget = split(/$WidgetDelimiter/,$bigdata); # Break widgets into array $WidgetCount= @widget; # Number of widget elementsMoving right along, seed the random number generator and generate a random number. The Perl rand(range) function returns a random number between 0 and range minus one. If range is 4, rand will return a random number greater than or equal to zero and less than 4. This is exactly what we want since an array with four elements will be subscripted by [0], [1]. [2], and [3]. The int function removes the fraction portion of the random number.
srand(); # Seed random number generator $WidgetIndex = int(rand($WidgetCount)); # Get random number for arrayAnd now, a little drum roll, display the widget.
print "$widget[$WidgetIndex]"; # Display the random widget
Lets bring it all together. Take the following steps:
I would appreciate any feed back you care to offer on this tutorial.
Don't forget to let me know if you use a random widget so we can give
you a free link. Also, let me know what else you would like to see on
these pages.
Thanks,
Urb LeJeune president America's
Town Square.

![]() | up to date with our "free stuff", special offers, and other goodies. |
![]() | Please sign our
self-updating guestbook How about some free stuff from ATS? |
Please send our
crotchety Webmaster some mail.


Copyright ©1996 America's Town Square
15 Hunter Drive
Tuckerton, NJ 08087
(609) 294-0320