perlfunc
gods
<P>
do - turn a
<FONT SIZE=-1>BLOCK</FONT> into a
<FONT SIZE=-1>TERM</FONT>
<P>
<HR>
<P>
do
<FONT SIZE=-1>BLOCK</FONT>
<P>
do
<CODE>SUBROUTINE(LIST)</CODE>
<P>
do
<FONT SIZE=-1>EXPR</FONT>
<P>
<HR>
<P>
Not really a function. Returns the value of the last command in the sequence of commands indicated by
<FONT SIZE=-1>BLOCK.</FONT> When modified by a loop modifier, executes the
<FONT SIZE=-1>BLOCK</FONT> once before testing the loop condition. (On other statements the loop modifiers test the conditional first.)
<P>
<FONT SIZE=-1>A</FONT> deprecated form of subroutine call. See [perlman:perlsub|the perlsub manpage].
<P>
Uses the value of
<FONT SIZE=-1>EXPR</FONT> 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.
<P>
<PRE> do 'stat.pl';
</PRE>
<P>
is just like
<P>
<PRE> scalar eval `cat stat.pl`;
</PRE>
<P>
except that it's more efficient and concise, keeps track of the current
filename for error messages, and searches all the <STRONG>-I</STRONG>
libraries if the file isn't in the current directory (see also the
<CODE>@INC</CODE> array in [%linkNodeTitle "perlman:perlvar|Predefined Names", $NODE, "Predefined Names",{anchor=>"Predefined_Names"};%]). It is also different in how code evaluated with <CODE>do FILENAME</CODE> doesn't see lexicals in the enclosing scope like <CODE>eval STRING</CODE> does. It's the same, however, in that it does reparse the file every time
you call it, so you probably don't want to do this inside a loop.
<P>
If [perlfunc:do|do] cannot read the file, it returns undef and sets <CODE>$!</CODE> to the error. If [perlfunc:do|do] can read the file but cannot compile it, it returns undef and sets an error
message in <CODE>$@</CODE>. If the file is successfully compiled, [perlfunc:do|do] returns the value of the last expression evaluated.
<P>
Note that inclusion of library modules is better done with the
[perlfunc:use|use()] and [perlfunc:require|require()] operators, which also do automatic error checking and raise an exception if
there's a problem.
<P>
You might like to use [perlfunc:do|do] to read in a program configuration file. Manual error checking can be done
this way:
<P>
<PRE> # read in config files: system first, then user
for $file ("/share/prog/defaults.rc",
"$ENV{HOME}/.someprogrc") {
unless ($return = do $file) {
warn "couldn't parse $file: $@" if $@;
warn "couldn't do $file: $!" unless defined $return;
warn "couldn't run $file" unless $return;
}
}
</PRE>
<HR>