next up previous contents
Next: Input/Output Up: Programming in Perl Previous: Arrays   Contents

Control flow

Perl supports the normal branching and looping constructs found in C or Java:

The only thing to remember is that in Perl the braces around the statement blocks are compulsory, even they contain only one statement.

When evaluating the boolean condition in these statements, the number 0, the strings "0" and "" and the empty list () are all treated as false. All other values are true.

Because Perl does not have explicit declarations of numeric and string variables, there are separate comparison operators for numeric and string values. The symbolic operators <, <=, >, >=, == and != compare their arguments as numeric values while string comparison is denoted by the operators lt, le, gt, ge, eq and ne.

In addition to the basic compound statements above, Perl supports an abbreviation elsif for mulitply branching if statements, a version of if called unless and a version of while called until, both of which invert the sense of the condition (that is, the statement block is executed if the condition is false).

For stepping through a list, Perl provides a special loop called foreach.

  foreach $value (@list){       # $value is assigned to each item in
     print $value, "\n";        # @list in turn
  }


next up previous contents
Next: Input/Output Up: Programming in Perl Previous: Arrays   Contents
Madhavan Mukund 2004-04-29