<PRE>
#!/usr/local/bin/perl
# Free For All Link Script       Version: 2.1
# Created by Matt Wright        mattw@misha.net
# Created On: 5/14/95           Last Modified: 1/29/96
#################################################################
# Define Variables

# The $filename variable represents the system path to your
# links.html file, which will contain all of your links and the
# form to add new links.

$filename = "/home/mattw/public_html/links/links.html";

# $linksurl is the URL to the same file as you listed in
# $filename, except this is the reference that will be used to
# return users to your link file.

$linksurl = "http://your.host.xxx/links/links.html";

# The $linkspl variable specifies the URL to your links.pl
# PERL/CGI script.  This is used as the action for the form if a
# user fails to enter their URL or title.

$linkspl = "http://your.host.xxx/cgi-bin/links.pl";

# This is the path to your system's date command.

$datecom = '/usr/bin/date';

# Done
#################################################################

# Get the Current Date.
$date = `$datecom +"%r on %A, %B %d, %Y %Z"`; chop($date);

# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&amp;/, $buffer);

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   $value =~ tr/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/&lt;([^&gt;]|\n)*&gt;//g;

   # Create an associative array (%FORM) that contains all of the
   # names and values that were input into the form.

   $FORM{$name} = $value;
}

# Send Errors back to the user if they failed to fill in the URL
# or Title portion of the links.html form.

&amp;no_url if ($FORM{'url'} eq 'http://');
&amp;no_url unless $FORM{'url'};
&amp;no_title unless $FORM{'title'};

# Enter our tags and sections into an associative array

%sections =  ( "busi","Business","comp","Computers",
               "educ","Education","ente","Entertainment",
               "gove","Government","pers","Personal",
               "misc","Miscellaneous");

# Determine How Many Links Are Currently in the Link File.
$response = `grep '&lt;li&gt;&lt;a href' $filename`;
@data = split(/\n/,$response);

$i=1;

foreach $line (@data) { # For every line in our data
  $i++;
}

# Open Previous Link File and Put it into one large string to
# manipulate later.

open (FILE,"$filename");
@LINES=&lt;FILE&gt;;
close(FILE);
$SIZE=

@LINES; # Loop through the entire file and if the line
equals # &lt;!--number--&gt; or &lt;!--date--&gt;, it will insert the new
values. # Otherwise, it simply prints the line back into the HTML

file. open
(FILE,"&gt;$filename"); for($a=0;$a&lt;=$SIZE;$a++) {
   $_=$LINES[$a];
   if (/&lt;!--number--&gt;/) {
      print FILE "&lt;!--number--&gt;&lt;b&gt;There are &lt;i&gt;$i&lt;/i&gt; links ";
      print FILE "on this page.&lt;/b&gt;&lt;br&gt;\n";
   }
   elsif (/&lt;!--time--&gt;/) {
      print FILE "&lt;!--time--&gt;&lt;b&gt;Last link was added at ";
      print FILE "$date&lt;/b&gt;&lt;hr&gt;\n";
   }
   else {
      print FILE $_;
   }
}
close (FILE);

open (FILE,"$filename");

while (&lt;FILE&gt;) {
   $raw_data .=  $_;
}

close(FILE);

# Make a normal array out of this data, one line per entry.
# NOTE: This eats up our newline characters, so be sure to add
# them back when we print back to the file.

undef $/;
@proc_data = split(/\n/,$raw_data);

# Open Link File to Output
open (FILE,"&gt;$filename");

foreach $line (@proc_data) { # For every line in our data

   print FILE "$line\n";   # Print the line.  We have to do this
                           # no matter what, so let's get it over
                           # with.

   # If the section tag equals the one the user wishes to add
   # their link to, add it.  Otherwise, just continue.
   foreach $tag (keys(%sections)) { # For every tag
      if ( ($FORM{section} eq $sections{$tag}) &amp;&amp;
         ($line =~ /&lt;!--$tag--&gt;/) ) {

         print FILE "&lt;li&gt;&lt;a href=\"$FORM{'url'}\"&gt;";
         print FILE "$FORM{'title'}&lt;/a&gt;\n";
      }
   }
}

close (FILE);

# Return Link File
print "Location: $linksurl\n\n";

# If the User forgot to enter a URL for their link, then simply
# send them this message, and followup form, which explains that
# they need to fill out everything before they can continue.

sub no_url {
   print "Content-type: text/html\n\n";
   print "&lt;html&gt;&lt;head&gt;&lt;title&gt;NO URL&lt;/title&gt;&lt;/head&gt;\n";
   print "&lt;body&gt;&lt;h1&gt;ERROR - NO URL&lt;/h1&gt;\n";
   print "You forgot to enter a url you wanted added to the ";
   print "Free for all link page.&lt;p&gt;\n";
   print "&lt;form method=POST action=\"$linkspl\"&gt;\n";
   print "&lt;input type=hidden name=\"title\" ";
   print "value=\"$FORM{'title'}\"&gt;\n";
   print "&lt;input type=hidden name=\"section\" ";
   print "value=\"$FORM{'section'}\"&gt;\n";
   print "URL: &lt;input type=text name=\"url\" size=50&gt;&lt;p&gt;\n";
   print "&lt;input type=submit&gt; * &lt;input type=reset&gt;\n";
   print "&lt;hr&gt;\n";
   print "&lt;a href=\"$linksurl\"&gt;Back to the Free for all Link";
   print "Page&lt;/a&gt;\n";
   print "&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;\n";

   # Exit since there was an error.
   exit;
}

# Send out a similar error message if the user forgot to enter a
# title for their link.

sub no_title {
   print "Content-type: text/html\n\n";
   print "&lt;html&gt;&lt;head&gt;&lt;title&gt;NO TITLE&lt;/title&gt;&lt;/head&gt;\n";
   print "&lt;body&gt;&lt;h1&gt;ERROR - NO TITLE&lt;/h1&gt;\n";
   print "You forgot to enter a title you wanted added to ";
   print "the Free for all link page.&lt;p&gt;\n";
   print "&lt;form method=POST action=\"$linkspl\"&gt;\n";
   print "&lt;input type=hidden name=\"url\" ";
   print "value=\"$FORM{'url'}\"&gt;\n";
   print "&lt;input type=hidden name=\"section\" ";
   print "value=\"$FORM{'section'}\"&gt;\n";
   print "TITLE: &lt;input type=text name=\"title\" size=50&gt;&lt;p&gt;\n";
   print "&lt;input type=submit&gt; * &lt;input type=reset&gt;\n";
   print "&lt;hr&gt;\n";
   print "&lt;a href=\"$linksurl\"&gt;Back to the free for all links";
   print "page&lt;/a&gt;\n";
   print "&lt;/form&gt;&lt;/body&gt;&lt;/html&gt;\n";

   # Exit Since there was an error.
   exit;
}
</PRE>