Perl Control and Conditional Statements,
Statement Modifiers
(and Conditional Operator)

if elsif else unless
while until do for foreach
goto next last redo
modifiers
Conditional Operator

In Perl conditionals and loops are defined in terms of a block rather than a statement (as in C) so curly braces are always required.


Conditional Control Statements


if(cond) {
    # if cond is true evaluate statements in block surrounded by curly braces,
    # curly braces are required
}


if(cond) {
   # stuff
} else {
   # more stuff
}


if(cond) {
} elsif(cond2) {
} elsif(cond3) {
...
} else {
}


unless(cond) {
    # if !cond is true evaluate statements in block surrounded by curly braces
    # no extra clause (elsunless)
}

Unconditional Control Statement (actually function)

goto LABEL # example #    label:
goto EXPR  # an expression that evaluates to a label name
goto &NAME
One can construct a FORTRAN like computed goto as follows:
    goto ("FOO", "BAR", "JUNK")[$i];
(May not work in compiled perl).

Loop Control Statements


while(cond) {
    # repeat block while cond is true
}


until(cond) {
    # repeat block while !cond is true
}

while(@ARGV) {
    process(shift @ARGV); # fifo pop
    # loop terminates when @ARGV is exhausted
}


do BLOCK # executes commands in block, returns last expression value
####
# when modified by a loop modifier the block
# is executed once before testing the condition.
####

do SUBROUTINE(LIST) # deprecated form of subroutine call

do EXPR # evaluates EXPR as a filename and executes
# the contents of the file as a Perl script. (See @INC array).



for(init; test; step) {
    # where init test step are expressions, like in C
}


foreach $user (@users) {
    # This block is executable for each element of array @users (or more
    # generally over any list), where loop variable $user is a reference 
    # to the element, not a copy.
    #
    # Note1 the the loop variable, $user in this case, is implcitly local
    # to the loop, overriding a more global variable of the same name,
    # e.g. declared with my().
    #
    # foreach is actaully an alias for for.
}

foreach $key (sort keys %hash) {
    # loop over sorted keys of a hash.
}

Loop Control Commands

# a loop (or any statement) can be labeled and referered to:
LINE: while($line=
) { last LINE if $line eq "\n"; # stop on first blank line next LINE if $line =~ /^#/; # skip comment lines ... }
next; # inside a loop skips to end of current loop (C: continue;)
next LABEL; # continue with next iteration of loop with LABEL

last; # inside a loop leaves current loop (C: break;)
last LABEL; # leave loop with LABEL

redo; # restart a loop without doing test or step
redo LABEL; # restart loop with LABELwithout doing test or step

A BLOCK by itself (labeled or not) is semantically equivalent to a loop that executes once. Thus you can use last to leave the block or redo to restart the block. A next will also exit the once-through block but will execute a continue block, while a lastdoes not.

There is no case or multiway switches in Perl, but you can construct a labelled block that looks like it:
SWITCH: {
   if(/^abc/) { $abc=1; last SWITCH; }
   if(/^def/) { $def=1; last SWITCH; }
   if(/^xyz/) { $xyz=1; last SWITCH; }
}

Simple Statement Conditional Modifier

Any simple statement may optionally be followed by a single modifier just before the terminating semicolon (or block ending). The possible modfiers are:
statement if EXPR;
statement unless EXPR;
statement while EXPR;
statement until EXPR;