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.
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:
} while (condition)
} until (condition)
In this case, Perl will only test for the condition *after* executing theloop once.
# 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
print ("Can we put you in a cubicle, cancel all your benefits, and pay
you less than minimum wage? [y/n] ");
$reply = <STDIN>;
} 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.