Perl
  Home arrow Perl arrow Page 6 - 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 - 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

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