Highlights
All Snippets
Top 100 Snippets
Librarians
gbCodeLib

By Language
VB6
JavaScript
Perl
HTML
SQL
Java
DOS

GBIC >> Source Code >> Perl >> Snippet

11 Conditional flow


Perl has 4 loop modifiers:     while , until , for , foreach

if (expression)
 BLOCK
elsif
 BLOCK
else
 BLOCK

while (expression) BLOCK       # while(1) BLOCK  will loop forever.  use "last" to exit

for ( initialization; test; increment) BLOCK

last         # exit while once current loop is complete
            # example:   last if ($i == 10)
redo         # redo
next       # next
goto LABEL     # absolute flow control

foreach $element ( @myarray )     # $element is a reference to each value of @myarray
   { print "$element \n " }       # the (@myarray) can also be a list ("one", "two", "three")

until (expression) Block     

Label - control statements       # label can be used with next/last/redo to control looping

next      # jumps to the next iteration         - if used with label, jumps to loop marked by label
last      # immediately breaks out of the loop  - if used with label, jumps to loop marked by label
redo      # causes loop to execute again        - if used with label, jumps to loop marked by label

LBL:   foreach ( @fruit )    {
                foreach $place ( @state )    {
                    last LBL if $place eq "Texas" ;   # use of label drops out of nested loops!
  }
        }

Simplest is to execute a statement based on True / False condition of an expression:

# Perl has four modifiers: if, unless, while, until

if (test - expression) BLOCK             # primary format
   - or -
(expression) if (test - expression)      # alternate format lets if come last
                                      # example:   last if ($i == 10)

Examples of modifier at end of line:

$max = 100 if $min < 100 ;             # executes first statement if ($min < 100) is True
print "Success" unless $error > 2;    
$total ++ while $total < 500            # executes repeatedly
$a = $a + 6 until $a > 100             # executes repeatedly


Compound statements:
Execution of expressions can depend on other expressions using one of the modifiers if , unless , while or until , for example:

EXPR1 if EXPR2 ;
EXPR1 until EXPR2 ;

The logical operators || , && or ?: also allow conditional execution:
EXPR1 || EXPR2 ;
EXPR1 ? EXPR2 : EXPR3 ;

Statements can be combined to form a BLOCK when enclosed in {} . Blocks may be used to control flow:
if (EXPR) BLOCK [ [ elsif (EXPR) BLOCK ... ] else BLOCK ]
unless (EXPR) BLOCK [ else BLOCK ]
[ LABEL: ] while (EXPR) BLOCK [ continue BLOCK ]
[ LABEL: ] until (EXPR) BLOCK [ continue BLOCK ]
[ LABEL: ] for (EXPR; EXPR; EXPR) BLOCK

[ LABEL: ] foreach VAR† (LIST) BLOCK
[ LABEL: ] BLOCK [ continue BLOCK ]

Program flow can be controlled with:

goto LABEL  continue execution at the specified label .  
last [ LABEL ] Immediately exits the loop in question . Skips continue block .  
next [ LABEL ] Starts the next iteration of the loop .  
redo [ LABEL ]  Restarts the loop block without evaluating the conditional again .  

Special forms are:
do BLOCK while EXPR ;
do BLOCK until EXPR ;
which are guaranteed to perform BLOCK once before testing EXPR, and