Perl
  Home arrow Perl arrow Page 7 - Perl 101 (Part 2) - Of Variables And O...
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 
IBM Developerworks
 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 2) - Of Variables And Operators
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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

    Dell PowerEdge Servers

    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

    - 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 2 hosted by Hostway