Perl Flow Control Functions

by Geethalakshmi 2010-09-17 12:38:13

Perl Flow Control Functions


do BLOCK
Returns the value of the last command in the sequence of commands indicated by BLOCK. When modified by a loop modifier, executes the BLOCK once before testing the loop condition. (On other statements the loop modifiers test the conditional first.)
goto LABEL
Finds the statement labeled with LABEL and resumes execution there. Currently you may only go to statements in the main body of the program that are not nested inside a `do {}' construct. This statement is not implemented very efficiently, and is here only to make the sed-to-perl translator easier. I may change its semantics at any time, consistent with support for translated sed scripts. Use it at your own risk. Better yet, don't use it at all.
last LABEL
last
The last command is like the break statement in C (as used in loops); it immediately exits the loop in question. If the LABEL is omitted, the command refers to the innermost enclosing loop. The continue block, if any, is not executed:

line: while () {
last line if /^$/; # exit when done with header
...
}

next LABEL
next
The next command is like the continue statement in C; it starts the next iteration of the loop:

line: while () {
next line if /^#/; # discard comments
...
}

Note that if there were a continue block on the above, it would get executed even on discarded lines. If the LABEL is omitted, the command refers to the innermost enclosing loop.
redo LABEL
redo
The redo command restarts the loop block without evaluating the conditional again. The continue block, if any, is not executed. If the LABEL is omitted, the command refers to the innermost enclosing loop. This command is normally used by programs that want to lie to themselves about what was just input:

# a simpleminded Pascal comment stripper
# (warning: assumes no { or } in strings)
line: while () {
while (s|({.*}.*){.*}|$1 |) {}
s|{.*}| |;
if (s|{.*| |) {
$front = $_;
while () {
if (/}/) { # end of comment?
s|^|$front{|;
redo line;
}
}
}
print;
}

Tagged in:

731
like
0
dislike
0
mail
flag

You must LOGIN to add comments