Tripod
Tripod

   handcrafted

Vol. 1, No. 39
Using the CGI Modules Tripod Gives You


Can we take a moment here to silently appreciate Tripod? Not only do they provide megabyte after megabyte of Web hosting to anyone who asks, and gripping, informative newsletters like this one, recently praised by the New York Review of Books as containing some of the best contemporary American writing, totally free of charge--but also dozens of Web site features over and above the call of duty. Streaming video. Built-in chat functions. And, by popular demand, CGI.

CGI brings a whole new level of power to the Web. (In brief, it accepts and processes input from visitors to your site.) To really really exploit it, you need to write your own scripts in Perl; but, failing that, Tripod also provides a few ready-made scripts that you can just upload and use. For those people who are ready to move beyond the readymades, Tripod ALSO provides a handful of Perl modules that make it easy to get started writing functional scripts.

The modules Tripod provides are six: TripodCGI.pm, TripodDate.pm, TripodInsert.pm, TripodMail.pm, TripodPage.pm, and CGI.pm. The names of the modules provide important clues about what each one is useful for: TripodDate helps with date handling, and so forth. But since they come as a package, there's no point in picking and choosing at this point. Let's install them and then take a look at how to use them.

In order to run a CGI script on Tripod, you need to place the file in your cgi-bin directory. If there are other files that the script needs to access, those files have to be in the cgi-bin directory too. To prevent mishaps, CGI scripts are confined to that one directory. So in order to use Tripod's CGI modules, the first step is to upload them to your cgi-bin directory. You can do that by going to this page.

and clicking "Upload!" That's it: now they're there. With no further delay, let's put them to work. Today we will look at the first couple of modules, and whet your appetites for the others!

TripodCGI.pm is a basic form-handling module. It offers a function called param() that grabs form input by name. So basically, if you have an HTML form and one of the fields is called "yourname", you can use param() to pass the contents of that form field to a CGI variable. Here is a snippet of code that does just that (comments follow # signs):

#!/usr/local/bin/perl
require TripodCGI; # this is necessary to invoke the TripodCGI module,
			     # which is what we want to use here.
$CGI = new TripodCGI;	     # this creates an object via which we can
				    # use TripodCGI functions.
$name = $CGI->param('yourname');    # this uses the param() function
			# to assign the contents of "yourname"
			# to the variable $name.
print "Content-Type: text/html\n\n"; # send an HTTP header
print "<html><body>";	# send the beginning of our page
print "<h1>$name</h1>";	# print the value of $name, in big type
print "</body></html>";	#close the tags; the end.

In order to realize this, you'll need to save the above code as a script in your cgi-bin directory, and then have your HTML form's action attribute point to it. For example, if you save the script as printname.pl, the form tag should look like this: <FORM METHOD="POST" ACTION="./cgi-bin/printname.pl">

[see it in action!]

If you want to grab all the fields from a form into an array variable, just say:

@allfields = $CGI->param();

and every form input will be passed to an array called @allfields.

The other function that the TripodCGI module provides is totally unrelated to param() -- it's called redirect(), and basically redirect is what it does. With minimal fuss, it sends the user on to a new page that you specify. It takes one argument: the destination URL. The syntax looks like this:

$CGI->redirect('http://www.webmonkey.com');

That will send the user automatically to Webmonkey.com. It is permissible (and useful) to put a variable inside the quotes instead of a hard-coded URL, so you can send your users to different pages depending on who they are, how they rate on your questionnaire, or whatever factors you choose.

Next, let's take a look at the TripodDate module. This offers a large double handful of functions having to deal with dates, including doesHeReallyLikeMe(), askHerOut(), and doubleDate(). No, that's a joke. TripodDate.pm deals with calendar dates. For the other kind you're on your own.

One of the easy and useful things TripodDate can do is return the current date. There are four separate functions that do this: currentDate(), currentYear(), currentMonth(); and currentDay(). The first one returns the whole date; the others each give you just part of the date, as you might expect. Use them as follows (first require-ing TripodDate and creating a $DATE object like you created $CGI for TripodCGI):

$thismonth = $DATE->currentMonth(); $fulldate = $DATE->currentDate();

Running that preceding line of code on October 18, 2000 would set the variable $thismonth to "10", and the variable $fulldate to "10/18/2000".

What else? There are a few functions for automatically converting dates from one format to another, but that's not terribly interesting. Let's see. How about dateIsPast()? That, as you hopefully can guess, tests a particular date to see whether it's past or not. It returns 0 if the date has not happened yet, and 1 if it has. You can use it like this:

if ($DATE->dateIsPast($yourbirthday)) {
   print "Sorry I missed your birthday.\n";
  }

One more neat function is addDaysToDate(). This takes two arguments: the date in question, and the number of days to add to it. (To subtract, make the second argument a negative number.) So:

$todaysdate = $DATE->currentDate();
$60daysago = $DATE->addDaysToDate($todaysdate, '-60');

Not too hard. You will find that addMonthsToDate works exactly the same way.

Well, we're out of time for this week. Thank you all for coming, and be sure to tune in next time for our very special guests TripodInsert.pm and TripodPage.pm!!!

HINTS, POINTERS, AND TIPS 'O THE TRADE:

Once you have uploaded the Perl modules, take a look at them to see how they work and to read the comments in the code. If, when you click on a module in the Tripod File Manager, it's all run together without line breaks, try viewing source on that page to bring the line breaks back.

Just play around. These are the kind of simple but powerful tools that are easiest to learn by experimentation. Try feeding the output of one function into the input of another function. See what sorts of neat things you can do.

When cleaning CDs, wipe from the center to the edge in a straight line, NEVER around and around in a circular direction. That is to say: go radially, not circumferentially.

RESOURCES: Write your own CGI scripts

Intro to CGI

Webmonkey's guide to CGI

Webmonkey: CGI Scripts for Fun and Profit

 
Subscribe/Unsubscribe

Handcrafted Archive

2002 March
February
January
2001 December
November
October
September
August
July
June
May
April
March
February
January
2000 December
November
October
September
August
July
June
May
April
March
February
January
1999 December
November
October
September
August
July
June
May
April
March
February



    Tripod: Home | Site Map | About Tripod | International | Tripod Help | Report Tripod Abuse | Members | Angelfire Members

     » Lycos.com  © Copyright 2008, Lycos, Inc. Lycos is a registered trademark of Lycos, Inc. All Rights Reserved.
     About Lycos | Help | Jobs | Advertise

     Your use of this website constitutes acceptance of the Lycos Privacy Policy and Terms & Conditions