Home arrow Perl Programming arrow Page 4 - Perl 101 (Part 3) - Looping The Loop

Dos And Don'ts - Perl

Part 3 in our continuing series on the popular scripting language, Perl. This week's article teaches you more about Perl's controlstructures - including the FOR and WHILE loops - and also introduces you toPerl's array variables.

  1. Perl 101 (Part 3) - Looping The Loop
  2. While You Were Sleeping...
  3. ...Or Until You Wake Up
  4. Dos And Don'ts
  5. For Pete's Sake!
  6. Every Comedian Needs An Exit
  7. Grade School
  8. Playing With Friends
  9. So Many Choices...
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 5
June 15, 2000

print this article


The two control structures explained above test the conditional expressionfirst, and only proceed to execute the statements within the loop if theexpression evaluates to true. However, there are often occasions when youneed to execute a particular set of statements at least once before youcheck for a valid conditional expression.

Perl has a solution to this problem too - it offers the "do-while" and"do-until" constructs, which allow you to execute a series of statements atleast once before checking the conditional expression. Take a look:

do { do this! } while (condition)

do { do this! } until (condition)
In this case, Perl will only test for the condition *after* executing theloop once.

#!/usr/bin/perl # weighted employee evaluation program version 2.0 # call me WEEP II! # use DO to ensure that the statements # within the loop are executed at least once do { print ("Can we put you in a cubicle, cancel all your benefits, and pay you less than minimum wage? [y/n] "); $reply = <STDIN>; chomp($reply); } while($reply ne 'y'); print ("Heh heh! This company couldn't do without employees like you!\n");
As you can see, the "do" construct can help to make your code more compact- compare WEEP version 2.0 with the original above.

This article copyright Melonfire 2000. All rights reserved.

>>> More Perl Programming Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: