Perl
  Home arrow Perl arrow Page 4 - Perl 101 (part 8) - Putting It To The ...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PERL

Perl 101 (part 8) - Putting It To The Test
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 2
    2000-10-02

    Table of Contents:
  • Perl 101 (part 8) - Putting It To The Test
  • Adding Things Up
  • Visitors Welcome!
  • The Code...
  • ...And The Explanation
  • Going Backwards
  • Fortune Cookies
  • You Have Mail!

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Perl 101 (part 8) - Putting It To The Test - The Code...
    (Page 4 of 8 )

    Here's the other half of the puzzle - the CGI script that takes care of the actual storage of form data.
    #! /usr/bin/perl
    # submit.cgi - accepts guestbook data and writes to file
    # define a variable that accepts a value from the form
    $in;
    # assign values to the variable depending on form METHOD
    if ($ENV{'REQUEST_METHOD'} eq "GET") {
    $in = $ENV{'QUERY_STRING'};
    } else {
    $in = <STDIN>;
    }
    # fix URL-encoded strings
    $in =~ s/\+/ /g;
    # all variables are passed to the script as name-value pairs separated by &
    # split the input string on the basis of &
    @detail = split (/&/, $in);
    # display data entered by user again
    print "Content-Type: text/html\n\n";
    print "<html><body>";
    print "<center>";
    print "<table cellspacing=5 cellpadding=5 width=600 bgcolor=#D6D6D6>";
    print "<tr><td align=center colspan=2 width=600><font face=Verdana
    size=2>Thank you for entering the following details in the
    guestbook.</font></td></tr>\n";
    # each name-value pair is stored as an element of an array
    # now take each element of the array and split to form a hash
    # on the basis of the = symbol
    # using the "foreach" loop, we split each element of the array into a
    "temporary" hash
    foreach $details(@detail)
    {
    %details = split (/=/, $details);
    # now extract the name-value pair from the temporary hash to display to the
    user
    # some forward thinking here:
    # since values need to be stored in a text file, create a variable called
    $entry
    # and differentiate the different elements of each guestbook entry by the #
    symbol
    # this comes in useful later, wait and see!
    while (($name, $value) = each %details)
    {
    print "<tr><td align=right width=300><font face=Verdana size=2>Your
    $name:</font></td><td align=left width=300><font face=Verdana size=2>
    $value</font></td></tr>\n";
    $entry .= $value . "#";
    }
    }
    # end the display with a link that allows the user to view other guestbook
    entries
    print "<tr><td align=center colspan=2 width=600><font face=Verdana
    size=1>Click <a href= view.cgi>here</a> to view other
    entries.</font></td></tr></table>";
    print "</body></html>";
    # make things simple by ensuring that every entry in the file is on a
    single line
    # by terminating the entry with a newline
    $entry = $entry."\n";
    # this is where the file write actually happens
    # remember to open the file in "append" mode to avoid losing previous data
    # make sure that you have permission to write to this file
    open (GBOOK, ">> guestbook.txt");
    print GBOOK "$entry";
    close (GBOOK);
    # whew!
    

    Now, how about an explanation?

    This article copyright Melonfire 2000. All rights reserved.

    More Perl Articles
    More By Vikram Vaswani and Harish Kamath, (c) Melonfire


     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway