Control Structures (Part 3)-- Perl Study Notes
Operator syntax
if ($expression)
{
....
}
elsif ($expression)
{
...
}
else
{
...
}
unless ($expression)
{
...
}
while ($expression)
{
...
}
until ($expression)
{
...
}
do
{
...
} while ($expression)
do
{
...
} until ($expression)
for($initial_exp1;$test_exp;$reinit_exp)
{
...
}
foreach $value (@list)
{
...
} # $value get each element in the @list
foreach (@list)
{
...
} #$_ get each element in the @list
How to populate true or false in Perl
a. convert the expression to string
b. if the string is empty or "0", it returns false else true
"0" #false
0 #false
"00" #true
undef #false
1 #true
last: to exit from the loop
while (something) {
something;
something;
something;
if (somecondition) {
somethingorother;
somethingorother;
last; # break out of the while loop
}
morethings;
morethings;
}
# last comes here
next: to exit current loop and continue the next round loop
while (something) {
something;
something;
something;
if (somecondition) {
somethingorother;
somethingorother;
next; # break out of the while loop
}
morethings;
morethings;
# next comes here
}
redo: to jump to the beginning of the current block(without reevaluating the control expression)
while (somecondition) {
# redo comes here without evaluate the somecondition
something;
something;
something;
if (somecondition) {
somethingorother;
somethingorother;
redo; # break out of the while loop
}
morethings;
morethings;
}
The follow is another loop structure without any while/for/foreach/until statement
{
startstuff;
startstuff;
startstuff;
if (somecondition) {
last;
}
laterstuff;
laterstuff;
laterstuff;
redo;
} # this block will be looped until somecondition is true
Pls note that last/next/redo can not be used in the do{}while/until statement
Expression Modifiers
some_expression if control_expression
this is equivalent to
if(control_expression)
{
some_expression
}
exp2 unless exp1; # like : unless(exp1){exp2;}
exp2 while exp1; #like: while(exp1){exp2;}
exp2 until exp1; #like: until(exp1){exp2;}
&& and || Control Structures
this && that; # equivalent to that if this
this || that; # equivalent to that unless this