Perl
  Home arrow Perl arrow Page 6 - Perl 101 (part 7) - CGI Basics
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
PERL

Perl 101 (part 7) - CGI Basics
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 4
    2000-09-25


    Table of Contents:
  • Perl 101 (part 7) - CGI Basics
  • Meet Donald Duck
  • Open Sesame
  • Perl And CGI
  • A Cure For Low Self-Esteem
  • GETting Your Form To Work

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Perl 101 (part 7) - CGI Basics - GETting Your Form To Work
    ( Page 6 of 6 )

    The CGI environment comes with a set of pre-defined variables that can assist in the task of developing server-side scripts. Here's a list of the important ones:

    $ENV{REMOTE_ADDR} The address of the client machine
    $ENV{REMOTE_HOST} The host name of the client machine
    $ENV{REQUEST_METHOD} The method used by a form to request data
    $ENV{HTTP_USER_AGENT} The client browser
    $ENV{QUERY_STRING} The query string passed if the GET method is used

    Our next few examples will give you some idea of how these, and similar variables, can be used within your scripts.
    #! /usr/bin/perl
    
    $ip = $ENV{REMOTE_ADDR};
    $browser = $ENV{HTTP_USER_AGENT};
    print "Content-Type: text/html\n\n";
    print "<html><body><font face=Arial>Your IP address is $ip and your browser
    is $browser</font></body></html>";
    

    And now, when you browse to this page, you'll see some information on your IP address and browser version.

    Our next example demonstrates how a Perl/CGI script can be used to process data submitted via a form. Here's the form...
    <html>
    <head>
    <basefont face=Arial>
    </head>
    <body>
    <form action=http://localhost/readform.cgi method=GET>
    Enter your first name: <input type=text length=20 name=name><br>
    <input type=submit value=Submit>
    </form>
    </body>
    </html>
    

    ...and here's the CGI script that receives and processes the data.
    #!/usr/bin/perl
    
    # readform.cgi
    # reads form data and generates a page
    
    # for GET data
    if ($ENV{'REQUEST_METHOD'} eq "GET")
    {
    $yourname=$ENV{'QUERY_STRING'};
    }
    # for POST data
     else
    {
    $yourname = <STDIN>;
    }
    
    # Remove spaces if any
    $yourname =~ s/\+/ /g;
    
    # split form data and store in hash
    %details = split (/=/, $yourname);
    
    # generate page
    print "Content-Type: text/html\n\n";
    print "<html><body>";
    
    while (($name, $value) = each %details)
    {
    print "Thank you for your submission, $value!\n";
    }
    
    print "</body></html>";
    

    The script above will accept data from the HTML form and store it in a variable called $yourname. The manner in which data is submitted by the form(GET or POST) decides the manner in which it is assigned to the variable; the QUERY_STRING environment variable is used to make this decision.

    If the text entered into the form contains spaces, the spaces are replaced with + characters when the form is submitted - this needs to be reversed via a regex. For example, if you enter the name "Luke Skywalker" into the form, the URL string will look like this:

    http://localhost/cgi-bin/readform.cgi?name=Luke+Skywalker

    Next, the name-value pairs are split apart on the basis of the = sign, and are assigned to their respective places in the hash %details. This hash is then used to print the name in the result page, which is also generated by the same script.

    Thus, the CGI environment can be used to accept data from one Web page, process it or transfer it to another, and generate a new page containing dynamically-generated output. This is the basis of using CGI to create dynamic Web sites.

    In the next issue of Perl 101, we'll delve deeper into CGI, with a look at some simple CGI programs that can be used to track hits on your Web site, or store visitor comments. Don't miss it!

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

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek