Loading ...
Sorry, an error occurred while loading the content.
 

Problems with Text Files

Expand Messages
  • anandria
    Hello, I just started to learn Perl for a correspondance course that I m taking, and I m having major problems with text files. The assignment calls for the
    Message 1 of 1 , Apr 1, 2002
      Hello,

      I just started to learn Perl for a correspondance
      course that I'm taking, and I'm having major problems
      with text files. The assignment calls for the survey
      (http://nexus.mohawkc.on.ca/distance/de04/survey.htm)
      to be filled out, and upon submission, the information
      has to be written to the survey.txt file and displayed
      to the screen in HTML format. I had it going in HTML
      format, but it was almost as if the file portion was
      being ignored. I had asked my instructor for help,
      but he got ticked off at me because it wasn't like the
      way it was written in the workbook. So, I've redone
      the page to mimick the one in the workbook and my
      problem is now this: when you click the "Submit"
      button on the survey.htm form, it pulls up a dialog
      box that says "You have chosen to download the file
      'survey.pl' from the following location http://...etc.
      Would you like to open it or save it to your hard
      drive?" I don't get any other output.

      I don't know what's wrong or where to go from here.
      I'll post the code below and I'm hoping that somebody
      out there can help me.

      Thanks in advance,

      Debbie

      ***********************************************

      # survey.pl

      # By: Debbie Russell 467195
      # For: Steven Hernden

      # Calls the FormatData function
      &FormatData

      # Calls the TextFile function
      &TextFile

      # Calls the HTMLOutput Function
      &HTMLOutput

      sub FormatData
      {
      # Gets the information from the the form on the
      previous page
      if ($ENV{'REQUEST_METHOD'} eq 'GET')
      {
      $buffer = $ENV{'QUERY_STRING'};
      }
      else
      {
      read ( STDIN, $buffer,
      $ENV{'CONTENT_LENGTH'});
      }

      @pairs = split(/&/, $buffer);

      # Decodes the URL encoding that is used when it
      gets transferred between pages
      foreach $pair (@pairs)
      {
      ($name, $value) = split(/=/, $pair);
      $name =~ tr/+/ /;
      $name =~ s/%(..)/pack("c", hex($1))/eg;

      $value =~ tr/+/ /;
      $value =~ s/%(..)/pack("c", hex($1))/eg;

      # Discards any include statements that have been
      set to gain access to/information from
      # the server
      $value =~ s/<!--(.|\n)*-->//eg;
      }

      # Returns the recognized values from the previous
      form
      if ($name eq 'filename' && ($value))
      {
      $CONFIG{$name} = $value;
      }
      else
      {
      if ($FORM{$name} && ($value))
      {
      $FORM{$name} = "$FORM{$name}; $value";
      }
      elsif ($value)
      {
      $FORM{$name} = $value;
      }
      }
      }

      sub TextFile
      {
      # Open the file for Appending

      open(SURVEY,">>de04/survey.txt") or die("Can\'t
      Access File");

      foreach $key (sort keys %FORM)
      {
      # Print the value in FORM HASH to data file
      print SURVEY "$FORM{$key}::";
      }

      # print a new line between the additions to the
      file
      print SURVEY "\n";
      close (SURVEY);
      }

      sub HTMLOutput
      {
      # Prints the HTML for the page
      print ("Content-type: text/html\n\n");

      print "<html>";
      print "<head>";
      print " <title>survey.pl</title>";
      print "</head>";

      print "<BODY BGCOLOR=\'#000000\'
      TEXT=\'#COCOCO\'>";
      print "<font face=\'Verdana\'>";
      print "<table border=\'1\' cellspacing=\'1\'
      cellpadding=\'1\'>";

      foreach $key (sort keys %FORM)
      {
      print " <tr>";
      print " <td>" . $key . "</td>";
      print " <td>" . $FORM{$key} . "</td>";
      print " </tr>";
      }


      print "</table>";
      print "</font>";
      print "</body>";
      print "</html>";
      }
    Your message has been successfully submitted and would be delivered to recipients shortly.