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) { &$var($parm); undef &$var; }
do 'stat.pl';
is just like
eval \`cat stat.pl\`;
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.line: while () { last line if /^$/; # exit when done with header ... }
line: while (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.) { next line if /^#/; # discard comments ... }
# 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;
}