Perl
  Home arrow Perl arrow Page 3 - Perl Conditionals
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 Conditionals
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 6
    2007-10-16


    Table of Contents:
  • Perl Conditionals
  • You Better or Else!
  • The Unless Statement
  • The While Loop

  • 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 Conditionals - The Unless Statement
    ( Page 3 of 4 )

    The Unless Statement is the opposite of the If statement in that it asks if a condition is false, and executes the code unless the condition is true.


    #!/usr/bin/perl


    $my_iq = 1000;

    $your_iq = 90;


    unless ($your_iq > $my_iq)

    {

    print "My brain is enormous and far superior to your own!";

    }

    In the above code, the program will print out the sentence: "My brain is enormous and far superior to your own!" every time, unless $you_iq is greater than $my_iq. 

    Getting Loopy

    Near the end of the day I always get a little loopy; I can't think straight and words just come out wrong. But I'm getting off topic here. Loops in code are something completely different.

    The purpose of a loop is to loop a piece of code so that you don't have to type it over a billion times. Let's take a look at the For Loop:


    #!/usr/bin/perl


    for ($count =1; $count<10; $count++)

    {

    print "I R THE GREATEST!n";

    }

    The above code created a variable name $count and assigned an initial value of 1. It also has a part that tells it to increment the value of $count by +1 ($greatness++) and to do so until the value of $count is 10 or greater ($count<10). So long as the value of $count is less than 10, the following will print to the screen:

      I R THE GREATEST!

      I R THE GREATEST!

      I R THE GREATEST!

      I R THE GREATEST!

      I R THE GREATEST!

      I R THE GREATEST!

      I R THE GREATEST!

      I R THE GREATEST!

      I R THE GREATEST!

    If we were working with numbers we could have also just assigned a numeric value to $count and have it do this:


    #!/usr/bin/perl


    for ($count =1; $count<10; $count++)

    {

    print "$countn";

    }

    That would print out the following to your screen:

      1

      2

      3

      4

      5

      6

      7

      8

      9

    The For Loop loops for the number of times you tell it to.



     
     
    >>> More Perl Articles          >>> More By James Payne
     

       

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