Perl
  Home arrow Perl arrow Page 2 - Perl 101 (part 7) - CGI Basics
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 7) - CGI Basics
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 2 stars2 stars2 stars2 stars2 stars / 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:
      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

    Route your faxes to your email inbox. Private, secure fax numbers available from CallWave. Choose your fax number.

    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

    - 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