Perl
  Home arrow Perl arrow Page 2 - 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 - Meet Donald Duck
    ( Page 2 of 6 )

    So far, you've used two different types of Perl variables - the scalar and the array. However, unbeknownst to you, Perl also comes with a third type of variable - the hash.

    One of the significant features of an array is that array values can only be accessed via a numerical index. This implies that if you need to access an element of the array, you need to first know its exact location in the array. Since this can get complicated with large arrays, Perl offers you a simpler way to access array values, using easy-to-remember "keywords" or "keys".

    Thus, a hashes is a species of Perl variable which allows you to define an array of key-value pairs. Take a look:


    %myhero = ("fname" => "Donald", "lname" => "Duck");
    

    The Perl statement above will create a hash named "myhero", which consists of two name-value pairs. The first key is "fname", and it points to the value "Donald", while the second is "lname" and it points to "Duck".

    You can also write the statement above like this:
    %myhero = ("fname", "Donald", "lname", "Duck");
    

    Accessing the elements of a hash is equally simple - in the example above, the notation
    $myhero{"fname"}
    

    will return the first value of the hash ("Donald"), while
    $myhero{"lname"}
    

    will return the second value ("Duck").

    The rules following hash names are the same as those for scalar and array variables - a hash name begins with a % symbol, followed by an alphabetic character, which may be followed by one or more numbers or letters. Hashes also have their own "space" in Perl.so the variables $duck, @duck and %duck are not treated as one and the same.

    This article copyright Melonfire 2000. All rights reserved.{mospagebreak title=Heroes Of The Silver Screen} Here's a simple program that demonstrates how hashes can be used:
    #!/usr/bin/perl
    
    # define a hash
    %director = ("1995" => "Mel Gibson", "1996" => "Anthony Minghella", "1997"
    => "James Cameron", "1998" => "Steven Spielberg", "1999" => "Sam Mendes");
    
    # print
    print "The Best Director Oscar in 1995 went to $director{1995}\n";
    print "The Best Director Oscar in 1996 went to $director{1996}\n";
    print "The Best Director Oscar in 1997 went to $director{1997}\n";
    print "The Best Director Oscar in 1998 went to $director{1998}\n";
    print "The Best Director Oscar in 1999 went to $director{1999}\n";
    

    And here's the output:
    
    The Best Director Oscar in 1995 went to Mel Gibson
    The Best Director Oscar in 1996 went to Anthony Minghella
    The Best Director Oscar in 1997 went to James Cameron
    The Best Director Oscar in 1998 went to Steven Spielberg
    The Best Director Oscar in 1999 went to Sam Mendes
    

    In the example above, a hash has been used to store a bunch of name-value pairs, and a print() function has been used to display them.

    You can also use the alternate hash notation if you prefer.
    
    # define a hash
    %director = ("1995", "Mel Gibson", "1996", "Anthony Minghella", "1997",
    "James Cameron", "1998", "Steven Spielberg", "1999", "Sam Mendes");
    



    This article copyright Melonfire 2000. All rights reserved.

     
     
    >>> 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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek