International English version Traditional Chinese version
Domain Registration with Web HostingWeb site Hosting with Various supportRegistrar Transfer with Web Hosting

Flow control operations in PERL

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.

do subroutine (list)
Executes a SUBROUTINE declared by a sub-declaration, and returns the value of the last expression evaluated in SUBROUTINE. If there is no subroutine by that name, produces a fatal error. You may use the "defined" operator to determine if a subroutine exists. If you pass arrays as part of LIST you may wish to pass the length of the array in front of each array. See the section on subroutines later on. The parentheses are required to avoid confusion with the "do EXPR" form.

SUBROUTINE may also be a single scalar variable, in which case the name of the subroutine to execute is taken from the variable. As an alternate (and preferred) form, you may call a subroutine by prefixing the name with an ampersand: &foo(@args). If you aren't passing any arguments, you don't have to use parentheses. If you omit the parentheses, no @_ array is passed to the subroutine. The & form is also used to specify subroutines to the defined and undef operators:

   if (defined &$var) {
      undef &$var;
do expr
Uses the value of EXPR as a filename and executes the contents of the file as a perl script. Its primary use is to include subroutines from a perl subroutine library.
   do '';
is just like
   eval \`cat\`;
except that it's more efficient, more concise, keeps track of the current filename for error messages, and searches all the -I libraries if the file isn't in the current directory (see also the @INC array in predefined names). It's the same, however, in that it does reparse the file every time you call it, so if you are going to use the file inside a loop you might prefer to use -P and #include, at the expense of a little more startup time. The main problem with #include is that cpp doesn't grok # comments -- a workaround is to use ";#" for standalone comments. Note that the following are NOT equivalent:
   do $foo;	# eval a file
   do $foo();	# call a subroutine
Note that inclusion of library routines is better done with the "require" operator.

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 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 (<STDIN>) {
      last line if /^$/; # exit when done with header
next label
The next command is like the continue statement in C; it starts the next iteration of the loop:
   line: while (<STDIN>) {
      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
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 (<STDIN>) {
      while (s|({.*}.*){.*}|$1 |) {}
      s|{.*}| |;
      if (s|{.*| |) {
         $front = $_;
         while (<STDIN>) {
            if (/}/) {  # end of comment?
               redo line;
return list
Returns from a subroutine with the value specified. Note that a subroutine can automatically return the value of the last expression evaluated. That's the preferred method -- use of an explicit return is a bit slower.

Back to the Perl Expressions index

Home | Support | Check | Host | Transfer | Whois | More Technology Services

© 1996-2008 Wyith Ltd dba All rights reserved.

Canada | USA | UK | Belgium | Italy | Hongkong | Singapore