in reply to I just want to include another file
From the perldocs:
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.
|
is just like
|
except that it's more efficient and concise, keeps track of the current filename for error messages, searches the @INC libraries, and updates %INC if the file is found. It also differs in that code evaluated with do FILENAME cannot see lexicals in the enclosing scope; eval STRING 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.
If do cannot read the file, it returns undef and sets $! to the error. If do can read the file but cannot compile it, it returns undef and sets an error message in $@. If the file is successfully compiled, do returns the value of the last expression evaluated.
Note that inclusion of library modules is better done with the use and require operators, which also do automatic error checking and raise an exception if there's a problem.
You might like to use do to read in a program configuration file. Manual error checking can be done this way:
|
CountZero
"If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: I just want to include another file
by Cap'n Steve (Friar) on Sep 14, 2004 at 06:01 UTC | |
by ysth (Canon) on Sep 15, 2004 at 03:27 UTC |