Perl
  Home arrow Perl arrow Page 6 - 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 
 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 - Decisions! Decisions!
    (Page 6 of 8 )

    Why do you need to know all this? Well, comparison operators come in very useful when building conditional expressions - and conditional expressions come in very useful when adding control routines to your code. Control routines check for the existence of certain conditions, and execute appropriate program code depending on what they find.

    The first - and simplest - decision-making routine is the "if" statement, which looks like this:
    if (condition)
    {
    do this!
    }

    The "condition" here refers to a conditional expression, which evaluates to either true or false. For example,
    if (bus is late)
    {
    con Dad into giving you a ride
    }

    or, in Perl language
    if (bus_arrival ==  0)
    {
    con_Dad();
    }

    If the conditional expression evaluates as true, all statements within the curly braces are executed. If the conditional expression evaluates as false, all statements within the curly braces will be ignored, and the lines of code following the "if" block will be executed.

    Here's a simple program that illustrates the basics of the "if" statement.
    #!/usr/bin/perl
    # ask for a number
    print ("Gimme a number! ");
    $alpha = ;
    chomp ($alpha);
    # ask for a different number
    print ("Now gimme a different number! ");
    $beta = ;
    chomp ($beta);
    # check
    if ($alpha == $beta)
    {
    print("Can't you read, moron?
    I need two *different* numbers!\n");
    }
    print("This is the last line! Go away now!n");

    And they say that the popular conception of programmers as rude, uncouth hooligans is untrue!

    In addition to the "if" statement, Perl also allows you a couple of variants - the first is the "if-else" statement, which allows you to execute different blocks of code depending on whether the expression is evaluated as true or false.

    The structure of an "if-else" statement looks like this:
    if (condition)
    {
    do this!
    }
    else
    {
    do this!
    }

    In this case, if the conditional expression evaluates as false, all statements within the curly braces of the "else" block will be executed. Modifying the example above, we have
    #!/usr/bin/perl
    # ask for a number
    print ("Gimme a number! ");
    $alpha = <STDIN>;
    chomp ($alpha);
    # ask for a different number
    print ("Now gimme a different number! ");
    $beta = ;
    chomp ($beta);
    # check
    if ($alpha == $beta)
    {
    print("Can't you read, moron?
    I need two *different* numbers!\n");
    }
    else
    {
    print("Finally! Someone with active brain cells!\n");
    }

    And here's another example, this time using strings - the trigger condition here is the input string "yes".
    #!/usr/bin/perl
    # ask a question
    print ("Did you like Austin Powers 2:
    The Spy Who Shagged Me?\n");
    $response = ;
    chomp ($response);
    # check
    if ($response eq "yes")
    {
    print("Groovy, baby!\n");
    }
    else
    {
    print("Loser! May your soul rot in hell!\n");
    }



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