_learning perl_, ch4, 1.1.2002, nh * statement blocks go in {s} * final ; optional (in statement blocks as well as in whole perl files) ------------- | IF blocks | ------------- if (condition1) { statment1; # block 1 statment2; } elsif (condition2) { # note "elsif" spelling -- no second 'e' otherstatement1; # as many "elsif"s as you want otherstatement1; # block 2 } else { stillotherstatement1; # block 3 stillotherstatement2; } <-- {s} required condition1 evaluated for trueness, then if true, block 1 is executed ^^^^^^^^ 0 or empty string = false anything else = true false | true ------|----- 0 | 00 "" | 0.00 1-1 | 1 | 2 similarly: UNLESS does something unless sth is true (duh) unless ($age < 18) { print "go vote; you're old enough."; } | `-> can also take an "else" ------------------------- | WHILE/UNTIL statement | ------------------------- while (condition) { statement1; statement2; } OR until (condition) { statement1; statement2; } can also use "do {} while/until", which evaluate the condition AFTER the body has been executed ----------------- | FOR statement | ----------------- as far as i can tell, just like C: | $i=1; for ($i=1; $i <= 10; $i++) { | while ($i <= 10) { statement; = statement; } | $i++; | } --------------------- | FOREACH statement | --------------------- "... takes a list of values and assignes them one at a time to a scalar variable, executing a block of code with each successive assignment." @a = (1,2,3,4,5); foreach $x (reverse @a) { # $x is a scalar that disappears when print $x; # the body of the loop exits -- local. } # C DOESN'T DO THIS. you don't have to specify the scalar, in which case perl pretends you use the generic $_ : foreach (reverse @a) { print; } --> scalar doesn't change, but you *can* change list values by using it: @a = (1,2,3,4); foreach $one (@a) { $one *= 3; } # @a is now (3,6,9,12)