Perl
  Home arrow Perl arrow Page 7 - Perl 101 (Part 2) - Of Variables And Operators
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 2) - Of Variables And Operators
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2000-06-01


    Table of Contents:
  • Perl 101 (Part 2) - Of Variables And Operators
  • Q
  • 2 2 ...
  • ... Or Two Plus Two
  • Comparing Apples And Oranges
  • Decisions! Decisions!
  • Handling The Gray Areas
  • Miscellaneous Notes

  • 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 2) - Of Variables And Operators - Handling The Gray Areas
    ( Page 7 of 8 )

    Both the "if" and "if-else" constructs are great for black-and-white, true-or-false situations. But what if you need to handle the gray areas as well?

    Well, that's why we have the "if-elsif-else" construct:
    if (first condition is true)
    	{
    	do this!
    	}
    elsif (second condition is true)
    	{
    	do this!
    	}
    elsif (third condition is true)
    	{
    	do this!
    	}
    
    	... and so on ...
    
    else
    	{
    	do this!
    	}

    You can have as many "elsif" blocks as you like in this kind of construct. The only rule is that the "elsif" blocks must come after the "if" block but before the "else" block.
    #!/usr/bin/perl
    
    # movie quote generator
    
    # set up the choices
    print("[1] The Godfather\n");
    print("[2] American Beauty\n");
    print("[3] The Matrix\n");
    print("[4] The Usual Suspects\n");
    print("[5] Casino\n");
    print("[6] Star Wars\n");
    print("Gimme a number, and I'll give you a quote!\n");
    
    # get some input
    $choice = ;
    chomp($choice);
    
    # check input and display
    if ($choice == 1)
    	 {
            print("I'll make him an offer he can't refuse.\n");
            }
    elsif ($choice == 2)
         	 {
            print("It's okay. I wouldn't remember me 
    either.\n");
            }
    elsif ($choice == 3)
            {
            print("Unfortunately, no one can be told what the 
    Matrix is. You
    have to see it for yourself. \n");
            }
    elsif ($choice == 4)
            {
            print("The greatest trick the devil ever 
    pulled was convincing the
    world he didn't exist.\n");
            }
    elsif ($choice == 5)
            {
            print("In the casino, the cardinal rule is to keep 
    them playing and
    to keep them coming back.\n");
            }
    elsif ($choice == 6)
            {
            print("I suggest a new strategy, Artoo: 
    let the Wookie win.\n");
            }
    else
            {
            print("Invalid choice!\n");
            }

    Depending on the data you input at the prompt, the "elsif" clauses are scanned for an appropriate match and the correct quote is displayed. In case the number you enter does not correspond with the available choices, the program goes to the "else" routine and displays an error message.

    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