Ah, but that's what BEGIN blocks are for. BEGIN { } blocks are executed at compile time.
#! /usr/bin/perl -T
BEGIN {
$DEZVAR = $ENV{CRROOTWSDIR};
if ($CRROOTWSDIR =~ /^([-\/\w.]+)$/) {
$MAINPATH = $1
} else {
die "Invalid path, please check setenv";
}
$LIB = "$MAINPATH/cr/cds/comms/x2p";
}
use lib $LIB;
blah;
blah;
blah;
The important thing here is that the use lib has to be outside the BEGIN block. If it were inside, it would be executed while the BEGIN block was being compiled, not while it was being executed.
Other useful blocks include: END {}, executed when your program is about to exit; CHECK {}, executed after your program has been compiled; and INIT {}, executed before your program starts running. The difference between the latter two is that perl -c will execute CHECK {} blocks, but not INIT {} blocks. CHECK {} was added in 5.6.0.
UPDATE: I don't know why, but BEGIN et al. are documented in perlmod. Thanks to merlyn for pointing me there! |